• Jersey处理表单Post信息


    目的:仅仅使用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

  • 相关阅读:
    测试rar/bz2/tar.gz/gz压缩文档完整性
    php中ajax调用出错的问题
    WIN32_LEAN_AND_MEAN宏
    Windows XP SP3下编译安装check-0.10.0
    Windows XP SP3下成功编译CUint2.1-3
    WebService客户端调用的几种方式
    Webservice 返回数据集 DataSet 及Android显示数据集LiveBindings
    REST 服务器调试 RESTDebugger.exe 和浏览器测试 webservice 调试工具
    delphi android路径 TPath 文件路径,文件管理 file path
    RESTClient 控件 从服务器获得数据集 REST
  • 原文地址:https://www.cnblogs.com/MedivhQ/p/4074980.html
Copyright © 2020-2023  润新知