1、传递的请求参数如何获取
GET方式: 参数放在URI后面
POST方式: 参数放在实体内容中
核心的API:
request.getParameter("参数名"); 根据参数名获取参数值(注意,只能获取一个值的参数)
request.getParameterValue("参数名“);根据参数名获取参数值(可以获取多个值的参数)
request.getParameterNames(); 获取所有参数名称列表
2、请求参数的编码问题
修改post方式的参数编码
request.setCharacterEncoding("utf-8");
修改get方式的参数编码
手动解码:String name =new String(name.getBytes(iso-8859-1"),"utf-8");
相关代码实例
1、index.html
<%@ 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 'index.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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="/HttpTest/HttpTest" method="get"> 用户名:<input type="text" name="name"></br> 密 码 :<input type="text" name="password"></br> 性 别 : <input type="radio" name="sex" value="男"/>男 <input type="radio" name="sex" value="女"/>女<br/> 籍 贯 :<select name="jiguan"> <option value="广东">广东</option> <option value="湖北">湖北</option> <option value="湖南">湖南</option> </select><br/> 爱 好 : <input type="checkbox" name="habit" value="篮球"/>篮球 <input type="checkbox" name="habit" value="足球"/>足球 <input type="checkbox" name="habit" value="棒球"/>棒球<br/> <textarea rows="5" cols="10" name="info"></textarea><br/> <input type="hidden" name="id" value="001"> <input type="submit" value="提交"> </form> </hr> </br> </br> <form action="/HttpTest/HttpTest" method="POST"> 用户名:<input type="text" name="name"></br> 密 码 :<input type="text" name="password"></br> 性 别 : <input type="radio" name="sex" value="男"/>男 <input type="radio" name="sex" value="女"/>女<br/> 籍 贯 :<select name="jiguan"> <option value="广东">广东</option> <option value="湖北">湖北</option> <option value="湖南">湖南</option> </select><br/> 爱 好 : <input type="checkbox" name="habit" value="篮球"/>篮球 <input type="checkbox" name="habit" value="足球"/>足球 <input type="checkbox" name="habit" value="棒球"/>棒球<br/> <textarea rows="5" cols="10" name="info"></textarea><br/> <input type="hidden" name="id" value="001"> <input type="submit" value="提交"> </form> </body> </html>
2、Servlet代码
package com.gqx.test; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HttpTest extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 设置查询参数编码 * 该方法只能对请求实体内容的数据编码起作用。post提交的数据在试题内容中,所以该方法对post有效 * get方法是将参数昂在uri的后面,所以对get方式无效,所以要对get重新编码 */ request.setCharacterEncoding("utf-8"); Enumeration<String> names=request.getParameterNames(); while (names.hasMoreElements()) { String string = (String) names.nextElement(); if (string.equals("habit")) { String[] habit=request.getParameterValues("habit"); System.out.print(string+":"); for (String string1 : habit) { if (request.getMethod().equals("GET")) { //手动重新编码(iso-8859-1字符串——>utf-8字符串) string1=new String(string1.getBytes("iso-8859-1"),"utf-8"); } System.out.print(string1+" "); } System.out.println(); }else { String value=request.getParameter(string); if (request.getMethod().equals("GET")) { //手动重新编码(iso-8859-1字符串——>utf-8字符串) value=new String(value.getBytes("iso-8859-1"),"utf-8"); } System.out.println(string+":"+value); } } } //接收post方式的请求 @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doGet(req, resp); } }
运行截图