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
- Jsp的form表单各种提交方式和后台请求参数读取
- jquery多选择动态参数的使用以及ajax异步请求中提交整个form表单中serializeArray()的使用以及后台值得获取方式
- form表单提交get请求,后台接收乱码的解决方案
- JFinal form表单提交请求参数的获取问题
- struts2/jsp 通过form表单方式或者js的方式提交请求,封装boolean值的问题
- jsp的form表单提交几种方式
- form表单提交,后台怎么获取select的值?后台直接获取即可,和input方式一样。
- form表单的提交方式
- form 表单提交的方式
- JSP中FORM的POST方式提交参数中文乱码
- form表单中的get和post提交请求的区别
- js 提交form表单 和 设置form 表单请求路径
- jsp表单用图标实现form的重置和提交
- 表单提交和超链接请求传递参数的几种方式
- 表单提交和超链接请求传递参数的几种方式
- Form表单提交参数类型与后台实体参数类型的问题
- form表单的提交方式:submit和button.
- HTML form 表单提交方式get和post的区别
- 几种简单排序
- JavaScript常用事件及其区别
- 【Angular】ng-disabled的用法;
- DCM示例文件下载网站
- Codeforces Round #431 (Div. 2)(有E题!)比赛总结
- Jsp的form表单各种提交方式和后台请求参数读取
- 【美团】 动态规划问题
- Linux命令总结
- 一句话代码判断是否为闰年
- 【Linux学习笔记】36:其它环境变量配置文件
- [bzoj2154]Crash的数字表格
- 08-Thrift高级2
- 学习笔记
- 关于BootstrapTable的使用