使用Session实现购物车----功能说明
程序包含一个HTML文件和两个Servlet组件:logon.html、LogonServlet.java和CoursesServlet.java。
logon.html提供用户登录的界面;LogonServlet.java是一个负责处理用户登录的Servlet程序。
CoursesServlet.java完成了三项功能:显示所有可选课程的列表、将用户选择的课程放入购物车、显示购物车中的课程。
LogonServlet.java的登录验证方式非常简单,只要登录的用户名不为空白字符串即可;LogonServlet.java先将成功登录的用户名保存到Session中,然后将请求转发给CoursesServlet.java显示所有可选课程的列表;如果登录失败,LogonServlet.java则再次显示登录界面。
CoursesServlet.java需要完成如下任务:
首先判断访问请求是否来自一个已登录用户,如果不是,则将请求重定向到logon.html页面。接着判断当前访问请求是否是用户选择课程时发出的,如果是,则将用户选择的课程加入购物车。
最后显示出所有供选择的课程列表和已放入购物车中的课程列表。
使用Session实现购物车----代码说明
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>ShoppingLogin.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=GB2312">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form id="form1" name="form1" method="post" action="ShoppingLogin">
<p>用户名:
<label for="username"></label>
<input type="text" name="username" id="username" />
</p>
<p>
<input type="submit" name="button" id="button" value="提交" />
<input type="reset" name="button2" id="button2" value="重置" />
</p>
</form>
</body>
</html>
CoursesServlet.java
package com.hbsi.csdn.SessionTest;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class CoursesServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public CoursesServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String sessionName = (String) session.getAttribute("name");
if (sessionName == null) {
response.sendRedirect("ShoppingLogin.html");
return;
}
String coursesSelect = request.getParameter("cname");
if (coursesSelect != null) {
Vector vCourses = (Vector) session.getAttribute("courses");
if (vCourses == null) { // 判断用户是否是第一次选择课程,如果是第一次,则新建向量,然后将获取的课程名添加至向量,并将向量存入session
vCourses = new Vector();
vCourses.add(coursesSelect);
session.setAttribute("courses", vCourses);
} else {
if(vCourses.contains(coursesSelect)){
out.println(sessionName+",你以前选择过了"+coursesSelect+"<hr>");
}else{
vCourses.add(coursesSelect);
}
}
}
//显示我们要购买的课程
String[] courses = { "c", "c++", "java", "java web", "vc#" };
out.println(sessionName + ",请选择你要购买的课程:<br>");
for (int i = 0; i < courses.length; i++) {
out.println(courses[i]
+ " <a href='CoursesServlet?cname="
+ courses[i] + "'>选择</a><br>");
}
out.println("<hr>");
Vector vCourses=(Vector) session.getAttribute("courses");
out.print(sessionName+",你选择购买的课程是:<br>");
if(vCourses!=null){
for(Enumeration e=vCourses.elements();e.hasMoreElements();){
out.println((String)e.nextElement()+"<br>");
}
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
SessionLogin.java
package com.hbsi.csdn.SessionTest;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class SessionLogin extends HttpServlet {
/**
* Constructor of the object.
*/
public SessionLogin() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String checkcode=request.getParameter("checkcode");
HttpSession session=request.getSession(false);
String savecheckcode=(String) session.getAttribute("check_code");
if(!checkcode.equals(savecheckcode)){
out.println("验证码无效");
return;
}
session.removeAttribute("check_code");
out.println("验证码通过!");
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}