State management of Java_web

Source: Internet
Author: User
Tags app service

Review and work reviews:

(1) How does the JSP handle client requests?

Handling responses using the Response object

(2) Please describe what is the difference between forwarding and redirection?

Forwarding is performed on the server side, and the commit information is passed across multiple pages through the forward method. The address bar of the client browser does not show the post-turn address

Redirection is a function of the client, by requesting a new address to implement the page steering, in the address bar can display the post-turn address

(3) How does JSP implement database access?

Accessing the database using JDBC

Preview check:
(1) Besides the request object and the response object, what built-in objects does the JSP include?
1, Cookie 2, session is JSP built-in object 3, Application object
(2) What is the role of cookies?
1, the tracking of specific objects 2, Statistics page views 3, simplified login

(3) Please indicate the difference between the session object and the Application object?
A session-scoped content is valid, and the object that is bound to the session is within that range
Effective within an application server, when the app service is started, the object is created and shared with all users

This chapter aims

The principle and application of mastering cookies

The principle and application of mastering session

The principle and application of mastering application

Chapter Tasks

I. Using cookies to save information that has been accessed

(1) What is a cookie?

A cookie is a series of text messages that a Web server holds on the client

The role of Cookies

Tracking for a specific object

Statistics page views

Simplified Login

Security can easily leak information

(2) The syntax of a cookie

First step: Import Package Imports="javax.servlet.http.Cookie" Second step: Create Cookiecookie Newcookie =new Cookie ("parameter""value"); Parameter: Name used to represent a cookie (key) value: Used to represent the value corresponding to the current key name step three: Write Cookieresponse.addcookie (Newcookie)

(3) Common methods for setting cookie properties

type          method name                  stated   void       setmaxage (int  expiry)      set the validity period of the cookie in seconds  void       setValue (string value)     after the cookie is created, assign a value to the cookie String     getName ()                  Gets the name of the cookie string     getValue () Gets the value of the                 cookie string     getmaxage ()                Gets the effective time of the cookie, in seconds
Case:------Get the user name from the Login Verification page form------<%String username=reqeust.getparameter ("username");//Create a cookie in the form of Key/valueCreate a cookie that uses the response Addcookie method to save the Cookiecookie uname=NewCookie ("uname"), username); Response.addcookie (uname);%>-----Display the user name---------------on the display page <%//gets the cookie in the request, saved as an arrayGet An array of cookies using reqeust, and get the corresponding content cookie by the name of the cookie cookies[]=request.getcookies (); //Iterate through an array to get Key=uname's cookie       for(inti=0;i<cookies.length;i++) {Cookie Ucookie=Cookies[i]; if(Ucookie.getname (). Equals ("uname"));//Determine the name of the cookie%>you are welcome:<%=ucookie.getvalue ()%>//gets the value of the key corresponding to the output display<%}%>

Second, using session object to implement page access control

(1)

(2) What is a session?
A session is a call between the browser and the server, including multiple requests between the browser and the server, the response process

(3) JSP built-in object session

(4) The relationship between session and window
When a session corresponds to a window, is the window opened by a hyperlink a new session?
Answer: No
Each session object with browser one by one corresponding to re-open a browser, equivalent to re-create a session object to reopen an IE window, direct access to the first page of the system
New window opened by hyperlink, the session of the new window is the same as the session of its parent window

(5) Use session to implement access control

 ------The code snippet for the login Processing page------<% if  (Rs.next ()) {//  If it is a registered user   "Logined_user" , Logineduser); Response.sendredirect ( "index.jsp" );}  else   {Response.sendredirect ( " login.html ");}  %>--If the user's login information does not exist in the session, go to the login page--<%user User  = (user) Session.getattribute ("Logined_user"  if  (user = = null   " login.jsp ");}  %> 

(6) include directive
In addition to the first page, other pages also need to join the login verification, there is no way to avoid the emergence of redundant code?
You can write some common content into a separate file, and then reference the file with the include directive, which reduces the redundancy of the Code and facilitates the modification of the common content

case: Create a login verification file logincontrol.jsp Import= "Org.jbit.bean.User"%><%= (User) session.getattribute ("Logined_user"); if NULL {response.sendredirect ("login.html");} %> using the include directive to reference the login verification file in the Background header page <%@ include file= "logincontrol.jsp"    %>

(7) Comparison between a cookie and a session
Session is to save user information on server side, cookie is to save user information on client
The session holds the object, and the cookie holds the string
The session closes with the end of the conversation and the cookie can be stored on the client for long
Cookies are typically used to save unimportant user information, and important information is saved using the session

Third, the use of application objects to achieve statistics online people

(1) JSP built-in object application

Application is similar to the system's "global Variables", which are used to implement data sharing between users application objects in a common way:

void SetAttribute (String key, Object value): Stores the value of an object in application in Application.setattribute ("Logined_user", in the form of a key/value) New ArrayList ());

Object GetAttribute (String key): Gets the value of the stored object in application according to the key

if (Application.getattribute ("logined_user") = null) {List loginedusers = (list) Application.getattribute ("logined _user "); }

(2) Application Object

(3) Number of visitors achieved 2-1

Create a login processing page------Log on to process page code snippets------if(Rs.next ()) {User Logineduser=NewUser (name, pass); Session.setattribute ("Logined_user", Logineduser); List loginedusers=NewArrayList ();//Visitor Listif(Application.getattribute ("Logined_user")! =NULL) Loginedusers= (List) application.getattribute ("Logined_user"); Loginedusers.add (Logineduser); //put the new logged in user's information into the visitor listApplication.setattribute ("Logined_user", loginedusers); Response.sendredirect ("Index.jsp");} Create a Visitor statistics page------Number of visited statistics page code snippet------<% List loginedusers =NewArrayList ();//Visitor Listif(Application.getattribute ("Logined_user")! =NULL) Loginedusers= (List) application.getattribute ("Logined_user"); %>at present, there are<%=loginedusers.size ()%> has visited this site!<br><br>

(4) Scope of JSP built-in objects

State management of Java_web

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.