HttpServletRequest对象
HttpServletRequest对象代表客户端浏览器的请求,当客户端浏览器通过HTTP协议访问服务器时,HTTP请求中的所有信息都会被Tomcat所解析并封装在这个对象中,通过这个对象提供的方法,可以获得客户端请求的所有信息。
获取请求信息
req.getRequestURL()
返回客户端浏览器发出请求时的完整URL。
req.getRequestURI()
返回请求行中指定资源部分。
req.getRemoteAddr()
返回发出请求的客户机的IP地址。
req.getLocalAddr()
返回WEB服务器的IP地址。
req.getLocalPort()
返回WEB服务器处理Http协议的连接器所监听的端口。
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 {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//获取请求的完整urlString RequestURL = req.getRequestURL().toString();//获取请求的URIString 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();}
}
<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,返回一个字符串。
String str = req.getParameter("key");
GetRequestDataServlet.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 {@Overrideprotected 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
<!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
<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组件)中的值,返回一个字符串数组。
String[] userlikes = req.getParameterValues("checkboxkey");
GetRequestDataServlet.java
public class GetRequestDataServlet extends HttpServlet {@Overrideprotected 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
<!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,该方法返回一个枚举类型。
Enumeration<String> parameterNames = req.getParameterNames();
//获取表单中所有的Key值Enumeration<String> parameterNames = req.getParameterNames();List<String> paraList =new ArrayList<>();while (parameterNames.hasMoreElements()){paraList.add(parameterNames.nextElement());}
pw.println("names: "+paraList);
使用Map结构获取提交数据
req.getParameterMap()
获取请求中所有的数据并存放到一个Map结构中,该方法返回一个Map,其中key为String类型value为String[]类型。
Map<String, String[]> parameterMap = req.getParameterMap()
//获取请求中所有的数据Map<String, String[]> parameterMap = req.getParameterMap();Iterator<Map.Entry<String, String[]>> iterator = parameterMap.entrySet().iterator();
while (iterator.hasNext()){Map.Entry<String, String[]> entry = iterator.next();String key = entry.getKey();String[] value = entry.getValue();pw.println(key + " = " + Arrays.asList(value));}