JSP Servlet Learning Notes

Source: Internet
Author: User

When interviewing a Java programmer, you are often asked about the knowledge in the JSP servlet. This knowledge is the basis of the JSP servlet, this article mainly comb the JSP servlet comparison base but more important knowledge.


1 Service method Invocation procedure

There is a service method in the Servlet interface that is not manually mobilized by someone, but is automatically called by Tomcat at run time. The Genericservlet abstract class implements the Servlet interface, and of course it overrides the service method, but it is not implemented specifically. In the HttpServlet class, the service method has a concrete implementation that invokes the protected service method of the class. The following are specific:


2 The life cycle of the servlet

The most important : there is only one Servlet object throughout the servlet's service to the client. When a user accesses at the same time, it is done in a multithreaded manner, and the thread has a pool of threads.
The whole life process is as follows:
A, load ClassLoader
B, instantiation of New (the first time the client comes in and is instantiated, the entire life process, only instantiated once)
C, initialize init (servletconfig) (the entire life process, only once instantiated)
d, processing request service (), Doget (), DoPost () (performed in multithreaded manner)
E, Exit service Destroy () (this method will be called when the Web application exits, reloads, and reloads automatically)
The concrete examples are as follows:

The results of the operation are as follows:


3 Why there are two init methods

Two init methods are defined in the Genericservlet class:

First look at Genericservlet source code. In the Genericservlet class, the source code for init (servletconfig config) is:


So, if there are no two init methods, we programmers can rewrite the init (servletconfig config) method, but if the programmer forgets to write This.config = config Word, there will be an error. So, the programmer simply rewrites the Init method without parameters, does not need to care about the init (servletconfig config) method, the system will automatically call the init (ServletConfig config) method method, The method is called to the user-defined init () method.


4 built-in objects for JSPs

The built-in object of JSP can be directly used in the JSP code without the need to create a new built -in object, such as an out object, it is a built-in object JSP, in addition to the request,response, session, application, Cookie, config, page, exception , etc.


5 dynamic inclusion and static inclusion of JSPs

Static inclusions

Statically contained syntax:<%@ include ...%>

Static contains the hungry function: it means to include another Web page for easy reuse.

Characteristics of static Inclusions:

A, the included page is included before compiling.

b, it converts the result is converted to a class file. (Dynamic inclusion is two Class)

C, This contains can not be passed parameters , for example: code <%@include file= "titlebar.jsp? User=liguohui"%> No, the parameters passed here User=liguohui is meaningless because it does not make sense to pass parameters during compilation.

D, contains the page and the included page access is the same request object , either tilebar.jsp or testbar.jsp page can call "Request.getparamter ();" function, and they call the same object and get the same result.

E. The character set in the containing page and the contained page must be the same , for example, if the character set defined in the TILEBAR.JSP code is: <% @page contenttype= "TEXT/HTML;CHARSET=GBK"% >; the character set defined in the TESTBAR.JSP code is: <% @pagecontentType = "text/html;charset=gb2312"%>, then an error occurs. (But this is only a feature of Tomcat, not the common feature of all containers).

F, there is no need to write .

Concrete examples of static inclusions:



Dynamic inclusion

Dynamically included syntax:<jsp:include page= "date.jsp"/>

Concrete examples of static inclusions:

The result of the operation is:

There is also a huge difference between dynamic and static inclusions:when statically included, only one class file will be compiled in the background, and the dynamic inclusion will compile two class files in the background.



6 Sendredirect and forward implement page jump

Sendredirect

The Sendredirect method can be used to move the page to another place, such as the following code, you can move the current page to the Forforward1.jsp page, but also to pass a parameter Name=liguohui.

When you enter the address in the Address bar: http://127.0.0.1/my/forward/test.jsp? Name=zhumingyun&oldname=hui, at the same time to the page to pass two parameters, You will find the following results:

The visible Name=zhumingyun&oldname=hui parameter is discarded, and the parameter Name=liguohui inside the sendredirect is preserved.
The background print out a word, visible, when the Sendredirect command issued, the following words continue to execute.


Forward

Forward is a dynamic steering with the following code:

Client input address: http://127.0.0.1/my/forward/forward.jsp? Name=liguohui, and pass a parameter Name=liguohui as shown: first, its address bar has not changed Second, it can take two pages of parameters, and the name of the parameter, the back of the previous overlay



The differences between Sendredirect and forward are summarized as follows (table part from Beijing Shang Academy tutorial video)



7 garbled problem in JSP

The solution is roughly as follows:



Complete the full text. Please specify the source of the transfer.





JSP Servlet Learning Notes

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.