Request对象:获得消息请求
Response对象:响应消息请求
一.Request方法:
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/mingming") public class ServletDemo1 extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.获取请求的方式 getMethod(); String method = request.getMethod(); System.out.println(method); //2.获得目录的路径 getContextPath(); String contextPath = request.getContextPath(); System.out.println(contextPath); //3.获取servlet路径 getServletPath(); String servletPath = request.getServletPath(); System.out.println(servletPath); //4.获得get请求参数 getQyeryString(); String queryString = request.getQueryString(); System.out.println(queryString); //5.获得请求的url getRequestURl();/getRequestURL();第二种输出不完整 String requestURI = request.getRequestURI(); System.out.println(requestURI); //6.获得协议的版本 getProtocol(); String protocol = request.getProtocol(); System.out.println(protocol); //7.获得客户的IP getRemoteAddr() String remoteAddr = request.getRemoteAddr(); System.out.println(remoteAddr); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
二.其他方法
获取请求参数的通用方式 不论get还是post 请求的方式都可以使用下列方法获取参数
1 getParameter(String name);
2 根据参数的名称获得参数的数组 getParameterValues(String name)
3 getParameterNames() 获得所有的请求参数的名称
4 getParameterMap() 获得所有参数的集合 map