request对象封装的是请求的数据,由服务器创建,作为实参传递给Servlet的方法,一个请求对应一个request对象,request对象可以获得请求数据。

1、获取请求行信息

(1)get提交

 <body bgcolor="#f5f5dc">
<center>
<h3>登录</h3>
<form action="http://localhost:8080/MyServlet_war_exploded/abc" method="get">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;用户名:<input type="text" name="myname" size=""><br>
密&nbsp;&nbsp;码:<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);
}

request对象的方法-LMLPHP

运行结果:

request对象的方法-LMLPHP

getMethod():获取提交方式
getRequestURI():URI
getRequestURL():URL
getContextPath():项目名称
(2)post提交
request对象的方法-LMLPHP

request对象的方法-LMLPHP

2、获取请求头信息

(1)获取请求头的一条信息:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {
String header=request.getHeader("Host");
System.out.println(header);
}

request对象的方法-LMLPHP

(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);
}
}

request对象的方法-LMLPHP

request对象的方法-LMLPHP

 3、获取用户信息

(1)get提交:

<center>
<h3>登录</h3>
<form action="http://localhost:8080/MyServlet_war_exploded/abc" method="get">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;用户名:<input type="text" name="userName" size=""><br>
密&nbsp;&nbsp;码:<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);
}

request对象的方法-LMLPHP

request对象的方法-LMLPHP

(2)post提交:

request对象的方法-LMLPHP

request对象的方法-LMLPHP

post提交与get提交的运行结果相同。

4、对用户提交的数据的同键不同值的处理

(1)获取提交数据的值:

 <form action="http://localhost:8080/MyServlet_war_exploded/abc" method="post">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="book" value="xiangzi">《骆驼祥子》<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="book" value="xiyou">《西游记》<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="book" value="shuihu">《水浒传》<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="book" value="hongloumemg">《红楼梦》<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="book" value="sanguo">《三国演义》<br>
<input type="reset" value="取消">
<input type="submit" value="确定">
</form>

request对象的方法-LMLPHP

request对象的方法-LMLPHP

  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">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="book1" value="xiangzi">《骆驼祥子》<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="book2" value="xiyou">《西游记》<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="book3" value="shuihu">《水浒传》<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="checkbox" name="book4" value="hongloumemg">《红楼梦》<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<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());
}
}

request对象的方法-LMLPHP

request对象的方法-LMLPHP

(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);
}
}
}

request对象的方法-LMLPHP

request对象的方法-LMLPHP

05-11 13:15