Servlets/JSP development technical Q &

Source: Internet
Author: User

 

<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> new document </title>
<Meta name = "generator" content = "editplus">
<Meta name = "author" content = "">
<Meta name = "keywords" content = "">
<Meta name = "Description" content = "">
</Head>

<Body>
Servlets filter is newly added to the servlet 2.3 specification. It intercepts the requests submitted by the user from the client and runs when the required resources are not reached. It operates requests from the client, intercepts the response before the resource is first sent to the client, and processes the response that has not yet been sent to the client.

Why does genericservlet add an Init () method based on Init (servletconfig config?
 
The init () method is called by the genericservlet. INIT (servletconfig config) method.

The init () method facilitates developers to customize servlet initialization without maintaining the storage of servletconfig objects.

To override genericservlet. INIT (servletconfig config), you must call the super. INIT (config) method.

What is the role of servletcontext. getcontect (Java. Lang. String uripath?

Returns the servletcontext object corresponding to the path specified in the same server. This object can be used to deal with other contexts in the server.

Uripath must start with "/" (This path is relative to the root path of the entire servlet document, rather than the root path of the current servletcontext ).

What is the servlet lifecycle?

The life cycle of a general servlet (genericservlet, that is, a servlet unrelated to the Protocol): 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 it necessary to rewrite the genericservlet. Service () method?

It is unnecessary for httpservlet. You only need to override its doxxxx () method. The Service () method in httpservlet automatically forwards requests to the corresponding doxxxx () method (for example, doget () method) based on the user request type ).

How to Use servletrequest. getreader () and servletrequest. getinputstream?

Note that the two methods cannot be used at the same time.

The servletrequest. getrealpath (string path) method is no longer recommended.

Use the servletcontext. getrealpath (string path) method.

What is the default charset of servletresponse?

The default character set (charset) of servletresponse is the ISO-8859-1, which can be changed through the setcontenttype (Java. Lang. String) method.

Example: setcontenttype ("text/html; charset = shift_jis ").

For more information about character sets, see RFC 2045.

What is the difference between httpservletrequest. getrequesturi () and httpservletrequest. getrequesturl?
 
The returned value of request. getrequesturi () is similar to/xuejava/requestdemo. jsp.

The returned value of request. getrequesturl () is similar to http: // localhost: 8080/xuejava/requestdemo. jsp.

What is the difference between httpservletrequest. encodeurl () and httpservletrequest. encoderedirecturl? Why are there two different methods?

When you use the URL-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.

Before calling httpservletresponse. sendredirect, call the encoderedirecturl () method. Otherwise, sesssion information may be lost. ...

How can we implement the single thread model for your servlet or JSP?

Implement the javax. Single. singlethreadmodel interface for servlet.

For JSP, write the following statement in page ctictive:

JSP tag and jsp xml-based tag

...

How to define a JSP page as an error page? Why?

Implementation Method:

Why? Because the exception object needs to be obtained (by default, "implicit object" exception cannot be directly used in JSP pages ).

What is the execution sequence of JSP pages?

The execution sequence of JSP pages is as follows:

JSP page translation. jsp page --> Servlet Source code.
JSP page compilation. Servlet Source code --> servlet class.
Load class (first time or the server restarted)

Create instance (may be repeated, if the JSP page is clear)

Call jspinit method (this method is not rewritten for common JSP pages. Rewrite must be in the declaration statement segment ).

Call _ jspservice method (similar to the doget and dopost methods of httpservlet, but can be used to process both post and getq requests ).

Call jspdestroy method (when the server uninstalls the Servet, for example, when the servlet has not been used for a long time ).

What are the implicit objects in JSP pages )? What are their respective types and functions?

Request --
Reponse --
Session --
Application --
Out --
Page --
Pagecontext --
Exception -- it is valid only when the current JSP page is error page.
Config --

What is the difference from <@ include file = "/Foo. jsp" %>?

<@ Include... -- page translation time.

What are the running Modes of servlets/JSP Container (ENGINE?

Standalone
Tomcat standalone Mode
In-process
Tomcat running inside Apache Web server.
Out-of-Process
Apache + mod_jk + Tomcat

What is the relationship between servlet, Servlet developer, Servlet API, and 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 message

The following is an example of response:

HTTP/1.0 200 OK
Date: Tue, 01 Dec 2001 23:59:59 GMT
Content-Type: text/html
Content-Length: 52

 

Hello, John!

 

What methods are defined in the HTTP 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.

According to the HTTP protocol specification, requests and response also have these essential content!

Therefore, you need to first understand the concept of HTTP message and the format of its content. These things 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.

What is the difference between requestdispatcher. Forward () and httpservletresponse. sendredirect?

Requestdispatcher. Forward () is run on the server; httpservletresponse. sendredirect () is done by sending a command to the client browser.

Therefore, requestdispatcher. Forward () is "Transparent" for the browser, while httpservletresponse. sendredirect () is not.

In addition, please note that when requestdispatcher. Forward () is called, response cannot be commit (response. iscommitted ()).

What is the difference between servletcontext. getrequestdispatcher (string URL) and servletrequest. getrequestdispatcher (string URL? Why?

The URLs in servletcontext. getrequestdispatcher (string URL) can only use absolute paths, while those in servletrequest. getrequestdispatcher (string URL) can use relative paths.

Because servletrequest has the concept of relative paths, while servletcontext does not.

How do I transfer requests to an address in another web app?

Servletcontext. getrequestdispatcher (string URL) and servletrequest. getrequestdispatcher (string URL) can only transfer requests to addresses in the same web app.

To transfer requests to an address in another web app, follow these steps:

1. Obtain the servletconext object (currentservletcontext. getcontext (uripath) of another web app )).

2. Call the servletcontext. getrequestdispatcher (string URL) method.

</Body>
</Html>

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.