web实验报告——会话跟踪使用程序

实验 会话跟踪实用程序
一、实验目的
通过实验掌握下列知识:
1 、   理解动态网页中会话跟踪的实现基础;
2 、   能使用会话跟踪实现购物车程序。
 
二、实验内容及步骤
1 、购物车程序基础练习
1 )将书中购物车程序实例源代码,配置于 tomcat 服务器中;
2 )运行该程序,查看运行效果,并将运行流程与效果,记录在实验报告中;
分析该程序采取的线程安全措施,并记录在实验报告中。
购物车程序实例源代码: session.rar
运行界面:
TechBooksPage
 

 OrderPage

ShowSession
 
 
2 、购物车程序改进练习
1 )为书中的基础程序增加登录页面( login.jsp ),并增加登录判断页面( LoginCheck.java ) , 若用户名为 bookstore ,密码为 123456 ,则登录成功,直接跳转显示 KidsBooksPage ,否则返回登录页面重新登录;
2 )若登录成功,则在登录判断页面( LoginCheck.java )中增加代码:
   session.setAttribute(“loginUser”, true);  用以标志登录用户;
3 )购书页面( KidsBooksPage 、 TechBooksPage )与订单页面( OrderPage )必须要登录用户才能访问,未登录用户需跳转到登录页面进行登录;
4 )若登录用户再次访问登录页面( login.jsp ),则无须再次登录,直接跳转显示 KidsBooksPage 。
  源代码:
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 > 用户登录界面 </ title >
</ head >
< body >
 
  <%
     // 使用内置对象 request 获取 loginUser 的 session 的值,若为空则停留在登陆界 面,
     // 若存在 session 值就直接跳转到 KidsBooksPage 页面
    Boolean flag1;
    HttpSession loginSession = request.getSession();
    flag1 = (Boolean)loginSession.getAttribute( "loginUser" );
     if (flag1== null  || flag1== false )
    { %>
         < br >
        < br >
        < br >
        < div  align = "center">
           < h1 > 用户登录 </ h1 >
         </ div >
        < form  action= "LoginCheck">
         < table  align="center">
         < tr >
            < td > 用户名 </ td >
            < td >< input  name = "userName"></td>
            </ tr >
            < tr >
            < td > 密码 </ td >
            < td >< input  type="password" name = "userPwd"></td>
            </ tr >  
            < tr >
            < td  colspan = "2" align = "center"><input type="submit" value = " 提交 " ></ td >
            </ tr >
         </ table >
         </ form >   
    <% }
    else
   { %>
        < jsp:forward  page= "KidsBooksPage" />
  <%  }
  %>
</ body >
</ html >
 
LoginCheck.java
package  session;
 
import  java.io.IOException;
import  javax.servlet.ServletException;
import  javax.servlet.http.Cookie;
import  javax.servlet.http.HttpServlet;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;
import  javax.servlet.http.HttpSession;
 
/**
  * Servlet implementation class LoginCheck
  */
public class  LoginCheck  extends  HttpServlet {
     private static final long serialVersionUID  = 1L;
      
     /**
      * @see HttpServlet#HttpServlet()
      */
     public  LoginCheck() {
         super ();
         //  TODO  Auto-generated constructor stub
    }
 
     /**
      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
      */
     protected void  doGet(HttpServletRequest request, HttpServletResponse response)  throws  ServletException, IOException {
        //  TODO  Auto-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" );
       }
    }
 
     /**
      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
      */
     protected void  doPost(HttpServletRequest request, HttpServletResponse response)  throws  ServletException, IOException {
        //  TODO  Auto-generated method stub
    }
 
}
运行结果:

 

 

 
 
3 、购物车程序改进练习 2 (选做)
1 )为以上程序增加注销功能,在购书页面( KidsBooksPage 、 TechBooksPage )与订单页面( OrderPage )中增加注销按钮;
 2 )按下注销按钮后,交由 Logout.java 页面进行处理:
   session.invalidate();
   response.sendRedirect(“../login.jsp”);
源代码:
package  session;
 
import  java.io.*;
import  javax.servlet.*;
import  javax.servlet.http.*;
 
import  java.util.*;
 
 
public abstract class  CatalogPage  extends  HttpServlet {
  private  Item[]  items ;
  private  String[]  itemIDs ;
  private  String  title ;
 
  /** Given an array of item IDs, look them up in the
    *   Catalog and put their corresponding Item entry
    *   into the items array. The Item contains a short
    *   description, a long description, and a price,
    *   using the item ID as the unique key.
    *   <P>
    *   Servlets that extend CatalogPage <B> must </B> call
    *   this method (usually from init ) before the servlet
    *   is accessed.
    */
 
  protected void  setItems(String[] itemIDs) {
     this . itemIDs  = itemIDs;
     items  =  new  Item[itemIDs. length ];
     for ( int  i=0; i< items .length; i++) {
       items [i] = Catalog.getItem(itemIDs[i]);
    }
 }
 
  /** Sets the page title, which is displayed in
    *   an H1 heading in resultant page.
    *   <P>
    *   Servlets that extend CatalogPage <B> must </B> call
    *   this method (usually from init ) before the servlet
   *   is accessed.
    */
 
  protected void  setTitle(String title) {
     this . title  = title;
 }
 
  /** First display title, then, for each catalog item,
    *   put its short description in a level - two (H2) heading
    *   with the price in parentheses and long description
    *   below. Below each entry, put an order button
    *   that submits info to the OrderPage servlet for
    *   the associated catalog entry.
    *   <P>
    *   To see the HTML that results from this method, do
    *   "View Source" on KidsBooksPage or TechBooksPage, two
    *   concrete classes that extend this abstract class.
    */
 
  public void  doGet(HttpServletRequest request,
                    HttpServletResponse response)
       throws  ServletException, IOException { 
          response.setContentType( "text/html" );
           response.setCharacterEncoding( "gb2312" );
            // 获取名为 loginUser 的 session ,若为 true, 就显示该页面,否则返回 login.jsp 页面
          Boolean flag;
        HttpSession session = request.getSession();
         synchronized (session) {
        flag = (Boolean)session.getAttribute( "loginUser" );
         if (flag== null  || flag ==  false )
          {
         response.sendRedirect( "login.jsp" );
          }   
         else
        {
            if  ( items  ==  null ) {
             response.sendError(response. SC_NOT_FOUND ,
                                 "Missing Items." );
              return ;
           }
           PrintWriter out = response.getWriter();
           out.println(ServletUtilities.headWithTitle( title ) +
                        "<BODY BGCOLOR=/"#FDF5E6/">/n"  +
                        "<H1 ALIGN=/"CENTER/">"  +  title  +  "</H1>" );
           Item item;
            for ( int  i=0; i< items .length; i++) {
             out.println( "<HR>" );
             item =  items [i];
              // Show error message if subclass lists item ID
              // that's not in the catalog.
              if  (item ==  null ) {
               out.println( "<FONT COLOR=/"RED/">"  +
                            "Unknown item ID "  +  itemIDs [i] +
                            "</FONT>" );
             }  else  {
               out.println();
               String formURL =
                  "OrderPage" ;
                // Pass URLs that reference own site through encodeURL.
               formURL = response.encodeURL(formURL);
               out.println
                 ( "<FORM ACTION=/""  + formURL +  "/">/n"  +
                   "<INPUT TYPE=/"HIDDEN/" NAME=/"itemID/" "  +
                   "       VALUE=/""  + item.getItemID() +  "/">/n"  +
                   "<H2>"  + item.getShortDescription() +
                   " ($"  + item.getCost() +  ")</H2>/n"  +
                  item.getLongDescription() +  "/n"  +
                   "<P>/n<CENTER>/n"  +
                   "<INPUT TYPE=/"SUBMIT/" "  +
                   "VALUE=/"Add to Shopping Cart/">/n"  +
                   "</CENTER>/n<P>/n</FORM>" );
               String formURL2 =  "Logout" ;
               formURL2 = response.encodeURL(formURL2);
               out.println( "<FORM ACTION=/""  + formURL2 +  "/">/n"  +
                     "<INPUT TYPE=/"SUBMIT/" "  +
                        "VALUE=/" 注销 /">/n"  +
                       "</CENTER>/n<P>/n</FORM>"
                 );
             
       }
       out.println( "<HR>/n</BODY></HTML>" );
      }
     }
   }
 }
 
  /** POST and GET requests handled identically. */
 
  public void  doPost(HttpServletRequest request,
                     HttpServletResponse response)
       throws  ServletException, IOException {
    doGet(request, response);
 }
}
 
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值