Question collection: servlets/jsp Development technical questions and answers

Source: Internet
Author: User
Tags define object character set config implement include session id string
Js|servlet| problem Why did Genericservlet add an init () method on the basis of Init (ServletConfig config)?
  
The init () method is called by the Genericservlet.init (ServletConfig config) method.
  
The init () method facilitates the developer customizing the servlet initialization without having to maintain the ServletConfig object's storage work.
  
Override Genericservlet.init (ServletConfig config) the calling Super.init (config) method that must be displayed.
  
   What is the role of Servletcontext.getcontect (Java.lang.String uripath)?
  
Returns the ServletContext object for the path specified in the same server that enables you to interact with the other context in the server.
  
The Uripath must start with "/" (the path is meant to be relative to the root path of the entire servlet document, not the root path of the current ServletContext).
  
   What is the servlet lifecycle?
  
The lifecycle of a general servlet (Genericservlet, that is, a protocol-independent servlet): Init ()--> Genericservlet.service (ServletRequest req, Servletresponse res)--> destroy.
  
HttpServlet lifecycle: init ()--> Genericservlet.service (servletrequest req, servletresponse res)---> Service ( HttpServletRequest req, HttpServletResponse resp)--> doxxxx ()-->destroy.
  
   Is there a need to rewrite the Genericservlet.service () method?
  
Not necessary for HttpServlet. You just need to rewrite its doxxxx () method. The HttpServlet service () method automatically forwards the request to the appropriate doxxxx () method (such as the Doget () method) based on the type of user request.
  
How to use Servletrequest.getreader () and Servletrequest.getinputstream ()?
  
Note that two methods cannot be used at the same time.
  
The Servletrequest.getrealpath (String path) method has not been recommended for use.
  
Please use the Servletcontext.getrealpath (String path) method.
  
   Servletresponse What is the default character set (CharSet)?
  
Servletresponse the default character set (CharSet) is iso-8859-1, and the new character set can be changed by setContentType (java.lang.String) method.
  
For example: setContentType ("text/html; Charset=shift_jis ").
  
For information about character sets, you can browse RFC 2045
  
   What is the difference between Httpservletrequest.getrequesturi () and Httpservletrequest.getrequesturl ()?
  

Request.getrequesturi () returns a similar value:/xuejava/requestdemo.jsp
  
Request.getrequesturl () returns a similar value: http://localhost:8080/xuejava/requestdemo.jsp
  
Httpservletrequest.encodeurl () and Httpservletrequest.encoderedirecturl () What is the difference? Why should there be two different methods?
  
When you use the url-rewriting approach to manage sessions, you need to use the above two methods.
  
The difference between the two methods is that two methods determine whether the logic that contains the session ID needs to be different.
  
You should call the Encoderedirecturl () method before calling Httpservletresponse.sendredirect, otherwise you may lose sesssion information. ...
  
   How do you enable your servlet or JSP to implement single Thread Model?
  
Implement the Javax.single.SingleThreadModel interface for the servlet.
  
For JSP, write the following statement in Page directive <%@ page isthreadsafe= "false"%>
  
JSP tag and JSP xml-based tag
  
...
  
   How do you define a JSP page as an error page?
  
Implementation method: <%@ page iserrorpage= "true"%>
  
Why? Because you need to get the exception object (by default, the implied object exception cannot be used directly in the JSP page).
  
   What is the order of execution of the JSP page?
  
The JSP page is executed in the following order:
  
JSP Page translation. JSP Page--> Servlet source code.
JSP Page Compilation. Servlet Source code--> servlet class.
Load Class (the restarted of the A or the server)
  
Create instance (may be many times, if <%@ page isthreadsafe= "false"%> is declared in the JSP page)
  
Call Jspinit methods (the normal JSP page does not override this method, the override needs to be in the Declaration statement section).
  
Call _jspservice method (similar to the Doget and Dopost methods of general HttpServlet, but can be used to process both post and GETQ requests).
  
Call Jspdestroy method (when the server unloads Servet, for example, when the servlet is not in use for a long time).
  
   What are the implied objects (Implicity object) in the JSP page? What are their respective types and functions?
  
Request--
Reponse--
Session--
Application--
Out--
Page--
PageContext--
Exception-valid only if the current JSP page is the error page.
Config--
  
   What is the difference between the <jsp:include page= "/foo/foo.jsp"%> and <@ include file= "/foo/foo.jsp"%>?
  

<jsp:include ...-Request time.
<@ include ...-Page translation time.
  
   servlets/jsp Container (Engine) There are several ways to run it?
  
Standalone
Tomcat Standalone Mode
In-process
Tomcat running inside Apache Web Server.
Out-of-process
Apache + mod_jk + Tomcat
  
What is the relationship between the Servlet,servlet developer, servlet API, servlet container?
  
Servlet,servlet Developer--->servlet API--> Servlet Container
The parts of an HTTP message
  
Message Part Description
The initial line:specifies the purpose of the request or response message
Example: get/reports/sales/index.html http/1.0
The header section:specifies the meta-information, such as size, type, and encoding,
About the content of the message
A Blank line:
An optional message body:the main content of the request or response
  
Here is an example of a response:
  
http/1.0 OK
Date:tue, Dec 2001 23:59:59 GMT
Content-type:text/html
content-length:52
  
<body>
</body>
  
   What methods are defined in the HTTP specification? What are their respective uses?
  
Get
Head
POST
Starting with the Http 1.1 specification, the following methods are added:
  
Put
OPTIONS
TRACE
DELETE
CONNECT
  
Servetrequest Why to define: getContentType (), Getcontentlength () method.
  
According to the HTTP protocol specification, Request and response have these essential content as well!
  
So you need to first understand the concept of HTTP message and the format of its content, which is the same for request and reponse.
  
The content type of the request sent to the Get mode is: null
  
The content type of the request sent by post is: application/x-www-form-urlencoded
  
The Post method sends a request similar to the following: Username=xuejava.
  
What is the difference between Requestdispatcher.forward () and Httpservletresponse.sendredirect ()?
  
Requestdispatcher.forward () is run on the server side, Httpservletresponse.sendredirect () is done by sending commands to the client browser.
  
So Requestdispatcher.forward () is "Transparent" to browsers, while Httpservletresponse.sendredirect () is not.
  
In addition, note that Requestdispatcher.forward () Response cannot have been commit (response.iscommitted ()) at the time of the call.
  
What is the difference between a servletcontext.getrequestdispatcher (string url) and a servletrequest.getrequestdispatcher (string url)? Why?
  
The URL in servletcontext.getrequestdispatcher (string URL) can only use absolute paths; servletrequest.getrequestdispatcher (string URL) The URL in can use a relative path.
  
Because ServletRequest has the concept of relative path, and ServletContext object has no secondary concept.
  
   How do I transfer a request to an address in another Web app?
  
Servletcontext.getrequestdispatcher (string url) and Servletrequest.getrequestdispatcher (string URL) can only transfer requests to the same web The address in the app.
  
If you need to transfer a request to an address in another Web app, you can do the following:
  
1. Get another web app's Servletconext object (Currentservletcontext.getcontext (Uripath)).
  
2. Invoke the Servletcontext.getrequestdispatcher (String url) method.

Related Article

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.