Welcome to “Algorithms and the Beauty of Programming” ↑ pay attention to us!

This article was first published on the wechat official account “Beauty of Algorithms and Programming”. Welcome to follow and learn more about this series of blogs in time.

1. Why use servlets?

Answer: Servlets are a good substitute for Common Gateway Interface (CGI) scripts. Usually CGI scripts are written in Perl or C, and they are always specific to a particular server platform. Servlets, on the other hand, are written in Java, so they are platform independent from the start. In this way, the promise that Java can be written once to run on any platform (writeOnce, Run Anywhere) can also be fulfilled on the server. Servlets also have some unique advantages over CGI scripts:

(1) Servlets are persistent. Servlets need only be loaded once by the Web server and can hold the service between requests (such as a database connection). In contrast, CGI scripts are transient and transient. Each request for a CGI script causes the Web server to load and execute the script. Once the CGI script is finished running, it is purged from memory and the result returned to the client. Each use of a CGI script results in repeated execution of program initialization procedures, such as connecting to a database.

(2) Servlets are platform independent. As mentioned earlier, servlets are written in Java and naturally inherit the platform independence of Java.

(3) Servlets are extensible. Because servlets are written in Java, they have all the benefits that Java can offer. Java is a robust, object-oriented programming language that is easily extensible to suit your needs. Servlets naturally have these characteristics as well.

(4) Servlets are safe. The only way to call a servlet from the outside world is through a Web server. This provides a high level of security, especially if your Web server is protected by a firewall.

(5) Setvlet can be used on a variety of clients. Because servlets are written in Java, you can easily use them in HTML just as you would with applets.

 

2. Lifecycle of servlets?

Answer: A Servlet is a component that can run in a Servlet container, so of course there should be a process from creation to destruction, which we can call the Servlet life cycle. The life cycle of a Servlet can be divided into five stages: load, instantiation, initialization, processing customer requests, and unloading. The methods are mainly init (), service (), and destroy ().

The life cycle is specified as follows:

The Servlet container does the job of loading the Servlet class and instantiating a Servlet object

The init () method does the initialization, which is invoked by the Servlet container

The service () method processes the client request and returns the result of the response

The destroy () method is called before the Servlet container unloads the Servlet, freeing some resources

3. Tell the difference between servlets and CGI?

The difference with CGI is that servlets are in a server process. They run their Service method in a multi-threaded manner. An instance can serve multiple requests and instances are generally not destroyed, whereas CGI creates a new process for each request and destroys the service after completion, so it is less efficient than servlets.

 

4. What are the main methods in the HttpServlet class? What are their roles?

Answer: The main HttpServlet methods are doGet, doPost, doPut, doDelete, doTrace, etc

The specific functions of the following methods are mainly understood as follows:

(1) Void doGet (it request, HttpServletResponseresponse) throws ServletException, IOException is invoked by the servlet engine to handle an HTTP GET request. Input parameters, HTTP headers, and input streams are available from the output streams of the Request object, response header, and Response object.

(2) the Void doPost (it request, HttpServletResponseresponse) throws ServletException, IOException is invoked by the servlet engine to process an HTTP POST request. Input parameters, HTTP headers, and input streams are available from the output streams of the Request object, response header, and Response object.

(3) the Void doPut (it request, HttpServletResponseresponse) throws ServletException, IOException is called by the servlet engine to handle an HTTP PUT request. In this method, the URI is requested to indicate the location of the file being loaded.

(4) Void doDelete (it request, HttpServletResponseresponse) throws ServletException, IOException is invoked by the servlet engine to process an HTTPDELETE request. Request THE URI to indicate that the resource was deleted.

(5) the Void doOptions (it request, HttpServletResponseresponse) throws ServletException, IOException is called by the servlet engine to handle an HTTPOPTIONS request. Returns an Allow response header indicating the HTTP methods supported by this servlet. A servlet does not need to override this method because the HttpServlet method already implements the functionality required by the specification.

(6) Void doTrace (it request, HttpServletResponseresponse) throws ServletException, IOException is invoked by the servlet engine to process an HTTP TRACE request. Causes the request header to be fed back as a response flag. A servlet does not need to override this method because the HttpServlet method already implements the functionality required by the HTTP specification.

(7) Void service (it request, HttpServletResponseresponse) throws ServletException, IOException Service(Requestrequest, Response Response) an immediate method called with a specified HTTP request and Response. This method actually directs requests to doGet(), doPost(), and so on. This method should not be overridden.

Void service(Request Request, Response Response)throwsServletException, IOException puts the Request and Response object into its specified HTTP subclass, And calls the service() method specifying HTTP.

 

5. Introduce javax.servlet. servlet interface and its main methods.

Answer: The primary role of the Servlet interface is to provide the init(), service(), and destroy() methods for the Servlet lifecycle.

The main methods in the Servlet interface are:

ThrowsServletException is called by the servlet engine once after the servlet is loaded and before the service is implemented. If init() generates an overflow UnavailableException, the servle exits the service.

(2) ServletConfiggetServletConfig () returns to the servlet init () method’s ServletConfig object

Void service(ServletRequest Request, ServletResponse Response)throws ServletException, IOException processes the request described in the Request object. Use the Response object to return the request result

(4)StringgetServletInfo() returns a string describing the servlet

(5) VoiddeStory () is called by the servlet engine when a servlet is about to be uninstalled to destroy the servlet instance.

 

6. Describe the difference, common ground and scope of application between JSP and Servlet.

Answer: JSPS are essentially servlets, but they are not created the same way. Servlet is JAVA program code composition is good at flow control and transaction processing and through the Servlet to generate dynamic web pages; JSP is composed of HTML code and JSP tag, which can easily write dynamic web pages. Therefore, in the actual application, Servlet is used to control the business process, and JSP is used to generate dynamic web pages. In the Struts framework, JSPS are in the view layer of the MVC design pattern, and servlets are in the control layer. JSP is an extension of Servlet technology, essentially a simple way of Servlet. JSP is compiled to be servlet-like. The main difference between servlets and JSPS is that the application logic of servlets is in Java files and completely separated from the HTML in the presentation layer. In the case of JSP, Java and HTML can be combined into a file with a.jsp extension. JSPS focus on views, while servlets focus on controlling logic.

7. What are the main functions and functions of Java servlets?

Answer: Servlets extend the capabilities of servers by creating a framework to provide request and response services on the Web. When the client sends a request to the server, the server can send the request information to the Servlet and have the Servlet set up the response that the server sends back to the client. The Servlet can be loaded automatically when the Web server is started or the client requests the service for the first time. After loading, the Servlet continues to run until another client requests it. Servlets have a wide range of capabilities. For example, servlets can do the following:

(1) Create and return a complete HTML page containing dynamic content based on the nature of the customer request.

(2) Create a portion of the HTML page (HTML fragment) that can be embedded into the existing HTML page.

(3) Communicate with other server resources, including databases and Java-based applications.

(4) Use multiple clients to process connections, receive input from multiple clients, and broadcast the results to multiple clients. For example, servlets do

It is a multi-participant game server.

(5) Open a new connection from the server to the applet in the browser when data is allowed to be transmitted in a single connection mode, and connect the connection

The connection remains open. Applets can also initiate a connection between the client’s browser and the server, while allowing the client and server to simply and efficiently execute the session. You can communicate through custom protocols or standards, such as IIOP.

(6) Use MIME type to filter data for special processing, such as image conversion and server-side including (SSI).

(7) A standard routine that provides custom processing to all servers. For example, servlets can modify how users are authenticated.

\

More interesting articles:


Sichuan Tourism Institute Where2Go team



Wechat: The beauty of algorithms and programming

Long press to identify the QR code to follow us!

Tips: Click on the lower right corner of the page “Write a message” to comment, looking forward to your participation! Welcome to forward!