web实验报告——会话跟踪使用程序
来源:互联网 发布:拳击 书 知乎 编辑:程序博客网 时间:2024/05/21 16:22
实验八会话跟踪实用程序
一、实验目的
通过实验掌握下列知识:
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); 用以标志登录用户;
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”);
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实验报告——会话跟踪使用程序
- web实验报告——cookie的使用
- WEB会话跟踪
- WEB会话跟踪技术
- web实验报告——MVC模式
- WEB会话识别实验
- web开发会话跟踪技术
- web开发的会话跟踪
- web开发会话跟踪技术
- Servlet会话管理——Cookie会话跟踪
- Servlet总结03——会话跟踪
- 会话跟踪技术之—Cookie技术
- 会话跟踪技术之—session
- 使用 URL 重写跟踪会话
- web实验报告——JSP动态网页编程
- web实验报告——表单与JSP内置对象
- 课程 Java Web程序设计06: 会话跟踪
- OpenGL ES纹理
- C++笔试题笔记
- 项目撰写规范
- 数据订正:insert select,删除重复数据,truncate
- web实验报告——会话跟踪使用程序
- web实验报告——会话跟踪使用程序
- 项目撰写规范
- Double转字符串,不使用任何系统函数【完整篇】
- js 的Type属性和Language属性
- C# 中的委托和事件
- web实验报告——数据库连接与应用(SQL Server)
- 一个关于jar包的低级错误
- vc6环境下编译live555库的环境配置方法
- 关于ACM博弈组合问题