Servlet summary ~ Interview

Source: Internet
Author: User

1. What is Servlet?
2. What is the role of Servlet?
3. servlet Lifecycle
4. How does the servlet process a request?
5. What is the difference between Servlet and JSP?
6. Cookie technology in Servlet
7. servlet Filters
8. listener in Servlet

 

1. What is Servlet?

Servlet is a Java program that is based on the HTTP protocol and runs on the server (such as Tomcat ),

 

Is a Java class written according to servlet specifications.

2. What is the role of Servlet?

It mainly processes client requests and sends the results to the client.

3. servlet lifecycle?

The servlet lifecycle is controlled by the servlet container. It can be divided into three stages: initialization, running, and destruction.

Initialization phase:

1. The servlet container loads the servlet class and reads data from the. Class file of the servlet class to the memory.

2. Then the servlet container creates a servletconfig object. The servletconfig object contains the servlet initialization configuration information.

3. Create a servlet object in the servlet container.

4. The servlet container calls the init method of the servlet object for initialization.

Running stage:

When the servlet container receives a request, the servlet container creates the servletrequest and servletresponse objects for this request.

 

Then, call the service method. And pass these two parameters to the service method. The service method obtains the requested

 

Information. And process the request. Then, the response result of this request is generated through the servletresponse object. Then destroy servletrequest and

 

Servletresponse object. Whether the request is submitted by post or get, the final request will be processed by the service method.

 

Destruction stage:

When a web application is terminated, the servlet container first calls the destrory method of the servlet object and then destroys the servlet object,

 

At the same time, the servletconfig object associated with the servlet object will be destroyed. We can release it in the implementation of the destroy method.

 

Resources used by the servlet, such as closing the database connection and closing the file input/output stream.

Note the following points:

In the servlet lifecycle, Servlet initialization and destruction only occur once, and the number of service method executions depends on the number of servlet clients

 

Number of client accesses

 

 

4. How does the servlet process a request?

When a user sends a request to a servlet, the servlet container creates a servletrequst and servletresponse object.

 

The user's request information is encapsulated in the servletrequst object, and the servlet container then encapsulates the servletrequst and servletresponse objects

 

Send the requested servlet to the user. The servlet writes the processed result in the servletresponse, and then the servlet container transmits the response result

 

To the user.

 

5. What is the difference between Servlet and JSP?

1. After JSP is compiled, it can be said that JSP is equivalent to servlet.

2. jsp is better at page (performance ). Servlet is better at logical editing. (Core differences ).

3. In actual applications, servlet is used to control business processes, while JSP is used to generate dynamic web pages. In the Struts framework,

 

JSP is located at the view layer of the MVC design mode, while servlet is located at the control layer.

 

6. What is the cookie technology in Servlet?

Cookies are a way for Web servers to store information on visitors' hard disks through browsers. They are developed by Netscape.

Benefits of cookie technology:

1. When the cookie validity period is not reached, the cookie enables users to access some websites they have browsed without entering their passwords and usernames.

2. Cookie allows the site to track the number of visits, the last visit time, and the path of visitors to the site.

Create a cookie

Java code
  1. // The two parameters are respectively the cookie name and cookie value.
  2. Response. addcookie (NewCookie ("ABC", "10000000 "));
[Java]
View plaincopy
  1. // The two parameters are respectively the cookie name and cookie value.
  2. Response. addcookie (new cookie ("ABC", "10000000 "));

 

Use cookie

Java code
  1. Cookie [] cook = request. getcookies (); // use a cookie array to receive
  2. For(IntJ = 0; j <cook. length; j ++) {// print cookie through Loop
  3. Cook [J]. getname (): // name of the cookie
  4. Cook [J]. getvalue (): // return the cookie value.
  5. }
[Java]
View plaincopy
  1. Cookie [] cook = request. getcookies (); // use a cookie array to receive
  2. For (Int J = 0; j <cook. length; j ++) {// print cookie through Loop
  3. Cook [J]. getname (): // name of the cookie
  4. Cook [J]. getvalue (): // return the cookie value.
  5. }

7. What are the filters in Servlet?

Main functions of filters

1. You must determine whether a user is logged on to any system or website.

2. The function of the online chat system or forum is to filter illegal texts.

3. Unified Solution code

(2) How to Create a filter:

 

1. generate a common class to implement the filter interface (javax. servlet. filter ;).

2. Rewrite the three methods in the interface: init, dofilter, and destroy.

3. Configure the filter in Web. xml.

8. listener in Servlet?

Listener: automatically performs some operations.

Three servlet listeners:

 

Request listening. Listen to the session. Listen to the application.

How to Create a session listener:

1. generate a common class. If it is a session listener, implement httpsessionlistener.

2. Then rewrite the five methods:

 

Java code
  1. Public VoidSessioncreated (httpsessionevent arg0) {}// create
  2. Public VoidSessiondestroyed (httpsessionevent arg0) {}// destroy
  3. Public VoidAttributeadded (httpsessionevent arg0) {} // Add
  4. Public VoidAttributeremoved (httpsessionevent arg0) {} // Delete
  5. Public VoidAttributereplaced (httpsessionevent arg0) {}// replace

1. What is Servlet?
2. What is the role of Servlet?
3. servlet Lifecycle
4. How does the servlet process a request?
5. What is the difference between Servlet and JSP?
6. Cookie technology in Servlet
7. servlet Filters
8. listener in Servlet

 

1. What is Servlet?

Servlet is a Java program that is based on the HTTP protocol and runs on the server (such as Tomcat ),

 

Is a Java class written according to servlet specifications.

2. What is the role of Servlet?

It mainly processes client requests and sends the results to the client.

3. servlet lifecycle?

The servlet lifecycle is controlled by the servlet container. It can be divided into three stages: initialization, running, and destruction.

Initialization phase:

1. The servlet container loads the servlet class and reads data from the. Class file of the servlet class to the memory.

2. Then the servlet container creates a servletconfig object. The servletconfig object contains the servlet initialization configuration information.

3. Create a servlet object in the servlet container.

4. The servlet container calls the init method of the servlet object for initialization.

Running stage:

When the servlet container receives a request, the servlet container creates the servletrequest and servletresponse objects for this request.

 

Then, call the service method. And pass these two parameters to the service method. The service method obtains the requested

 

Information. And process the request. Then, the response result of this request is generated through the servletresponse object. Then destroy servletrequest and

 

Servletresponse object. Whether the request is submitted by post or get, the final request will be processed by the service method.

 

Destruction stage:

When a web application is terminated, the servlet container first calls the destrory method of the servlet object and then destroys the servlet object,

 

At the same time, the servletconfig object associated with the servlet object will be destroyed. We can release it in the implementation of the destroy method.

 

Resources used by the servlet, such as closing the database connection and closing the file input/output stream.

Note the following points:

In the servlet lifecycle, Servlet initialization and destruction only occur once, and the number of service method executions depends on the number of servlet clients

 

Number of client accesses

 

 

4. How does the servlet process a request?

When a user sends a request to a servlet, the servlet container creates a servletrequst and servletresponse object.

 

The user's request information is encapsulated in the servletrequst object, and the servlet container then encapsulates the servletrequst and servletresponse objects

 

Send the requested servlet to the user. The servlet writes the processed result in the servletresponse, and then the servlet container transmits the response result

 

To the user.

 

5. What is the difference between Servlet and JSP?

1. After JSP is compiled, it can be said that JSP is equivalent to servlet.

2. jsp is better at page (performance ). Servlet is better at logical editing. (Core differences ).

3. In actual applications, servlet is used to control business processes, while JSP is used to generate dynamic web pages. In the Struts framework,

 

JSP is located at the view layer of the MVC design mode, while servlet is located at the control layer.

 

6. What is the cookie technology in Servlet?

Cookies are a way for Web servers to store information on visitors' hard disks through browsers. They are developed by Netscape.

Benefits of cookie technology:

1. When the cookie validity period is not reached, the cookie enables users to access some websites they have browsed without entering their passwords and usernames.

2. Cookie allows the site to track the number of visits, the last visit time, and the path of visitors to the site.

Create a cookie

Java code
  1. // The two parameters are respectively the cookie name and cookie value.
  2. Response. addcookie (NewCookie ("ABC", "10000000 "));
[Java]
View plaincopy
  1. // The two parameters are respectively the cookie name and cookie value.
  2. Response. addcookie (new cookie ("ABC", "10000000 "));

 

Use cookie

Java code
  1. Cookie [] cook = request. getcookies (); // use a cookie array to receive
  2. For(IntJ = 0; j <cook. length; j ++) {// print cookie through Loop
  3. Cook [J]. getname (): // name of the cookie
  4. Cook [J]. getvalue (): // return the cookie value.
  5. }
[Java]
View plaincopy
  1. Cookie [] cook = request. getcookies (); // use a cookie array to receive
  2. For (Int J = 0; j <cook. length; j ++) {// print cookie through Loop
  3. Cook [J]. getname (): // name of the cookie
  4. Cook [J]. getvalue (): // return the cookie value.
  5. }

7. What are the filters in Servlet?

Main functions of filters

1. You must determine whether a user is logged on to any system or website.

2. The function of the online chat system or forum is to filter illegal texts.

3. Unified Solution code

(2) How to Create a filter:

 

1. generate a common class to implement the filter interface (javax. servlet. filter ;).

2. Rewrite the three methods in the interface: init, dofilter, and destroy.

3. Configure the filter in Web. xml.

8. listener in Servlet?

Listener: automatically performs some operations.

Three servlet listeners:

 

Request listening. Listen to the session. Listen to the application.

How to Create a session listener:

1. generate a common class. If it is a session listener, implement httpsessionlistener.

2. Then rewrite the five methods:

 

Java code
  1. Public VoidSessioncreated (httpsessionevent arg0) {}// create
  2. Public VoidSessiondestroyed (httpsessionevent arg0) {}// destroy
  3. Public VoidAttributeadded (httpsessionevent arg0) {} // Add
  4. Public VoidAttributeremoved (httpsessionevent arg0) {} // Delete
  5. Public VoidAttributereplaced (httpsessionevent arg0) {}// replace

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.