另外还有一个就是getParameterMap()方法,但是我自己去翻API,竟然没有找见,难道是API过时了?先不管,这个方法返回的所有的参数和参数的值,被封装成了一个map返回。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%>Servlet表单提交
package linkin;import java.io.IOException;import java.util.Arrays;import java.util.Enumeration;import java.util.Map;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class LinkinServlet extends HttpServlet{ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); String userName = req.getParameter("userName"); System.out.println(userName); String[] schools = req.getParameterValues("school"); for (String school : schools) { System.out.println(school); } Enumeration enumeration = req.getParameterNames(); while (enumeration.hasMoreElements()) { String name = (String) enumeration.nextElement(); System.out.println(name + ":" + req.getParameter(name)); } Mapmap = req.getParameterMap(); for (String key : map.keySet()) { System.out.println(Arrays.asList(map.get(key))); } for (Map.Entry entry : map.entrySet()) { System.out.println(entry.getKey() + ":" + Arrays.asList(entry.getValue())); } System.out.println(req.getMethod()); System.out.println(req.getQueryString()); Enumeration enumeration1 = req.getHeaderNames(); while (enumeration1.hasMoreElements()) { System.out.println(enumeration1.nextElement() + ":" + req.getHeader((String) enumeration1.nextElement())); } /* * host:keep-alive * content-length:max-age=0 * accept:http://localhost:8080 * user-agent:application/x-www-form-urlencoded * referer:gzip, deflate * accept-language:JSESSIONID=4214F4CE399E02730C3219CF0BD83623 */ } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); }}
2,传参LinkinServlet linkin.LinkinServlet name LinkinPark... LinkinServlet /LinkinServlet
setAttribute public void setAttribute(String name, Object object);
这个方法在请求中添加一个属性,这个属性可以被其他可以访问这个请求对象的对象(例如一个嵌套的Servlet)使用。传参一般用setAttribute() 方法,不管是request,还是session,还是application都可以用这个方法将值放进去,不同的只是放入的范围不同而已。
package linkin;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class LinkinServlet extends HttpServlet{ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); String userName = req.getParameter("userName"); String passWord = req.getParameter("passWord"); req.setAttribute("userName", userName); req.getSession().setAttribute("passWord", passWord); req.getRequestDispatcher("/Linkin1.jsp").forward(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); }}
LinkinServlet linkin.LinkinServlet name LinkinPark... LinkinServlet /LinkinServlet
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%>Servlet表单提交
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%>Servlet显示数据 OK,成功跳转到页面。 姓名:<%=request.getAttribute("userName") %>; 年龄:<%=session.getAttribute("passWord") %>