目的:仅仅使用Jersey将页面表单中的数据传到后台处理后返回一个结果给前台;
准备工作:Jerseyjar包导入
1)web.xml配置
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <context-param> <param-name>webAppRootKey</param-name> <param-value>jersey.root</param-value> //项目根级名 </context-param> <servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class> com.sun.jersey.spi.container.servlet.ServletContainer </servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>com.medivh.control</param-value> //服务处理类所在包 </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/jersey/*</url-pattern> //定义项目服务第一级访问路径: jersey </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> </span>
2)前台页面配置 index.jsp
<span style="font-size:14px;"><%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>测试使用</title> <script src="js/jquery.js"></script> <script type="text/javascript"> function post() { //利用Jquery封装的Ajax向后台发送表单信息 $.ajax({ url :"jersey/login/get", type : "POST", data:$("#user").serialize(), success : function(obj) { var htmlstr = "姓名:"+obj.username+" 密码:"+obj.pwd+" 信息:"+ obj.msg; alert(htmlstr); } }); } </script> </head> <body> <form id="user" name="user" > 用户名:<input type="text" name="username" id="username" ><br> 密 码:<input type="password" name="pwd" id="pwd"><br> <a href="javascript:post()"> 提交</a> </form> </body> </html> </span>
3)JAVA类1: UserService.java
<span style="font-size:14px;">package com.medivh.control; import javax.ws.rs.Consumes; import javax.ws.rs.FormParam; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.springframework.stereotype.Component; import com.medivh.pojo.User; @Component @Path("/login") public class UserService { @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) //接受的参数类型为表单信息 @Path("/get") @Produces({MediaType.APPLICATION_JSON}) //返回的数据类型为JSON类型 public User update(@FormParam("username") String username,@FormParam("pwd") String pwd)//接受了两个表单参数; { User user = new User(); user.setUsername(username); user.setPwd(pwd); user.setMsg("你已经成功将表单数据提交了过来!"); return user; } } </span>
4)JAVA类2:User.Java
<span style="font-size:14px;">package com.medivh.pojo; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="user") public class User { private String username; private String pwd; private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } } </span>
5)tomcat 中server.xml 添加如下代码(我的tomcat端口为80)
<span style="font-size:14px;"><Context path="/jersey" docBase="D:workspacejerseyWebRoot" debug="0" privileged="true"></Context></span>
6)浏览器访问路径
localhost:jersey
源代码0分下载地址:http://download.csdn.net/detail/wow4464/7340387