Jsp/servlet Foundation

Source: Internet
Author: User

If there is nothing to do today, revisit the knowledge of the servlet to make a note of it. At the beginning of contact with the Java EE, also did not spend too much time to this piece, followed by the teaching video, now a little forgotten, but this piece is the interview examiner like to ask the place, to investigate the basic skills.

Servlet

Servle is a special Java class that has no main method and cannot run on its own, that is, it needs other containers or tools to start. The servlet is run on the client to process the request and make the appropriate program. Simply put, if you want to use a Dynamic Web resource (that is, develop a Java program to output data to the browser), you need to complete the following 2 steps:
1. Write a Java class, inherit HTTPS Ervle T .
2. Deploy the well-developed Java class to the Web server.

3. Modify Web. Xml To configure the mapping address for access to the modified Serlvet.

Servlet life cycle

The concept of life cycle, how to say, the following is a personal understanding, it is important but in development it seems to use is not particularly much, and now the framework so much, do not have to devote attention to this piece. However, as a basic familiarity, the follow-up will also have to learn andriod also need life cycle. The servlet life cycle, a simple generalization consists of four links: servlet class loading-----"instantiation-----" service-----"destroyed.


1. Loading and instantiation

< Span style= "White-space:pre" > when the servlet container starts, Reads the information in the Web. XML configuration file, constructs the specified Servlet object, creates a ServletConfig object, and invokes the ServletConfig object as a parameter to invoke the Init method of the Servlet object; code is easier to understand, init () Method and method, init ()

<span style= "Font-family:times New Roman;" > @Overridepublic void init () throws Servletexception {System.out.println ("====== init without parameter ======"); Super.init ();} @Overridepublic void init (servletconfig config) throws servletexception {System.out.println ("====== init with parameter ====== "); Super.init (config);} </span>

The output is:

====== init with parameter ======

====== init without parameter ======

====== Service Method ======

Viewing the source code is not difficult to find, with the parameter of the init (ServletConfig config) method is finally shown to call the init () method without parameters, in the init method, we can construct some logic code to initialize the Servlet before it is instantiated. How exactly is it loaded?

Load mode

Configure the servlet in the project's Web. Xml as follows:<servlet> and <servlet-mapping> need to be paired, the former mainly defines the servlet naming and the code package path,< Load-on-startup> the meaning of the label is summarized,

If not configured <>load-on-start> the servlet is instantiated only when called; in three cases:

1, loadonstartup < 0
In the case of negative numbers, the Web container does not instantiate when it is started, and is instantiated when the servlet is called for the first time. This situation is the same as not setting loadonstartup.
2, Loadonstartup > 0
When the Web container is started, it is instantiated, the order is small to large, and the positive integers are instantiated first.
3, Loadonstartup = 0
When the Web container is started, it is instantiated, which is equivalent to the largest integer, so when the Web container starts, it is instantiated at the end of the process.

The mapping <url-pattern> is used to do path mapping; The/test/in the code below, the access path is: http://localhost:8080/project naming/test, To access the servlet, in the same way that the package,action name is defined in Struts.xml in struts2 and accessed in the same manner.

<span style= "Font-family:times New Roman;" ><servlet><servlet-name>demoServlet</servlet-name><servlet-class> com.niuxl.servlet.demoservlet</servlet-class><load-on-startup>1</load-on-startup></ Servlet><servlet-mapping><servlet-name>demoservlet</servlet-name><url-pattern>/test </url-pattern></servlet-mapping></span>

2: Enter the service

When a request is submitted, the servlet invokes the service () method for processing, often by invoking the doget () or Dopost () method on the request type, and in the Service () method, Servlets can accept customer requests through servletrequest, or they can use Servletresponse to set up response information.

3: Destruction

The Destroy () method is automatically called when the Web container shuts down or detects that a servlet is being removed from the container, so that the instance frees up the resources it consumes.

4: Uninstall

When a servlet finishes calling the Destroy () method, the secondary instance waits to be reclaimed by the garbage collector, and the init () method initialization is called again if the servlet needs to be reused again.

Note: In the life cycle, the init () and service () methods are called only once, and the entire process has only one Servlet object, in the case of Singleton mode. In general, when we write servlets, we only need to make doget () and Dopost ().


JSP (Java Server Page)

JSP is a technique for developing dynamic web resources, with the greatest emphasis on nesting Java code in JSP pages.

The JSP nature is also a servlet, and the runtime needs to rely on other containers. JSP is the extension of the servlet, so we can see that the JSP is mainly composed of two parts:
static parts: standard HTML tags, static page content, and the same content as static HTML pages.
Dynamic Parts: Java program-controlled content, which is generated dynamically by Java programs.

When a JSP file is requested for the first time, the JSP engine (itself a servlet) will first convert the JSP file into a Java source file. In the conversion process, if the JSP file is found to have syntax errors, the conversion process will be interrupted, and the server and the client output error messages;

In the MVC architecture pattern, in the case of JSPs and Servlets, a controller is typically used by a servlet, and view is typically played by a JSP.

The working principle of JSP can be summarized briefly as follows:
1.JSP relies on web containers to run, such as the popular web container tomcat/weblogic/jboss and so on.
2.JSP must be edited into a servlet to be responded to by the server to the client.

Visitors to the 3.JSP page do not need to install any client, or even run the Java environment, because the JSP page is delivered to the client's standard HTML page.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Jsp/servlet Foundation

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.