Summary of Servet and JSP Learning

Source: Internet
Author: User

I went to the interview the day before yesterday and met a technical interviewer. I asked a lot of questions and did not answer them. Instead, I did not understand them well enough and did not summarize them. The most typical is the hierarchy of collection classes, the relationship between JSP built-in objects. These problems cannot be said, but are not solid enough. They do not stand at the height of a system to view these problems. Although the interview was unsuccessful, it was still quite rewarding. I saw that I was still not enough to learn many things. The interview mainly involves web development, using three frameworks: struts, spring, and hibernate. Over the past six months, we have been engaged in security and desktop applications.ProgramWithout web development, the main advantage of Java is web development. After the interview, I plan to take a good look at the three dozen frameworks. I believe in my ability to learn. Unfortunately, I did not show it in front of the interviewer. The following is what I wrote according to "deep understanding of Java Web development" compiled by Zhang Xiaoxiang.

Servelt consists of two packages: javax. servlet and javax. servelt.HTTP
The former is the basic Servet class, and the latter isHTTPRelated Classes. In the latter, most of the classes implement the former class. Many of the classes in the latter are directly used when writing servelt.
Genericservlet implements the servlet and servletconfig interfaces.
Httpservlet inherits the genericservlet class
We inherit httpservlet when writing servlet.

The servletconfig object can obtain servlet configuration parameters, such as the initialization parameters set in Web. XML, and the servletconfig object through servlet. getservletconfig.
The servletconfig object includes references to servletcontext.

Servletcontext is shared by all the servelts in the Web application. The serveltcontext object is obtained through getservletcontext. Its scope is the entire web application. Therefore, it can access resource files and use relative paths. You can obtain the local path mapped to the virtual path.

Httpservletresponse is a subinterface of servletresponse used to respond to requests. Used HTTP The Response Message object of the Protocol. You can generate response status lines, construct Response Message Headers (set character set encoding), and create response bodies.
How to add a cookie: httpservletresponse. addcookie (cookie ).
The following statement can be used to solve the servelt Chinese output problem:
Response. setcharacterencoding ("gb2312 ");
Response. setcontenttype ("text/html; gb2312 ");
Response. setlocale (New java. util. locale ("ZH", "cn "));
Allows the browser to regularly refresh the webpage or jump to another page
Response. setheader ("refresh", "2 ");
Response. setheader ("refresh", "2; url =HTTP : // Www.it315.org ");
Disable caching of the content of the current document.
Response. setdateheader ("expires", 0 );
Response. setheader ("cache-control", "No-Cache ");
Response. setheader ("Pragma", "No-Cache ");
Create response body: Response. getoutputstream () and getwriter ()
The servletoutputstream and printwriter objects are obtained respectively. One is a byte stream and the other is a bytes stream.
Request redirection and request forwarding:
Requestdispatcher. Forward () is used for request redirection, and httpservletresponse. sendredirect () is used for request forwarding (). Requestdispatcher is obtained through the getrequestdispatcher method in the servletcontext interface.
The difference is that redirection calls other servlets for processing and is transparent to users, while sendredirect () forwards requests to other pages. Redirection can only forward requests within resources in the same web application, while sendredirect () can forward requests between any pages.
Difference between requestdispatcher. indlude () and requestdispatcher. Forward ().
The indlude caller cannot change the status code and header of the Response Message. The same request object and response object are shared between callers and callers in the forward method.
The requestdispatcher. Forward and httpservletresponse. sendredirect methods do not support buffer output to the client until they are called. The buffer content is cleared.

Httpservletrequest is a sub-interface of servletrequest, which represents the request message.
Through this method, you can obtain information about the request line, network connection information, request header information, transmitted parameters, and so on.
Request line: for the following requests:
HTTP : // Localhost: 8080/it315/controller/One. jsp? Param1 = ABC
The following information can be obtained:
Request. getmethod (): Get
Request. getrequesturi ():/it315/controller/One. jsp
Request. getquerystring (): param1 = ABC
Request. getprotocol ():HTTP /1.1
Request. getcontextpath:/it315
Request. getpathinfo:/One. jsp
Request. getpathtranslated: C: \ tomcat-5.5.4 \ webapps \ it315 \ one. jsp
Request. getservletpath:/Controller
Network connections are IP addresses, ports, and so on.
Request Header Information: obtains the value of the header field of the specified name, the date, the Content-Type header field, and the character set encoding of the entity part of the request message.
Anti-leech technology:
Prevent the content from being directly Hyperlink and determine whether the Referer field in the Request Header comes from this site.
GET request parameter: getparameter (string)
GET request message entity content: corresponding to response, there is a method to get request message entity content. Request. getinputstream () and getreader () methods. Byte stream and byte stream respectively
Transmits attribute information using the request domain. The method is request. setattribute (). Then redirect the request to another processing module.

Session and status management
There are two methods: Cookie and session. The cookie is stored on the client,The session is saved on the server.. The image metaphor is that cookie is a discount card issued by the mall, and the discount policy is recorded on the discount card. Session is a metaphor for a disease history card. The hospital keeps a medical record for each patient. Only the card number is entered on the medical records card.
Cookie is a class that is instantiated through new cookie (string name, string value. Request. getcookies () return all cookies. Response. addcookie (cookie ). Httpsession is an interface. Request. getsession () to the session object.

JSP
Correspondence between JSP built-in objects and Servlet classes
Application ----> servletcontext
Config -----> servletconfig
Session -----> httpsession
Request -----> httpservletrequest
Response -----> httpservletresponse
Page is the servlet object that JSP is translated.
An out instance is a jspwriter instance. The printwriter that calls servletresponse. getwriter () writes the buffer content of the out object to the buffer provided by the servlet engine.
Pagecontext is a subclass of javax. servlet. jsp. jspcontext. The pagecontext object encapsulates the running information of the current JSP page. It provides methods to return other built-in objects of the JSP page.
The relationships between built-in objects can be obtained from the relationships in the servelt. You can get the application through config and the application through session. You can obtain the session through the request. You can use pagecontext to obtain all built-in objects.

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.