Differences and connections between JSP serverlet and jspserverlet

Source: Internet
Author: User

Differences and connections between JSP serverlet and jspserverlet

Jsp is html containing java

Servlet is java contains html

Jsp requests to tomcat --- tomcat encapsulates jsp to servlet implementation.

Therefore, a session is automatically created during a jsp request.

Instead of requesting. getSession (true) in servlet)

Servlet is a server-side program that dynamically generates html pages and sends them to the client.

There are many out. println () in the program, and java and html languages are messy. So

Later, jsp was launched. In fact, jsp is a servlet. Every jsp is

Convert it into a servlet File and compile it into. class for running.

With jsp, the servlet is no longer responsible for generating html pages in the MVC mode.

The role of the control program logic controls the flow between jsp and javabean.

-----------------------------------------------------------------

Differences between Servlet and Jsp

* Servlet does not contain built-in objects. Originally, all the built-in objects in Jsp must pass the HttpServletRequest object, or

HttpServletResponse object generation.

* For static HTML tags, the Servlet must use the webpage output stream to output the response line.

In short, Jsp is a simplification of Servlet. To use Jsp, you only need to complete the content that the programmer needs to output to the client.

How to embed Java scripts into a class is completed by the Jsp Container. Servlet is a complete Java class, the Service of this class

Method is used to generate a response to the client.

-Servlet Development

Servlet is usually called a server-side Applet and is used to process and respond to client requests.

Servlet is a special Java class, which must inherit from HttpServlet. Each Servlet can respond to client requests.

Servlet provides different methods to respond to client requests.

* DoGet: used to respond to get requests from the client.
* DoPost: used to respond to the post request of the client
* DoPut: used to respond to the put request of the client
* DoDelete: used to respond to the delete request of the client
* Service (HttpServletRequest request, [can respond to all types of requests on the client]

HttpServletResponse response)

Throws ServletException, java. io. IOException

Other methods

* Init (Servletconfig config): the initialization method called when a Servlet instance is created.

* Destory (): The resource recycling method automatically called when the Servlet instance is destroyed.

-Servlet Configuration

The compiled Servlet Source file does not respond to user requests. It must also be compiled into a class file. Compile the. class

Files are placed in the WEB-INF/classes path, if the Servlet has a package, you should also put the class file under the corresponding package path.

To enable the Servlet to respond to user requests, you must configure the Servlet in the Web application. Modify the Servlet Configuration

Web. xml file

Configuring Servlet requires two parts:

* Configure the Servlet name: corresponding to the <servlet/> element in web. xml

* Configure the Servlet URL: corresponding to the <servlet-mapping/> element in web. xml

-Servlet Lifecycle

When a Servlet runs in a container, creation and destruction of its instances are not determined by the programmer, but by the container.

Servlet creation has two options:

* Create a Servlet instance when the client requests the corresponding Servlet: Most servlets use this Servlet

* When a Web application is started, the Servlet instance load-on-startup Servlet is created immediately.

Servlet lifecycle:

Begin
---> Instance created
---> Initialize [init]
---> Respond to the client request [doGet, doPost, service]
---> Destroyed [destroy] --->
End

-Use Servlet as the Controller

The workload of using Servlet as the presentation layer is too large. All HTML tags must be generated using the page output stream.

Therefore, using Servlet as the presentation layer has the following three disadvantages:

* Low development efficiency. All HTML tags must use the page output stream.

* It is not conducive to team collaboration, and the artist cannot participate in Servlet interface development.

* The program has poor maintainability. Even if you modify the title of a button, you need to re-edit the Java code and re-compile the code.

Use the MVC Architecture of Servlet:

Jsp page --> Servlet program --> Servlet forwards a new Jsp page according to the condition

-Load-on-startup Servlet

The time for Servlet instantiation is also when the Web application is started, that is, the load-on-startup Servlet

The Servlet started when the application is started, usually used for some background services, or the Servlet that intercepts many requests;

This type of Servlet is usually used as the basic Servlet and provides important background services.

Configuration in web. xml:

Add an element to the <servlet/> element

<Load-on-startup> 1 </load-on-startup>

-Access Servlet configuration parameters

When configuring the Servlet, you can also add additional configuration parameters. By using configuration parameters, you can achieve better decoupling,

Avoid writing all parameters in a program in hard encoding mode.

To access Servlet configuration parameters, you must use the instance of the ServletConfig class. ServletConfig provides the following methods:

Java. lang. String getInitParameter (java. lang. String name) [obtain initialization parameters]

The Difference and connection between JSP serverlet is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support the help house.

Related Article

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.