Learn more about the SSH framework (Spring MVC +spring framework +hibernate +spring Security) "One: servlet principles" __ssh

Source: Internet
Author: User
It is a pleasure to share with you the latest learning results, which are mainly written around the classic SSH framework. If there is any mistake, please correct me.
Because of the Java EE that had been in contact for some time two years ago, but at that time will only some simple configuration, understand some basic things, when the junior found these are all fur, so want to learn the entire framework, thus write down this series of sharing experience, know it is more to know why.

Below into the text: 1.servlet is what.

First we go to the official website to see the details. Found Javax.servlet's package, we found in fact the servlet package inside is a number of interfaces to find the servlet core interface, which is defined in the definition of a servlet is a small Java programming that runs within a Web s Erver. Servlets receive and respond to requests from WEB clients, usually across HTTP, the hypertext Transfer. That means Protocol. T is a small program running on a Web server that receives and responds to requests from Web clients and communicates using HTTP. Here the Web server can have a lot of, such as the common Tomcat,jetty and so on. What is the 2.servlet container.

The servlet and servlet containers are interdependent, but mutually evolved, and servlet,jsp are run in the servlet container, and the servlet container is run on the Web server (tomcat,jetty). We can look at the Tomcat container model.


You can see that the Tomcat container is divided into four levels. The real management servlet is the context container, and we can actually find such a label in Tomcat/config/server.xml in the Tomcat directory

<context docbase= "D:\Tomcat 8.0\webapps\tomcattest" path= "/tomcattest" true "reloadable=" Org.eclipse.jst.j2ee.server:TomcatTest "/> <context docbase= D:\Tomcat 8.0\webapps\gittest" path= "/gittest" Reloadable= "true" source= "Org.eclipse.jst.j2ee.server:GitTest"/>

You can see that a servlet container can have multiple context, and it means there can be multiple applications, each with no interference. 3.Servlet container startup process

We can look at the Tomcat source has such a startup class, you can use Java to manually start a Tomcat instance, and configure the relevant information, and then add WebApp application, this series of work is quite complex, when the good thing we can not so troublesome, A series of tasks, directly within the IDE, can start the Tomcat,tomcat container and initialize the Web application according to the process. initialization of 4.WEB applications

The initialization studio for Web applications is implemented in Contextconfig's Configurestart return, Mainly used to parse web.xml. The portal of the Web program, Tomcat first finds Globalwebxml, exists in Cof.web.xml, then looks for hostwebxml, then looks for the application profile Web.xml, Saved in the Webxml object by parsing, and finally Tomcat sets the properties in the Webxml object to the context container, which includes the Servlet object, filter filter, listener, and so on, but the final return is not a Servlet object. It is a Servletwrapper object that has been packaged. This also explains why the Tomcat container model has four layers. 5. Create a servlet instance

When the context container is started, it reads conf/ Web.xml content, this is the root of all Web applications, which define a lot of default configuration items, in fact, when our project has only one index.jsp without any servlet, run up to direct access to index.jsp, This is because there is a word in the default configuration item

<!--================== Built in Servlet definitions ====================--> <servlet> <servlet-n Ame>default</servlet-name> <servlet-class>org.apache.catalina.servlets.defaultservlet</ servlet-class> <init-param> <param-name>debug</param-name> <param- value>0</param-value> </init-param> <init-param> <param-name>listin gs</param-name> <param-value>true</param-value> </init-param> <load -on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>jsp</servlet- Name> <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> <init-param&gt
            ; <param-name>fork</param-name> <param-value>false</param-value> </init-param > <inIt-param> <param-name>xpoweredBy</param-name> <param-value>false</param-va lue> </init-param> <load-on-startup>3</load-on-startup> </servlet>

There are two classes, Defaultservlet and Jspservlet, whose loading order is 1 and 3, which means that when Tomcat starts, they start, in fact, all two classes inherit HttpServlet, and the creation of the servlet instance is started by Wrapper.loadservlet, and first it gets servletclass, Then give it to Instancemanager to create an object based on Servletclass.class, and if the object is configured with Jsp-file, then the Servletclass object is Jspservlet above. Initialization of 6.servlet

There is a Initservlet method in Standardwrapper, which is to invoke the Servlet's Init method, The Standardwrappperfacade that wraps the Standardwrapper object is also passed to the servlet as the ServletConfig object. If the servlet is associated with a JSP file, then the initialization is Jspservlet, and then the request is emulated, the JSP is called, and the class is compiled and initialized.

The servlet initialization work is very complex and there are a lot of processes in between, and we can look at the source document carefully. analysis of 7.Request and response objects The servlet defines two objects, ServletRequest and Servletresponse objects, which are the parent classes of the HttpServletRequest and HttpServletRequest, respectively. But why the ServletRequest and Servletresponse objects that are passed in the context container can be transformed into

HttpServletRequest and HttpServletRequest. Explained as follows:

When Tomcat receives a request, The Org.apache.coyote.Request and org.apache.coyote.Response two classes are created to describe the request and the corresponding information, which will be distributed to subsequent threading after simple processing, so their objects are small enough to be reclaimed by the JVM, and subsequent threads will create the ORG.A Pache.catalina.connector.Request and Org.apache.catalina.connector.Response objects, both of which remain in the entire servlet container until they are passed to the target servlet, but the type that the target receives is Requestfacade and Responsefacade, these two are called Façade class, the equivalent of two lad, when the letter will come when the two lad is responsible for processing, the purpose is to encapsulate data. 8.Servlet is how the request is handled usually when we send a request like Http://localhost:8080/TomcatTest/login, How does the container find the Loginservlet class to handle it? In fact, Tomcat has a mapper class that is designed to hold the child container information in the container container in Tomcat, Before the Org.apache.catalina.connector.Request class goes into the container container, Mapper will be based on this request hostname, ContextPath this information to the Mappingdata attribute of the request, and finally to the corresponding servlet processor. So far, the servlet can do all the work for us, but today's web apps rarely deliver all the pages of the interaction directly to the servlet, but rather with a more efficient MVC framework that maps all requests to a servlet , and then to implement the service method, such as the SPRINGMVC to learn later. 9. Summary This study reviews and delves into the servlet, and by reading the source code, the official document gives us a deeper understanding of the servlet because the servlet is the foundation of all subsequent frameworks, The spring framework behind the servlet is also an empty shelf, and the basics are the most important.
10. Appendix about the test code in this chapter in my GitHub address source code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.