request对象封装的是请求的数据,由服务器创建,作为实参传递给Servlet的方法,一个请求对应一个request对象,request对象可以获得请求数据。
1、获取请求行信息
(1)get提交
<body bgcolor="#f5f5dc">
<center>
<h3>登录</h3>
<form action="http://localhost:8080/MyServlet_war_exploded/abc" method="get">
用户名:<input type="text" name="myname" size=""><br>
密 码:<input type="password" name="mypassword" size="" ><br><br>
<input type="reset" value="取消">
<input type="submit" value="登录">
</form> </center>
</body>
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream out=response.getOutputStream();
String method= request.getMethod();
System.out.println(method);
String URI=request.getRequestURI();
System.out.println(URI);
StringBuffer URL=request.getRequestURL();
System.out.println(URL);
String path=request.getContextPath();
System.out.println(path);
}
运行结果:
getMethod():获取提交方式
getRequestURI():URI
getRequestURL():URL
getContextPath():项目名称
(2)post提交
2、获取请求头信息
(1)获取请求头的一条信息:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String header=request.getHeader("Host");
System.out.println(header);
}
(2)获取请求头的所有信息:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Enumeration<String> headerNames=request.getHeaderNames();//获取键的名字
while(headerNames.hasMoreElements()){
String headerName= headerNames.nextElement();
String headerValue=request.getHeader(headerName);
System.out.println(headerName+":"+headerValue);
}
}
3、获取用户信息
(1)get提交:
<center>
<h3>登录</h3>
<form action="http://localhost:8080/MyServlet_war_exploded/abc" method="get">
用户名:<input type="text" name="userName" size=""><br>
密 码:<input type="password" name="password" size="" ><br><br>
<input type="reset" value="取消">
<input type="submit" value="登录">
</form> </center>
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name=request.getParameter("userName");
String password=request.getParameter("password");
System.out.println(name+":"+password);
}
(2)post提交:
post提交与get提交的运行结果相同。
4、对用户提交的数据的同键不同值的处理
(1)获取提交数据的值:
<form action="http://localhost:8080/MyServlet_war_exploded/abc" method="post">
<input type="checkbox" name="book" value="xiangzi">《骆驼祥子》<br>
<input type="checkbox" name="book" value="xiyou">《西游记》<br>
<input type="checkbox" name="book" value="shuihu">《水浒传》<br>
<input type="checkbox" name="book" value="hongloumemg">《红楼梦》<br>
<input type="checkbox" name="book" value="sanguo">《三国演义》<br>
<input type="reset" value="取消">
<input type="submit" value="确定">
</form>
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String[] books= request.getParameterValues("book");
if(books!=null){
for(String shu:books){
System.out.println(shu);
}
}
}
(2)获取名字:
<center>
<h3>您喜欢的书有哪些:</h3>
<form action="http://localhost:8080/MyServlet_war_exploded/abc" method="post">
<input type="checkbox" name="book1" value="xiangzi">《骆驼祥子》<br>
<input type="checkbox" name="book2" value="xiyou">《西游记》<br>
<input type="checkbox" name="book3" value="shuihu">《水浒传》<br>
<input type="checkbox" name="book4" value="hongloumemg">《红楼梦》<br>
<input type="checkbox" name="book5" value="sanguo">《三国演义》<br>
<input type="reset" value="取消">
<input type="submit" value="确定">
</form>
</center>
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Enumeration<String> names=request.getParameterNames();
while(names.hasMoreElements()){
System.out.println(names.nextElement());
}
}
(3) 获取键和值:
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String,String[]> map=request.getParameterMap();
for(Map.Entry<String,String[]> entry:map.entrySet()){
System.out.println(entry.getKey());
for(String str:entry.getValue()){
System.out.println(str);
}
}
}