Java EE基础-request对象中方法含义
web.xml配置:
<servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>TestServlet</servlet-name> <servlet-class>com.nubb.servlet.TestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>TestServlet</servlet-name> <url-pattern>/servlet/TestServlet.do</url-pattern> </servlet-mapping>
浏览器首次发送请求后经过Http协议在服务器端Servlet执行顺序:构造方法->init方法->service方法->doGet方法(若是Post请求则先经过doPost方法,这是传下来的习惯)->destory方法(此方法是在服务器关闭时才会调用)
在此请求时只是经过service、doGet方法。
访问url:localhost:8080/qq/servlet/TestServlet.do?flag=true&tag=qq
TestServlet源码:
package com.nubb.servlet; import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TestServlet extends HttpServlet { private static final Logger log = LoggerFactory.getLogger(TestServlet.class); /** * */ private static final long serialVersionUID = 3995193730201556140L; /** * Constructor of the object. */ public TestServlet() { super(); log.info("TestServlet"); System.out.println("TestServlet"); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log log.info("destory"); System.out.println("destroy"); } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { log.info("doGet"); System.out.println("doGet"); request.setCharacterEncoding("UTF-8"); System.out.println("getContextPath: "+request.getContextPath()); //应用名称 /qq System.out.println("getAuthType: "+request.getAuthType()); //认证类型 null System.out.println("getCharacterEncoding: "+request.getCharacterEncoding()); //得到HTTP请求字符编码,若没指定返回null UTF-8 System.out.println("getContentType: "+request.getContentType()); //HTTP请求类型 application/x-www-form-urlencoded Enumeration enume = request.getHeaderNames(); //遍历 System.out.println("HeaderNames"); while(enume.hasMoreElements()){ System.out.print(enume.nextElement().toString()+":"); //host:connection:content-length:cache-control:accept:origin:user-agent:content-type:referer:accept-encoding:accept-language:accept-charset:cookie } System.out.println("getHeaders(Referer): "+request.getHeader("Referer")); //getHeaders(Referer): http://localhost:8080/qq/ System.out.println("getPathInfo: "+request.getPathInfo()); //servlet映射之后的路径 null System.out.println("getLocalPort: "+request.getLocalPort()); //访问端口 8080 System.out.println("getMethod: "+request.getMethod()); //获取请求方式。doPost POST System.out.println("getPathTranslated: "+request.getPathTranslated()); //暂不明了。。。 null System.out.println("getQueryString: "+request.getQueryString()); //url所带参数 flag=login&tag=qq System.out.println("getRemoteUser: "+request.getRemoteUser()); //单点登录验证用户是否登录 null System.out.println("getRequestedSessionId: "+request.getRequestedSessionId()); //请求被记录在Session A267E0ACCC3A85F76AF6ECEC0A8EAE4D System.out.println("getRequestURI: "+request.getRequestURI()); //应用请求路径 /qq/servlet/TestServlet.do(注意斜杠“/”) System.out.println("getRequestURL: "+request.getRequestURL().toString()); //完整路径 http://localhost:8080/qq/servlet/TestServlet.do System.out.println("getServletPath: "+request.getServletPath()); //servlet请求路径 /servlet/TestServlet.do System.out.println("getSession: "+request.getSession()); //获取Session如果没有则创建一个新的Session org.apache.catalina.session.StandardSessionFacade@6b3fc7 System.out.println("getSession(boolean): "+request.getSession(true)); //获取Session如果没有若参数为true则创建新的Sesion org.apache.catalina.session.StandardSessionFacade@6b3fc7 System.out.println("getSession(boolean): "+request.getSession(false)); //获取Session如果没有若参数为true则创建新的Sesion org.apache.catalina.session.StandardSessionFacade@6b3fc7 System.out.println("getUserPrincipal: "+request.getUserPrincipal()); //得到认证用户 null System.out.println("isUserInRole: "+request.isUserInRole("maomao")); //判断role用户是否存在 false } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { log.info("doPost"); System.out.println("doPost"); this.doGet(request, response); } @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("service"); super.service(req, resp); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here log.info("init"); System.out.println("init"); } }
写博客不仅是一种习惯,更是一种沉淀。