Http(2)
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);
}
}
运行截图

随机推荐
- java中动态反射
java中动态反射能达到的效果和python的语法糖很像,能够截获方法的实现,在真实方法调用之前和之后进行修改,甚至能够用自己的实现进行特别的替代,也可以用其实现面向切片的部分功能.动态代理可以方便实 ...
- SNA社交网络算法
社交网络需要用到igraph库,所以需要安装.可以在lfd的网站 http://www.lfd.uci.edu/~gohlke/pythonlibs/ 上下载python_igraph,具体的pyth ...
- CommonsChunkPlugin的一些总结
CommonsChunkPlugin 官方文档地址 https://webpack.github.io/docs/list-of-plugins.html#commonschunkplugin new ...
- iOS打电话
1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示NSMutableString * str=[[NSMutableString alloc] initWithFo ...
- 【转】来自GDXB大大大大的小总结
一 最短路 模型一 增加限制 例:给定一个图,求起点到终点的最短路,其中你可以使用最多k次机会使某条边的边权变为x. 解法:把每个点拆成k个点,分别表示还能使用多少次机会,构造新图. 模型二 一个点 ...
- const char*, char const*, char*const的区别
http://www.cnblogs.com/aduck/articles/2244884.html
- Android开源项目发现---ListView篇(持续更新)
资料转载地址:https://github.com/Trinea/android-open-project 1. android-pulltorefresh 一个强大的拉动刷新开源项目,支持各种控件下 ...
- 2.5.5 使用DatePickerDialog, TimePickerDialog
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...
- 【CF】222 Div.1 B Preparing for the Contest
这样类似的题目不少,很多都是一堆优化条件求最优解,这个题的策略就是二分+贪心.对时间二分, 对费用采用贪心. /* 377B */ #include <iostream> #include ...
- 【CF】196 Div.2 D. Book of Evil
显然这个图是一课树,看着题目首先联想到LCA(肯定是可以解的).但是看了一下数据大小,应该会TLE.然后,忽然想到一个前面做过的题目,大概是在一定条件下树中某结点旋转成为根后查询最长路径.结果灵感就来 ...