HttpServletRequest对象
HttpServletRequest对象代表客户端浏览器的请求,当客户端浏览器通过HTTP协议访问服务器时,HTTP请求中的所有信息都会被Tomcat所解析并封装在这个对象中,通过这个对象提供的方法,可以获得客户端请求的所有信息。
获取请求信息
req.getRequestURL()
返回客户端浏览器发出请求时的完整URL。
req.getRequestURI()
返回请求行中指定资源部分。
req.getRemoteAddr()
返回发出请求的客户机的IP地址。
req.getLocalAddr()
返回WEB服务器的IP地址。
req.getLocalPort()
返回WEB服务器处理Http协议的连接器所监听的端口。
java
package com.java;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class GetRequestInfoServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求的完整url
String RequestURL = req.getRequestURL().toString();
//获取请求的URI
String requestURI = req.getRequestURI();
//获取发出请求的客户机的IP地址。
String clientAddr = req.getRemoteAddr();
//获取服务端所在的IP地址
String serverAddr = req.getLocalAddr();
//获取WEB服务器处理Http协议的连接器所监听的端口。
int localPort = req.getLocalPort();
PrintWriter printWriter = resp.getWriter();
printWriter.println("URL:"+RequestURL);
printWriter.println("URI:"+requestURI);
printWriter.println("clientAddr:"+clientAddr);
printWriter.println("serverAddr:"+serverAddr);
printWriter.println("localPort:"+localPort);
printWriter.flush();
printWriter.close();
}
}
xml
<servlet>
<servlet-name>getRequestInfoServlet</servlet-name>
<servlet-class>com.java.GetRequestInfoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getRequestInfoServlet</servlet-name>
<url-pattern>/getRequestInfo.do</url-pattern>
</servlet-mapping>
获取请求表单数据
根据key获取指定value
req.getParameter("key");
根据key获取对应的value,返回一个字符串。
java
String str = req.getParameter("key");
GetRequestDataServlet.java
java
package com.java;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class GetRequestDataServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取用户名
String userName = req.getParameter("userName");
//获取用户名密码
String userPwd = req.getParameter("userPwd");
PrintWriter pw = resp.getWriter();
pw.println("userName:"+userName);
pw.println("userPwd:"+userPwd);
pw.flush();
pw.close();
}
}
addUser.html
html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="getRequestData.do" method="post">
<input type="text" name="userName"/></br>
<input type="password" name="userPwd"/></br>
<input type="submit" value="OK"/>
</form>
</body>
</html>
web.xml
xml
<servlet>
<servlet-name>getRequestDataServlet</servlet-name>
<servlet-class>com.java.GetRequestDataServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getRequestDataServlet</servlet-name>
<url-pattern>/getRequestData.do</url-pattern>
</servlet-mapping>
获取复选框(checkbox组件)中的值
req.getParameterValues("checkboxkey");
获取复选框(checkbox组件)中的值,返回一个字符串数组。
java
String[] userlikes = req.getParameterValues("checkboxkey");
GetRequestDataServlet.java
java
public class GetRequestDataServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取用户名
String userName = req.getParameter("userName");
//获取用户名密码
String userPwd = req.getParameter("userPwd");
//获取复选框数据
String[] arr = req.getParameterValues("userLike");
List<String> list = Arrays.asList(arr);
PrintWriter pw = resp.getWriter();
pw.println("userName: "+userName);
pw.println("userPwd: "+userPwd);
pw.println("userLike: "+list);
pw.flush();
pw.close();
}
}
addUser.html
html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="getRequestData.do" method="post">
用户名:<input type="text" name="userName"/></br>
密 码:<input type="password" name="userPwd"/></br>
爱好:<input type="checkbox" name="userLike" value="sport"/>体育
<input type="checkbox" name="userLike" value="music"/>音乐
<input type="checkbox" name="userLike" value="Art"/>艺术<br/>
<input type="submit" value="OK"/>
</form>
</body>
</html>
获取所有提交数据的key
req.getParameterNames()
获取请求中所有数据的key,该方法返回一个枚举类型。
java
Enumeration<String> parameterNames = req.getParameterNames();
java
//获取表单中所有的Key值
Enumeration<String> parameterNames = req.getParameterNames();
List<String> paraList =new ArrayList<>();
while (parameterNames.hasMoreElements()){
paraList.add(parameterNames.nextElement());
}
java
pw.println("names: "+paraList);
使用Map结构获取提交数据
req.getParameterMap()
获取请求中所有的数据并存放到一个Map结构中,该方法返回一个Map,其中key为String类型value为String[]类型。
java
Map<String, String[]> parameterMap = req.getParameterMap()
java
//获取请求中所有的数据
Map<String, String[]> parameterMap = req.getParameterMap();
Iterator<Map.Entry<String, String[]>> iterator = parameterMap.entrySet().iterator();
java
while (iterator.hasNext()){
Map.Entry<String, String[]> entry = iterator.next();
String key = entry.getKey();
String[] value = entry.getValue();
pw.println(key + " = " + Arrays.asList(value));
}