Interview JSP, servlet related knowledge-life cycle, differences, etc.

Source: Internet
Author: User
Tags html header http post

1. servlet Life cycle

The so-called life cycle refers to the entire process of how a servlet container creates a servlet instance, allocates its resources, invokes its methods, and destroys its instance.

Phase one: instantiate (that is, create a Servlet object, call the constructor)

Object instantiation occurs in the following two scenarios.

First case:

When the request arrives at the container, the container finds whether the Servlet object exists and if it does not, the instance is created.

Second case:

When the container is started, or when an application is newly deployed, it checks whether the servlet has a Load-on-starup configuration in Web. Xml. If so, the servlet instance is created.

The lower the Load-on-starup parameter value, the higher the priority (the minimum value is 0, the highest priority).

Phase two: initialize

Assign a resource to the servlet, call init (servletconfig config);

The Config object can be used to access the servlet's initialization parameters.

Initialization parameters are parameters that are configured with Init-param.

Init can override.

Phase three: Ready/Call

There is a request to reach the container, and the container invokes the Servlet object's service () method.

The service () method of HttpServlet calls the Doget () or Dopost () method according to the requested method. However, these two do methods, by default, throw an exception and require subclasses to override.

Phase IV: destruction

The container removes the Servlet objects that are no longer needed, based on its own algorithm.

The Destroy () method of the Servlet object is called before it is deleted.

The Destroy () method is used to release resources.

Throughout the servlet's lifecycle, Init,destroy executes only once, and the service method executes multiple times.

2. The difference between JSP and servlet

1.The JSP has been compiled into a servlet. (The essence of JSP is that SERVLET,JVM can only recognize Java classes, not the code of the JSP, the Web container compiles the JSP code into the Java class that the JVM can recognize)
2.JSP is better at displaying the page, and the servlet is better at logic control .
3.There are no built-in objects in the servlet , and the built-in objects in the JSP must be obtained by HttpServletRequest objects, HttpServletResponse objects, and HttpServlet objects.
JSP is a simplification of servlets, the use of JSP only need to complete the programmer needs to output to the client's content, JSP java script How to mosaic into a class, by the JSP container to complete. The servlet is a complete Java class, and the service method of this class is used to generate a response to the client.

3. The difference between get and post in HTTP

The

1, Get method passes the user's data through a URL request, connects the field names in the form with their contents, joins the paired strings , puts the URL of the program that the Action property refers to , such as [url] Http://www.mdm.com/test.asp?name=asd&password=sad[/url], the data will be displayed directly on the URL, as if the user clicked on a link; Post method via HTTP Post mechanism, which places the names of fields within the form and their contents in the HTML header (header), to the server side to be handled by the program referred to by the Action property, which reads and processes the data of the form through the standard input (stdin) method , The Post method has an area for storing parameters.


2, get method need to use Request.QueryString to get the value of the variable, and Post way through Request.Form to access the content of the submission


3, Get The amount of data transmitted by the method is very small , only text is passed. Generally limited to about 2 KB, but the execution efficiency is better than the Post method, and the  post way to pass a relatively large amount of data , you can transfer large text and binary data, such as files, it is waiting for the server to read the data, but there is a byte limit, This is to avoid malicious attacks on the server with a large amount of data, according to Microsoft, Microsoft to use Request.Form () can receive maximum data limit, IIS 4 KB bytes, IIS 5 KB bytes

Recommended: Unless you are sure that the data you submitted Can be submitted at one time, or try to use the Post method


4, Get to submit data, will bring security issues , such as a landing page, through the Get method to submit data, the user name and password will appear on the URL, If the page can be cached or someone else can access the client's machine, the user's account number and password can be obtained from the history, so the form submission is recommended using the Post method; The common problem with the form page submitted by the Post method is that if the page refreshes, a dialog box pops up

Recommendation: For security reasons, it is advisable to use Post to submit data

4. The difference between HTML and JSP

HTML (hypertext Markup Language) Text Markup Language, it is a static page, and JavaScript as the explanatory language, why is it interpreted language? Because, as long as you have a browser then it can be displayed normally, without the need for the specified compilation tool, just write the HTML tag in the TXT document OK.
JSP (Java Server page) to see this meaning is the Java service side of the page, so it is dynamic, it needs to be compiled after the JDK to send the content to the client to display, we all know that the Java file compiled will produce a class file, The final implementation is the class file, JSP is the same, it will be compiled into a class file! JSP not only to compile, it has to be translated, first the JSP translated into a servlet file, and then compiled into a class file. When the user accesses the JSP, it executes the class file and finally ...

1. The simplest difference is that HTML can be opened directly, JSP can only be published to Tomact and other servers to open.
2. The definition on the HTML page is a static page can be run directly, the JSP page is a dynamic page which needs to be converted into a servlet when it runs.
3. Their header is different, this is the head of the JSP "<%@ page language=" java "import=" java.util.* "pageencoding=" GBK "%>" in the table header has encoded format and poured into the package and so on.
4. It is also very good to distinguish between the use of <%%> in JSP can write Java code, and HTML does not have <%%>.

Thank you for your reading , if you have other knowledge or the wrong place, want to share a bit, learn from each other!

Interview JSP, servlet related knowledge-life cycle, differences, etc.

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.