Experiment Eight Session Tracking Utility I. Purpose of the experimentThrough the experiment grasps the following knowledge: 1, understands the dynamic webpage the realization base of the session tracing; 2, can use the session to track the realization shopping cart program.Ii. Contents and steps of the experiment 1 , Shopping Cart program Basics Exercise1 The book Shopping Cart program instance source code, configure in the Tomcat server; 2 run the program to see the running effect, and will run the process and effect, recorded in the experiment report, analyze the thread safety measures taken by the program and record it in the experiment report. Shopping Cart Program Instance source code: Session.rar Run Interface: techbookspage: Orderpage: ShowSession:2 , Shopping cart program improvement exercises1 for the book's Basic program to increase the login page (login.jsp), and increase the Login Judgment page (Logincheck.java), if the user name is bookstore, the password is 123456, the login is successful, direct jump show kidsbookspage, otherwise return Log back on the login page, 2 if the login is successful, add code to the Login Judgment page (Logincheck.java): Session.setattribute ("Loginuser", true); to sign the logged-in user; 3) Purchase book page (Kidsbookspage, Techbookspage) and Order page ( Orderpage) must be logged in to the user to access, not logged in users need to jump to the login page to login; 4 If the logged-in user accesses the login page again (login.jsp), you do not need to login again, direct jump display kidsbookspage. Source code: login.jsp: <%@ page language= "java" contenttype= "text/html; charset=gb2312 " pageencoding=" gb2312 "%> <! DOCTYPE html PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "http://www.w3.org/TR/html4/ LOOSE.DTD "> < HTML > < head > < meta http-equiv=" Content-type " content=" text/html; charset=gb2312 "> < title > User Login Interface </title > if(flag1==NULL|| flag1==false ) {%> < br > & nbsp; < br > < br > & nbsp < div align = "center" > < h1 > User Login Else{%> < Jsp:forward page= "Kidsbookspage"/> <%}%> </body > </HTML > Loginch Eck.javaPackageSessionImportJava.io.IOException;ImportJavax.servlet.ServletException;ImportJavax.servlet.http.Cookie;ImportJavax.servlet.http.HttpServlet;ImportJavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportJavax.servlet.http.HttpSession; /** * Servlet Implementation class Logincheck * * Public classLogincheckextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; /** *@seeHttpservlet#httpservlet () */ PublicLogincheck () {Super(); //TODOauto-generated constructor stub}/** *@seeHttpservlet#doget (HttpServletRequest request, httpservletresponse response) * *protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsServletexception, IOException {//TODOauto-generated Method Stub HttpSession session = Request.getsession (); String name = Request.getparameter ("UserName"); String pwd = Request.getparameter ("Userpwd");if(Name.equals ("bookstore") && pwd.equals ("123456")) {Session.setattribute ("Loginuser",true); Response.sendredirect ("Kidsbookspage"); }Else{Response.sendredirect ("login.jsp"); } } /** *@seeHttpservlet#dopost (HttpServletRequest request, httpservletresponse response) * *protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsServletexception, IOException {//TODOauto-generated method stub} Run results:3 , Shopping cart program improvement exercises 2 (choose to do)1 for the above program to increase the cancellation function, in the Purchase book page (Kidsbookspage, Techbookspage) and Order page (Orderpage) to increase the logout button, 2 press the Logout button, the Logout.java page for processing: Session.invalidate (); Response.sendredirect (".. /login.jsp "); Source:PackageSessionImportjava.io.*;Importjavax.servlet.*;Importjavax.servlet.http.*;Importjava.util.*; Public Abstract classCatalogpageextendsHttpServlet {Privateitem[] items;PrivateString[] Itemids;PrivateString title; /** Given An array of item IDs, look them up in the * Catalog and put their corresponding item entry * into th E items Array. The item contains a short * description, a long description, and a price, * using the item ID as the unique Ke Y. * <P> * servlets that extend Catalogpage <B> must </B> call * This method (usually From Init) before the servlet * is accessed. */protected voidSetitems (string[] itemids) { This. Itemids = Itemids; Items =NewItem[itemids. length]; for(inti=0; i< items. Length; i++) {items [i] = Catalog.getitem (Itemids[i]); }/** Sets the page title, which is displayed in * a H1 heading in resultant page. * <P> * servlets that extend Catalogpage <B> must </B> call * This method (usually. It) before the servlet * is accessed. */protected voidSettitle (String title) { This. title = title; /** the title, then, for each catalog item, * Put it short description in a level-two (H2) Headin G * with the parentheses and long description * |