Jsp的form表单各种提交方式和后台请求参数读取

来源:互联网 发布:小智 超级真新人 知乎 编辑:程序博客网 时间:2024/05/19 21:59

//=================项目结构==============\\


//==================简单的jsp的form的action请求和参数解析








//============================================Ajax的请求参数提交================================\\

<%@ 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>    <base href="<%=basePath%>">        <title>My JSP 'ajaxForm.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!-- <script type="text/javascript" src="jsplus/jquery/jquery-3.2.1.js"></script> --><script type="text/javascript" src="<%=request.getContextPath()%>/jsplus/jquery/jquery.js"></script>  </head>    <body style="text-align: center; padding-top: 300px;">   <form id="submitForm" method="post">   用户名:   <input name="useraccount" id="useraccount" type="text"/>   <br/>   密码:   <input name="userpassword" id="userpassword" type="text"/>   <br/>   <br/><input name="loginBtn" type="submit" onclick="submitForm()" />      </form>  </body>  <script type="text/javascript">function submitForm() {var arry={};arry.useraccount=$("#useraccount").val();arry.userpassword=$("#userpassword").val();$.ajax({url : "<%=request.getContextPath()%>/AjaxServlet",type : "post",data : arry,async : false,cache : false,beforeSend : function() {if (!$("#useraccount").val()) {alert("用户名不能为空!");return false;}},error : function(request) {alert("请求异常!!!");},success : function(data) {//var r = jQuery.parseJSON(data);}});}    </script>  </html>
package com.test.user.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet(name="/AjaxServlet",urlPatterns={"/AjaxServlet"})public class AjaxServlet extends HttpServlet{/** * SERID */private static final long serialVersionUID = 5222793251610509038L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {String useraccount =req.getParameter("useraccount");String userpassword =req.getParameter("userpassword");System.out.println("Ajax_获取表单数据:"+useraccount+"=>"+userpassword);}}


//===============================jquery.form.js的ajax表单提交


<%@ 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>    <base href="<%=basePath%>">        <title>My JSP 'jqeryform.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><script type="text/javascript" src="jsplus/jquery/jquery-3.2.1.js"></script><script type="text/javascript" src="jsplus/jquery/jquery.js"></script>  </head>    <body style="text-align: center; padding-top: 300px;">   <form id="submitForm" method="post">   用户名:   <input name="useraccount" id="useraccount" type="text"/>   <br/>   密码:   <input name="userpassword" id="userpassword" type="text"/>   <br/>   <br/>   </form><input name="loginBtn" type="submit" onclick="submitForm()" />     </body>  <script type="text/javascript">function submitForm() {$.ajax({url : "<%=request.getContextPath()%>/FormServlet",type : "post",data : $('#submitForm').serialize(),dataType:"json",async : false,cache : false,beforeSend : function() {alert("发送的参数为"+$('#submitForm').serialize());if (!$("#useraccount").val()) {alert("用户名不能为空!");return false;}},error : function(request) {alert("请求异常!!!");},success : function(data) {//直接地区data的msgalert("直接解析_的msg:"+data.msg+",flag:"+data.flag+",obj:"+data.obj);}});}    </script>  </html>
package com.test.user.servlet;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.http.protocol.HTTP;import com.alibaba.fastjson.JSONObject;import com.test.user.po.User;@WebServlet(name="/FormServlet",urlPatterns={"/FormServlet"})public class FormServlet extends HttpServlet{/** * SERID */private static final long serialVersionUID = 5222793251610509039L;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{doPost(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {//从jquery序列化的流中读取请求参数String reqBody = "";// 读取请求内容BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream(), HTTP.UTF_8));String line = null;StringBuilder sb = new StringBuilder();while ((line = br.readLine()) != null) {sb.append(line);}reqBody = sb.toString();System.out.println("_______:"+reqBody);//直接getParameters读取参数System.out.println("测试jquery序列化参数:"+req.getParameter("useraccount"));//返回json串给前端jqyery解析User user = new User(); user.setUseraccount("zhagnsan"); user.setUserpassword("123456");JSONObject jo=new JSONObject();jo.put("flag", "success");jo.put("msg", "读取返回json数据");jo.put("obj", user);resp.getWriter().write(jo.toJSONString());}}






源码:http://pan.baidu.com/s/1hsaNoYo

阅读全文
0 0