Knowledge points in servlet & JSP
1. What is the servlet lifecycle?
(1) servlet containers are responsible for loading and instantiating servlet A: servlet has a good life-time definition, including loading and instantiating, initializing, processing requests, and service termination. This survival period is caused
The init, service, and destroy methods of the javax. servlet. servlet interface. After the servlet is instantiated by the server, the container runs
The init method is used to run the service method when the request arrives. The service method automatically dispatches the doxxx method (doget
The destroy method is called when the server decides to destroy the instance.
The difference with CGI is that the servlet is in a server process and runs its service method in multiple threads. An instance can serve
Multiple requests, and the instance will not be destroyed. cgi generates a new process for each request, and the service will be destroyed after completion, so the efficiency is high.
Lower than servlet.
2. What is the difference between forward () and redirect () in Java Servlet APIs? A: the former is only the redirection of control in the container, and the redirection address is not displayed in the address bar of the client browser; the latter is a full hop.
The browser will get the jump address and resend the request link. In this way, you can see the link after the jump from the address bar of the browser.
Connection address. Therefore, the former is more efficient. When the former can meet the needs, try to use the forward () method, and this will also help to hide
Hide the actual link. In some cases, for example, to jump to a resource on another server, you must use sendredirect ()
Method.
3. Basic servlet architecture answer: public class servletname extends httpservlet {public void dopost (httpservletrequest request, response) throws servletexception, ioexception {} public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {}}
4. When do I call doget () and dopost ()? A: The method attribute in the form label in the JSP page is doget () when get is called, and dopost () When post is called ().
5. servlet lifecycle answer: when a Web container loads a servlet, the lifecycle begins. Call the servlet Init () method to initialize the servlet. Call
Use the Service () method to call different do *** () methods based on different requests. The Web Container calls the Servlet
Destroy () method.
6. How to Implement servlet single-thread mode A: <% @ page isthreadsafe = "false" %>
7. Methods for transferring objects between pages answer: request, session, application, Cookie, etc.
8. What are the similarities and differences between JSP and Servlet, and what are their relationships? A: JSP is an extension of Servlet technology. It is essentially a simple servlet method, with more emphasis on the external expression of the application. After JSP is compiled, it is a "Class
Servlet ". The main difference between Servlet and JSP is that the application logic of servlet is in the Java file and completely from the presentation layer.
. In JSP, Java and HTML can be combined into a file with the extension. jsp. JSP focuses on views,
Servlet is mainly used for control logic.
9. Four session tracking techniques answer: Session scope servletsjsp page description 1. Page No indicates objects and attributes related to a page. A page is composed of a compiled Java Servlet class (can contain any
The include command, but there is no include action. This includes both Servlet and
JSP page; 2. Request is the object and attribute related to a request sent by the Web Client. A request may span multiple pages, involving
Multiple Web Components (due to the relationship between the Forward Command and the include action); 3. session represents an object and attribute related to a user experience for a Web Client. A web session can also be
It usually spans multiple client requests. 4. Application represents the objects and attributes related to the entire web application. This essentially spans the entire web application,
Includes a global scope of pages, requests, and sessions.
10. main method of request object answer: setattribute (string name, object): Set the parameter value of the request whose name is name to getattribute (string name ): getattributenames (): returns the name set of all properties of the request object. The result is an enumerated instance getcookies (): returns all cookie objects of the client, the result is a cookie array getcharacterencoding (): returns the character encoding method getcontentlength () in the request: returns the length of the Request body getheader (string name ): get the header information defined by the HTTP protocol getheaders (string name): returns all the values of the request header with the specified name. The result is an enumerated instance getheadernames (): returns the name of the Request Header, the result is an enumerative instance getinputstream (): returns the input stream of the request, which is used to obtain the data in the request getmethod (): getparameter (string name) method for obtaining the data transmitted from the client to the server ): getparameternames (): obtains the names of all parameters sent from the client to the server. The result is an enumerated instance getparametervalues (string name ): getprotocol (): gets the protocol name getquerystring () used by the client to send data to the server. getrequesturi (): Get the query string getrequesturi (): get the client address getremoteaddr () sending the request string: Get the Client IP address getremotehost (): Get the client name getsession ([Boolean create]): return and request related session getservername (): get the server name getservletpath (): Get the path of the script file requested by the client getserverport (): Get the server port number removeattribute (string name): delete an attribute in the request
11, we often encounter the output of some encoding characters in the Web application development process, such as iso8859-1, how to output a certain Encoding
String? A: Public String translate (string Str) {string tempstr = ""; try {tempstr = new string (Str. getbytes ("ISO-8859-1"), "GBK"); tempstr = tempstr. trim ();} catch (exception e) {system. err. println (E. getmessage ();} return tempstr ;}
12. Which of the following methods are generally implemented during servlet execution? A: Public void Init (servletconfig config) Public servletconfig getservletconfig () Public String getservletinfo () Public void Service (servletrequest request, servletresponse response) Public void destroy ()