(1 ),Why?GenericservletInInit (servletconfig config)AddedInit ()Method?
Init ()Method is genericServlet. INIT (ServletConfig config) method call.
Init ()This method facilitates customization by developers.ServletWithout maintenance.ServletConfig object storage.
Override genericServlet. INIT (ServletThe super. INIT (config) method must be called.Servletcontext. getcontect (Java. Lang. stringuripath)What is the role?
Returns the sameServerCorresponding to the path specified inServletContext object, which can be usedServerOther context in.
Uripath must start with "/" (the path is relative to the entireServletThe root path of the document, instead of the currentServletContext ).
(2 ),ServletWhat is lifecycle?
AverageServlet(GenericServletIs not related to the Protocol.Servlet) Lifecycle:Init ()-->Genericservlet.Service(ServletrequestReq,ServletresponseRes) -->Destroy.
HTTPServletLifecycle:Init ()--> GenericServlet. Service (ServletRequest req,ServletResponseres) ---> Service (HTTPServletRequestreq, HTTPServletResponse resp) --> doxxxx () -->Destroy.
(3)Is there any need to rewriteGenericservlet. Service ()Method?
For HTTPServletIt is unnecessary. You only need to override its doxxxx () method. HTTPServletMediumService ()The method automatically forwards the request to the corresponding doxxxx () method (for example, the doget () method) based on the user request type ).
ServletRequest. getreader () andServletHow to use request. getinputstream?
Note that the two methods cannot be used at the same time.
ServletThe request. getrealpath (stringpath) method is no longer recommended.
Please useServletContext. getrealpath (stringpath) method.
(4 ),ServletresponseDefault Character Set(Charset)What is?
ServletThe default character set (charset) of response is the ISO-8859-1, which can be changed using the setcontenttype (Java. Lang. String) method.
Example: setcontenttype ("text/html; charset = shift_jis ").
(5 ),Httpservletrequest. getrequesturi ()AndHttpservletrequest. getrequesturl ()What is the difference??
The returned value of request. getrequesturi () is similar to/xuejava/requestdemo. jsp.
Request. getrequestURL() Return values are similar to: http: // localhost: 8080/xuejava/requestdemo. jsp
HTTPServletRequest. encodeURL() And HTTPServletRequest. encoderedirectURL() What is the difference? Why are there two different methods?
When usingURL-When you use the Rewriting Method to manage sessions, you need to use the above two methods.
The difference between the two methods is that the logic of the two methods to determine whether to include the session ID is different.
When you call HTTPServletBefore Response. sendredirect, call encoderedirectURL() Method. Otherwise, sesssion information may be lost. ...
(6 ),How to make yourServletOrJSPImplementationSinglethread model?
ForServletImplementationJavax. Single. singlethreadmodel interface.
ForJSP, In the pageDirective<% @ Page isthreadsafe = "false" %>
JSPTag andJSP XML-Basedtag
(7 ),HowJSP pageDefinedError Page?Why?
Implementation Method: <% @ page iserrorpage = "true" %>
Why? Because the exception object needs to be obtained (by defaultJSPPage cannot directly use "implicit object" exception ).
(8 ),JSP pageWhat is the execution order?
JSPThe page execution sequence is as follows:
JSPPagetranslation.JSPPage -->ServletSource code.
JSPPagecompilation.ServletSource code -->ServletClass.
Load class (first time or the server restarted)
Create instance (it may be many times, ifJSP<% @ Page isthreadsafe = "false" %>)
Call jspinit method (GeneralJSPPage does not override this method. Rewrite must be in the declaration statement segment ).
Call _ jspservice method (similar to HTTPServletBut can be used to process both post and getq requests ).
Call jspdestroy method (ServerWhen detaching A Servet, for example, whenServlet).
(9 ),JSP pageHidden objects(Implicity object )?What are their respective types and functions?
Request --
Reponse --
Session --
Application --
Out --
Page --
Pagecontext --
Exception -- only when the currentJSPIt is valid only when page is error page.
Config --
(10 ),<JSP: includepage = "/Foo. jsp" %>And<@ Include file = "/Foo. jsp" %>What is the difference?
<JSP: Include... -- request time.
<@ Include... -- page translation time.
(11 ),Servlets/jspcontainer (engine)How many running modes are available?
Standalone
TomcatStandalone Mode
In-process
TomcatRunninginside ApacheWeb Server.
Out-of-Process
Apache + mod_jk +Tomcat
(12 ),Servlet,ServletDevelopers,Servlet API,ServletWhat is the container relationship?
Servlet,ServletDeveloper --->Servlet API-->ServletContainer
The parts ofHTTPMessage
Message part description
The initial line: Specifies the purpose of the request or response message
Example: Get/reports/sales/index.htmlHTTP/1.0
The header section: Specifies the meta-information, such as size, type, andEncoding,
About the content of the message
A blank line:
An optional message body: the main content ofthe request or response message
The following is an example of response:
HTTP/1.0 200ok
Date: Tue, 01 Dec 2001 23:59:59 GMT
Content-Type: text/html
Content-Length: 52
<HTML>
<Body>
<H1> hello, John! </H1>
</Body>
</Html>
(13 ),HTTPWhat methods are defined in the specification? What are their respective purposes?
Get
Head
Post
The following methods have been added since the HTTP 1.1 Standard:
Put
Options
Trace
Delete
Connect
In servetrequest, why is the getcontenttype () and getcontentlength () methods required.
AccordingHTTPProtocol specifications. Request and response also have these essential content!
Therefore, you must first understandHTTPThe concept of message and its content format are the same for request and reponse.
For a GET request, the content type is null.
For post requests, the content type is application/X-WWW-form-URLEncoded
The content of the POST request is similar to: username = xuejava.
Requestdispatcher.Forward ()And HTTPServletWhat is the difference between response. sendredirect?
Requestdispatcher.Forward ()Is running on the server; httpServletResponse. sendredirect () is done by sending a command to the client browser.
So requestdispatcher.Forward ()The browser is "Transparent", whereas HTTPServletResponse. sendredirect () is not.
In addition, pay attention to requestdispatcher.Forward ()At the time of calling, response cannot be commit (response. iscommitted ()).
ServletContext. getrequestdispatcher (stringURL) AndServletRequest. getrequestdispatcher (stringURL) What is the difference? Why?
ServletContext. getrequestdispatcher (stringURL).URLOnly absolute paths can be used.ServletRequest. getrequestdispatcher (stringURL).URLYou can use relative paths.
BecauseServletRequest has the concept of relative path.ServletThe context object has no sub-concept.
(14 ),How to transfer requests to anotherWeb app?
ServletContext. getrequestdispatcher (stringURL) AndServletRequest. getrequestdispatcher (stringURL) Only requests can be transferred to the sameWebThe address in the app.
If you need to transfer the request to anotherWebFor an address in the app, follow these steps:
- Obtain anotherWebAppServletConext object (currentServletContext. getcontext (uripath )).
- CallServletContext. getrequestdispatcher (stringURL) Method.