session

来源:互联网 发布:windows平板应用 编辑:程序博客网 时间:2024/04/29 01:16

购物车

package session;


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>");
   }
  }
 }

 
 public void init() throws ServletException {
  // Put your code here
 }

}

 

package session;


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>");
   }
  }
 }

 
 public void init() throws ServletException {
  // Put your code here
 }

}

 

原创粉丝点击