Request,response,session,cookie,application

Source: Internet
Author: User

A:request
Customer service side to server side request

Java is about encapsulation, so request is a package of something, what is it?
By literal means: request!
From the HTTP protocol, when you send a request to the server, you will give some information to the servers, such as your language, Session-id and other basic information, and then add such as when you submit the form when the data ....
All of this data has been encapsulated in the request after it has entered the server, so you can easily get all kinds of information in the request.

B:response
The server responds to the request and returns it to the user (client side, browser)


1 solving Chinese garbled characters
Request.setcharacterencoding ("");-----solve Chinese garbled characters
---------The data is passed from page A to page B, the data is prevented from being passed garbled
Response.setcontenttype ();------solve Chinese garbled characters
---------jump from page A to page B to prevent the B page from garbled characters

2 Steering Page
Response.sendredirect ();------redirection
Request.getrequestdispactcher (). ForWord ();---forwarding

Steering difference:
1. The front is slow, the latter is fast
2. The value Saved by Request.setattribute () cannot be read in the front, while the latter (request) can read
3. The former Address bar shows the address after the true turn, and the latter shows the address before the turn
4. The former, the "/" to the beginning of the address, represents the root directory of the machine, while the latter, "/" represents the root directory of the application

3. Get the value
Request.getparameter ("title"); is the value of taking the form (last page)
and Request.getattrebute ("name"); (The latter gets the value of the Request.setattribute ()) is not the same as the attention to distinguish


Customer Service (A)-(Request)-"Server (B) (jump)---(request)-" another page (c) {These two request is different, C cannot request.getattrebute (value in a)}
Unless, on the server side (b) Reques.getrequestdispatcher ("specify page C"). Forward (Request,response);
C to Request.getattrebute (the value in a);
{Explanation: Package A to B's request and pass it as a total parameter to the C page, i.e. A to B, B to a response, to C page}

The JSP built-in object, which is a class in the servlet, is then instantiated. So you can use them directly (in JSP and servlet)
In struts:
HttpServletRequest request=servletactioncontext.getrequest ();
HttpServletResponse Response=servletactioncontext.getresponse ();
Map<string, Object> Session=actioncontext.getcontext (). GetSession ();


One: Session sessions
On the server side, close the browser and disappear.
Session.setattribute ("name", object)
GetAttribute ("Object name")
User name to be displayed when logging in

Can't be used directly in a servlet.
HttpSession session = Request.getsession ();
Session.setattribute ("User", US);

Two: Persistent (on-disk) cookies
There is a customer service side
A:
Cookie Cook=new Cookie ("First name", value (string, not object))
{
User Us=new user ();
Us.setpassword (STRPWD);
Us.setusername (Strna);
Cookie Cook=new Cookie ("Loginuser", Urlencoder.encode (Us.getusername ()));
}

Cook.setmaxage (365*24*60*60); expiry time
Response.addcookie (Cook);
B:

String uid= "";
Cookie[] Cooks=request.getcookies ();
if (cooks!=null) {
for (int i=0;i<cooks.length;i++) {
if (Cooks[i].getname (). Equals ("Loginuser")) {
Uid=urldecoder.decode (Cooks[i].getvalue ());
}
}
}
println ("UID"); Display user name

In the servlet (cannot) direct use, to introduce the package import Javax.servlet.http.Cookie;

Three: Global application

cannot be used directly in a servlet
ServletContext application = This.getservletcontext ();



Server-side (server generally does not stop, if you want to stop the existence of the database, started after the removal from the database)
Count of visits, number of people online

It exists in the memory space of the server, and once the server is started, a Application object is automatically generated, and the application object will persist unless the server is shut down. In the life cycle of the Application object, each JSP program running on the current server can be arbitrarily accessed and the values of the parameters (or Java objects) that the application object binds to.

During the operation of the JSP server, there is only one Application object, the Application object is created by the server, it is automatically purged by the server, cannot be created by the user, and the user cannot clear the Application object. We can only create a synchronous copy of this Application object. Application is the synchronized copy of the first Application object created automatically by the JSP engine (default). It is important to note that these replicas are synchronized and that any changes that occur on any one copy will affect other replicas (with the session limit).

1.setAttribute (String name, Object object)

2.getAttribute (String name)


{//Do not use setattribute () before Get, if not (count this property name), then Ocount is empty
Object ocount= Application.getattribute ("Count");
if (ocount==null) {
Application.setattribute ("Count", 1);
}
else{
Application.setattribute ("Count", 1+ (Integer) ocount);
}
。。。。。。。。。。。。。。。。。。。。。。。。。
int ocount= (Integer) application.getattribute ("Count");
Print (ocount); The first few visits
}

3.removeAttribute ()

Getattributenames () returns all parameters that are bound to the current application object or the name of the Java object; The return value of this method is an enumeration object (enumeration).

Getserverinfo () This method can get the details of the current server

Log (String msg) This method is used to write information to the log file of the server, and we can record the visitor's information in the log file, which is useful for statistical access to traffic sources, and also facilitates the administrator to monitor the operation of the website. You can also log error events that occur during the execution of a JSP program into the logs file

Request,response,session,cookie,application

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.