JBuilder2005 servlet Development Essentials

Source: Internet
Author: User
Tags filter define event listener http post http request implement variables thread
The servlet can create protocols, platform-independent web applications with a servlet, applets run in the browser's JRE, and the servlet runs in the servlet container of the Web application server, and the servlet does not have a user graphical interface. Servlet and the Web application server's servlet container interact to receive requests to return responses. The request was first processed by the Web application server's servlet container and passed to Servlet,servlet to return the response to the client via the Web application server. A client program can use any language development that can send a request to a Web application server.

The biggest advantage of the servlet is its high performance, the servlet uses a very different way of running with CGI, first the servlet loads and resides in memory at the first initialization, and then runs directly from memory, and second, the servlet works in single instance multithreading by default , when a new request arrives, the servlet instance opens the request for a new thread service.

   Servlet Architecture and thread safety

All servlet implementations Javax.servlet.Servlet interfaces directly or indirectly, which specify how the servlet communicates with the servlet container, and also define the lifecycle of the servlet. Genericservlet is a protocol-independent universal Servlet,httpservlet that is a servlet,web application developed specifically for HTTP protocols that directly inherits HttpServlet. The inheritance system of its class is shown in the following illustration:


Figure 1 The class inheritance system of the servlet

The Javax.servlet.Servlet interface includes 3 ways to control the servlet lifecycle, respectively:

init (servletconfig config) method

When the servlet initializes, the init () method is called to perform the work of initializing the servlet, and the Init () method is invoked only once. When the servlet is initialized, it enters a ready state, ready to respond to client requests.

Service (ServletRequest req, Servletresponse resp) method

The servlet container invokes the service () method to process the request and return the response. ServletRequest and Servletresponse are passed as incoming parameters to the service (), ServletRequest encapsulates the requested information and Servletresponse encapsulates the response information.

Destroy () method

The servlet container can uninstall the servlet at any time, at which point Destory () is invoked, where you can release the resources that the servlet occupies.

The classes in the Javax.servlet.http package are used to support HTTP protocols and create HTML Web pages. The HTTP protocol is based on the request/response mode of operation, and these HTTP requests include:

Get

POST

Put

DELETE

Head

TRACE

CONNECT

OPTIONS

Javax.servlet.http.HttpServlet defines methods for multiple service HTTP protocols, which are named Doxxx () style naming and HTTP request name echoes: HTTP GET requests correspond to doget (), while HTTP Post corresponds to Dopost () and so on. HttpServlet initially responds to client requests with service (HttpServletRequest req, HttpServletResponse resp) and invokes the corresponding doxxx () method, based on HTTP requests.

Generally, you only need to cover the doget () or Dopost () method, and if you want more control, you can override the Doput () and Dodelete () methods, and other methods are rarely used. If you use the JBuilder servlet Wizard, you can specify which methods to create.

It is particularly necessary to point out that the servlet works in a multi-threaded manner, and that the servlet can handle multiple requests at the same time. As a developer, you need to be aware of the thread safety of the servlet member variables, the local domain variables in doget (), doPost () are thread safe, and the member variables of the servlet are the hidden dangers of thread security. So unless you want to apply this feature in general, it is not appropriate to define some of the variables that can be rewritten as member variables of the servlet, otherwise you must take the thread synchronization measures to ensure thread safety.

   features and applications of servlet

Although the servlet can also be used to generate dynamic Web pages, but this feature has gradually given way to the new sharp JSP, but the servlet is not because of the talent of the history of the Java Museum as an antique, it still has the ability to laugh proudly:

• Auto Start

In general, the JSP is compiled and initialized only after the first call by the client, and the servlet can be initialized automatically when the Web container starts by using the Web.xml <load-on-startup> configuration. You can use the servlet feature to complete the initialization of a Web application, such as downloading a dictionary table, controlling a table, initializing configuration information, and so on, and starting a background process.

• Path matching mapping

The servlet can respond to multiple matching URIs by using the Web.xml <servlet-mapping> wildcard configuration URI mapping, and the JSP can only be invoked through a specific URI. This feature allows you to intercept and process the request before entering a specific page, and many Web application frameworks, such as struts and spring, take advantage of this feature of the servlet to create the architecture on this basis.

· Servlet Filter

The servlet filter inherits from Javax.servlet.Servlet and implements the Javax.servlet.Filter class, which is processed before the request reaches the service program and before the response is sent to the client. If you have a large number of pages that require the same processing, you can use a servlet filter to handle this together. If you can use the servlet filter for encoding conversion, or for each page to add a unified header first.

· Servlet Listener

Java EE defines multiple Web event listener interfaces, which are classes that inherit Javax.servlet.Servlet and implement these event listener interfaces. If the Web application server is interested in an event for a Web container, you can construct a servlet that implements the listener interface for that event to handle the event when it occurs. The flexibility to use servlet listeners makes it easy to do something that is hard to implement.

In addition, a servlet is more appropriate than a JSP if a Dynamic Web page exhibits less logic and more business-processing logic, such as a request to compute PI and return a result page. So to create a dynamic Web page, in the end to use the servlet or JSP issues on the indecisive, please remember the following classic words: a servlet is a Java program that contains HTML code, and a JSP is an HTML Web page that contains Java 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.