通过HttpServletRequest获取表单提交的数据:
前端页面:
1 <html> 2 <head> 3 <title>register</title> 4 <meta name="keywords" content="keyword1,keyword2,keyword3"> 5 <meta name="description" content="this is my page"> 6 <meta name="content-type" content="text/html; charset=UTF-8"> 7 </head> 8 <body> 9 <form action="/prj-servlet-12/user/save" method="post"> 10 用户名 11 <input type="text" name="username"> 12 <br> 13 密码 14 <input type="password" name="password"> 15 <br> 16 性别 17 男<input type="radio" name="sex" value="m"> 18 女<input type="radio" name="sex" value="f"> 19 <br> 20 爱好 21 <input type="checkbox" name="interest" value="sport">运动 22 <input type="checkbox" name="interest" value="music">音乐 23 <input type="checkbox" name="interest" value="dance">跳舞 24 <input type="checkbox" name="interest" value="study">学习 25 <input type="checkbox" name="interest" value="read">阅读 26 <br> 27 学历 28 <select name="grade"> 29 <option value="gz">高中</option> 30 <option value="dz">大专</option> 31 <option value="db">大本</option> 32 </select> 33 <br> 34 简历 35 <textarea rows="10" cols="60" name="introduce"></textarea> 36 <br> 37 <input type="submit" value="注册"> 38 <input type="reset" value="重置"> 39 40 </form> 41 </body> 42 </html>
前端页面的name和value对应request对象中Map集合的name和value
Servlet程序:
1 package com.bjpowernode.javaweb.servlet; 2 3 import java.io.IOException; 4 import java.util.Enumeration; 5 import java.util.Map; 6 import java.util.Set; 7 8 import javax.servlet.ServletException; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 public class SaveUserServlet extends HttpServlet { 14 15 private static final long serialVersionUID = 1L; 16 17 @Override 18 protected void doPost(HttpServletRequest request, HttpServletResponse response) 19 throws ServletException, IOException { 20 //获取用户信息,表单提交,这些数据被自动封装到request对象中了 21 //从request中获取这些信息 22 String username = request.getParameter("username"); 23 String password = request.getParameter("password"); 24 String sex = request.getParameter("sex"); 25 String interest = request.getParameter("interest"); 26 String grade = request.getParameter("grade"); 27 String introduce = request.getParameter("introduce"); 28 29 System.out.println(username); 30 System.out.println(password); 31 System.out.println(sex); 32 System.out.println(interest); 33 System.out.println(grade); 34 System.out.println(introduce); 35 36 //这个方法适合取checkbox中的数据 37 String[] interests = request.getParameterValues("interest"); 38 for(String in : interests){ 39 System.out.println(in); 40 } 41 42 //获取整个参数Map集合 43 @SuppressWarnings("unchecked") 44 Map<String,String[]> parameterMap = request.getParameterMap(); 45 Set<String> names = parameterMap.keySet(); 46 for(String name : names){ 47 String[] values = parameterMap.get(name); 48 System.out.print(name + "="); 49 for(String value : values){ 50 System.out.println(value + " "); 51 } 52 System.out.println(); 53 } 54 55 //获取Map集合所有的key 56 @SuppressWarnings("unchecked") 57 Enumeration<String> keys = request.getParameterNames(); 58 while(keys.hasMoreElements()){ 59 String key = keys.nextElement(); 60 System.out.println(key); 61 } 62 63 } 64 65 }
具体介绍HttpServletRequest接口的常用方法:
代码示例:
1 package com.bjpowernode.javaweb.servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class TestRequestServlet extends HttpServlet { 12 13 private static final long serialVersionUID = 1L; 14 15 @Override 16 protected void doGet(HttpServletRequest request, HttpServletResponse response) 17 throws ServletException, IOException { 18 19 response.setContentType("text/html;charset=UTF-8"); 20 PrintWriter out = response.getWriter(); 21 22 //获取上下文路径【webapp的根路径】 23 String contextPath = request.getContextPath(); 24 out.print("context path = " + contextPath + "<br>"); 25 26 //获取浏览器的请求方式 27 String method = request.getMethod(); 28 out.print("method = " + method + "<br>"); 29 30 //获取请求的URI、URL、Servlet Path 31 String uri = request.getRequestURI(); 32 out.print("uri = " + uri + "<br>"); 33 34 String url = request.getRequestURL().toString(); 35 out.print("url = " + url + "<br>"); 36 37 String servletPath = request.getServletPath(); 38 out.print("servletPath = " + servletPath + "<br>"); 39 40 //获取客户端IP地址 41 String clietIp = request.getRemoteAddr(); 42 System.out.println("client ip = " + clietIp); 43 } 44 }
代码示例(包含请求转发,在两个Servlet中传递数据,关于请求转发,参考此链接):
1 package com.bjpowernode.javaweb.servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 import com.bjpowernode.javaweb.entity.User; 11 12 public class AServlet extends HttpServlet { 13 14 private static final long serialVersionUID = 1L; 15 16 @Override 17 protected void doGet(HttpServletRequest request, HttpServletResponse response) 18 throws ServletException, IOException { 19 //创建User对象 20 User user = new User(); 21 user.setUsercode("111"); 22 user.setUsername("zhangsan"); 23 24 //将User对象存储到request范围中 25 request.setAttribute("userObj", user); 26 27 //从request范围中取出数据 28 //Object obj = request.getAttribute("userObj"); 29 //response.getWriter().print(obj); 30 31 //跳转 32 //执行完AServlet之后,跳转到BServlet执行,将AServlet执行和BServlet执行放到同一个请求当中 33 //必须使用转发技术 34 //forward[转发] 35 //1、获取请求转发器对象[以下转发器指向了BServlet] 36 // RequestDispatcher dispatcher = request.getRequestDispatcher("/b"); 37 38 //2、调用请求转发器的forward方法即可完成转发 39 // dispatcher.forward(request, response); 40 41 //转发[转发是一次请求] 42 request.getRequestDispatcher("/b").forward(request, response); 43 44 } 45 }
1 package com.bjpowernode.javaweb.servlet; 2 3 import java.io.IOException; 4 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 10 public class BServlet extends HttpServlet { 11 12 private static final long serialVersionUID = 1L; 13 14 @Override 15 protected void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 18 Object obj = request.getAttribute("userObj"); 19 response.getWriter().print(obj); 20 } 21 }
还剩最后三个方法没介绍,setCharacterEncoding、getCookie、getSession将在后面分别介绍。
Servlet笔记7--HttpServletRequest介绍
原文:http://www.cnblogs.com/qjjazry/p/6371715.html