Tomcat中Request对象的常用方法
- Requset对象可以通过对象获取
请求行
,请求头
,请求体
中的所有信息
package com.shanlei;
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.util.Enumeration;
public class RequestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Post请求");
this.doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("get请求");
System.out.println(request.getMethod());
StringBuffer requestURL = request.getRequestURL();
System.out.println(requestURL);
System.out.println(request.getRequestURI());
System.out.println(request.getScheme());
System.out.println(request.getHeader("User-Agent"));
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()){
String key = headerNames.nextElement();
String value = request.getHeader(key);
System.out.println(key+":"+value);
}
String user = request.getParameter("user");
String pwd = request.getParameter("pwd");
System.out.println("user:"+user);
System.out.println("password:"+pwd);
Enumeration<String> parameterNames = request.getParameterNames();
while(parameterNames.hasMoreElements()){
String key = parameterNames.nextElement();
String value = request.getParameter(key);
System.out.println(key+":"+value);
}
String[] favs = request.getParameterValues("fav");
for (String fav : favs) {
System.out.println(fav);
}
String remoteAddr = request.getRemoteAddr();
String remoteHost = request.getRemoteHost();
int remotePort = request.getRemotePort();
String localAddr = request.getLocalAddr();
String localName = request.getLocalName();
int localPort = request.getLocalPort();
}
}