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"); } }