使用Session实现购物车

使用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]
+ "&nbsp;&nbsp;&nbsp;&nbsp;<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
}


}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值