web实验报告——会话跟踪使用程序
2010-09-13 16:25
351 查看
实验八会话跟踪实用程序 一、实验目的 通过实验掌握下列知识: 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; /** *ServletimplementationclassLoginCheck */ publicclass LoginCheck extends HttpServlet { privatestaticfinallongserialVersionUID = 1L; /** *@seeHttpServlet#HttpServlet() */ public LoginCheck() { super(); // TODO Auto-generated constructor stub } /** *@seeHttpServlet#doGet(HttpServletRequestrequest,HttpServletResponseresponse) */ protectedvoid 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"); } } /** *@seeHttpServlet#doPost(HttpServletRequestrequest,HttpServletResponseresponse) */ protectedvoid 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.*; publicabstractclass CatalogPage extends HttpServlet { private Item[] items; private String[] itemIDs; private String title; /**GivenanarrayofitemIDs,lookthemupinthe * CatalogandputtheircorrespondingItementry * intotheitemsarray.TheItemcontainsashort * description,alongdescription,andaprice, * usingtheitemIDastheuniquekey. * <P> * ServletsthatextendCatalogPage<B>must</B>call * thismethod(usuallyfrominit)beforetheservlet * isaccessed. */ protectedvoid 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]); } } /**Setsthepagetitle,whichisdisplayedin * anH1headinginresultantpage. * <P> * ServletsthatextendCatalogPage<B>must</B>call * thismethod(usuallyfrominit)beforetheservlet * isaccessed. */ protectedvoid setTitle(String title) { this.title = title; } /**Firstdisplaytitle,then,foreachcatalogitem, * putitsshortdescriptioninalevel-two(H2)heading * withthepriceinparenthesesandlongdescription * below.Beloweachentry,putanorderbutton * thatsubmitsinfototheOrderPageservletfor * theassociatedcatalogentry. * <P> * ToseetheHTMLthatresultsfromthismethod,do * "ViewSource"onKidsBooksPageorTechBooksPage,two * concreteclassesthatextendthisabstractclass. */ publicvoid 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>"); } } } } /**POSTandGETrequestshandledidentically.*/ publicvoid doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |
相关文章推荐
- web实验报告——会话跟踪使用程序
- web实验报告——cookie的使用
- 使用C/C++实现Socket聊天程序(代码+实验报告)
- 使用C/C++实现Socket聊天程序(代码+实验报告)
- C++中使用调试器逐步跟踪程序
- Lodop使用之WEB套打程序开发
- 不使用Web服务器编写SOAP服务程序相关联接
- 可以在mono下运行的一个ajax web小程序,使用PostgreSQL作为数据库(期待MonoDevelop 2.0)
- 【实验报告】《数据结构》实验一: VC编程工具的灵活使用
- Acunetix Web Vulnerability Scanner使用和生成报告的方法
- # 2016-2017-2 20155319 《Java程序设计》实验四Android程序开发实验报告
- 使用GruntJS构建Web程序之合并压缩篇
- 用在WEB开发中实现会话跟踪实现
- 循序渐进看Java web日志跟踪(3)-Log4J的使用和配置
- JAVAWEB开发之JSP、EL、及会话技术(Cookie和Session)的使用详解
- 使用 Watir 加速面向 Web 应用的自动化测试程序的开发
- 关于使用Spring和hibernate开发web程序的配置说明和简单实例的详细说明
- 使用Ruby程序实现web信息抓取的教程
- Android开发中使用WebView后程序进入后台依旧占用CPU的解决
- 第十周实验报告--2--学会使用循环控制语句解决实际问题