Servlet
76. What is the Servlet lifecycle?
A: servlet has a good life cycle definition, including loading and instantiation, initialization, request processing, and service termination. The lifetime is expressed by the init, service, and destroy methods of the javax. servlet. Servlet interface. After the Servlet is instantiated by the server, the container runs its init method, runs its service method when the request arrives, and the service method automatically dispatches the doXXX method (doGet, doPost) corresponding to the request, etc, 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 multi-thread mode. An instance can serve multiple requests, and its instance is generally not destroyed, CGI generates a new process for each request. After the service is completed, it is destroyed, so the efficiency is lower than that of servlet.
77. 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 address after the redirection is not displayed in the address bar of the client browser. The latter is a complete redirection, And the browser will get the redirection address, and resend the request link. In this way, the link address after the jump is displayed in the address bar of the browser. 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 the actual link. In some cases, for example, to jump to a resource on another server, you must use the sendRedirect () method.
78. Basic Servlet Architecture
A:
Public class ServletName extends HttpServlet {
Public void doPost (HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
}
Public void doGet (HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
}
}
79. Under what circumstances 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 ().
80. servlet Lifecycle
A: When a web container loads servlet, the lifecycle begins. Call the servlet init () method to initialize the servlet. You can call the service () method to call different do *** () methods based on different requests. End the service. The web Container calls the servlet's destroy () method.
81. How to Implement servlet single-thread mode
A: <% @ page isThreadSafe = "false" %>
82. Methods for transferring objects between pages
A: request, session, application, cookie, etc.
83. 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 compilation, it is "servlet-like ". The main difference between Servlet and JSP is that the application logic of Servlet is in the Java file and is completely separated from the HTML in 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.
84. Four session tracking technologies
Answer: Description of the ServletsJSP page in the session Scope
Page No indicates the objects and attributes related to a page. A page is represented by a compiled Java servlet class (which can contain any include command but has no include action. This includes both servlet and JSP pages compiled into servlet.
A request is an object and attribute related to a request sent by a Web Client. A request may span multiple pages and involves multiple Web Components (due to the relationship between the forward instruction and the include action)
A session is an object and attribute associated with a user experience for a Web Client. A Web session can also frequently span requests from multiple clients.
Application represents the objects and attributes related to the entire Web application. This is essentially a global scope that spans the entire Web application, including multiple pages, requests, and sessions
85. Main Methods of the Request object
A:
SetAttribute (String name, Object): Set the parameter value of the request whose name is name.
Getattribute (string name): returns the property value specified by name.
Getattributenames (): returns the name set of all attributes of the request object. The result is an enumerated instance.
Getcookies (): return all cookie objects on the client. The result is a cookie array.
Getcharacterencoding (): returns the character encoding method in the request.
Getcontentlength (): returns the length of the Request body.
Getheader (string name): Get the file header defined by HTTP
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 enumerated instance.
Getinputstream (): returns the input stream of the request to obtain data in the request.
Getmethod (): method for obtaining data transmitted from the client to the server
Getparameter (string name): Get the parameter value specified by name sent 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): obtains all values of the parameter specified by name.
Getprotocol (): gets the protocol name on which the client transmits data to the server.
Getquerystring (): Get the query string
Getrequesturi (): obtains the client address of the request string.
Getremoteaddr (): obtains the IP address of the client.
Getremotehost (): Get the client name
Getsession ([Boolean create]): returns the session associated with the request.
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): deletes an attribute in a request.
86. 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;
}
87. 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 ()