String getparamter(String name) :根据参数名获取参数值, username=zs &password=123
@WebServlet("/requestDemo1") public class requestDemo1 extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("username"); System.out.println("get"); System.out.println(username); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { /* String username = req.getParameter("username"); System.out.println("post"); System.out.println(username);*/ this.doPost(req,resp); }
String[] getParametervalue(String name):根据参数名获取参数值的数组 hobby=xx&hobby=game
@WebServlet("/requestDemo1") public class requestDemo1 extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("username"); String[] hobbies = req.getParameterValues("hobby"); for (String hobby : hobbies) { System.out.println(hobby); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req,resp); }
Enumeration<String> getParamterNames() 获取所有请求的参数名称
@WebServlet("/requestDemo1") public class requestDemo1 extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("username"); Enumeration<String> parameterNames = req.getParameterNames(); while (parameterNames.hasMoreElements()){ String name = parameterNames.nextElement(); System.out.println(name); String parameter = req.getParameter(name); System.out.println(parameter); System.out.println("-----------"); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req,resp); }
map<String,String[]> getParamter():获取所有参数的map结合
@WebServlet("/requestDemo1") public class requestDemo1 extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("username"); Map<String, String[]> parameterMap = req.getParameterMap(); Set<String> set = parameterMap.keySet(); for (String s : set) { String[] strings = parameterMap.get(s); System.out.println(s); for (String string : strings) { System.out.println(string); } System.out.println("--------------------------"); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req,resp); }
Request_获取请求参数中文乱码问题处理
中文乱码问题:
get方式:tomcat 8 已经将get方式乱码问题解决了
post方式:会乱码
解决:在获取参数前,设置request的编码request.setCharacterEncoding("utf-8");
@WebServlet("/requestDemo2") public class requestDemo2 extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String username = req.getParameter("username"); System.out.println(username); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req,resp); } }