【JSP开发】session模拟购物车小例子
程序员文章站
2024-03-20 13:30:22
...
利用session来模拟购物网站的购物车,这次没有用静态页面,全部用Servlet来模拟。
用户点击购买之后,就会将商品加入到购物车中(其实就是放入session中),等用户点
击结账的时候,就会将购物车中的信息全部给用户看。
欢迎界面,里面有"购买"、"结账"链接,点击购买,就会加入"洗衣机"这个商品。然后
点击"结账"就会看到你买的商品
WelcomeServlet:
购买Servlet,会将购买的商品信息存入session
package cn.edu.session;
SessionDemo1:
结账Servlet,会将session中存入的商品信息拿出来
用户点击购买之后,就会将商品加入到购物车中(其实就是放入session中),等用户点
击结账的时候,就会将购物车中的信息全部给用户看。
欢迎界面,里面有"购买"、"结账"链接,点击购买,就会加入"洗衣机"这个商品。然后
点击"结账"就会看到你买的商品
WelcomeServlet:
package cn.edu.session;
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;
public class WelcomeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
//进主页的session会用到最后
request.getSession();
String url1=response.encodeURL("/day07/SessionDemo1");/*这个方法会自
动的在URL上面加上session的id号,这是防止用户禁用cookie(原因看我以前的文章)*/
String url2=response.encodeURL("/day07/SessionDemo2");
//效果:<a href='/day07/SessionDemo1;jsessionid=572FCD20D16825556EAAD9E8B1D3984D'>购买</a></br>
//<a href='/day07/SessionDemo2;jsessionid=572FCD20D16825556EAAD9E8B1D3984D'>结账</a>
out.write("<a href='"+url1+"'>购买</a></br>");
out.write("<a href='"+url2+"'>结账</a>");
}
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
doGet(request,response);
}
}
购买Servlet,会将购买的商品信息存入session
package cn.edu.session;
SessionDemo1:
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;
//购买
public class SessionDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
HttpSession session=request.getSession();
//request.getSession(false)只获取session,不出创建session(一般显示
购物车的时候会出现此代码)
String sessionid=session.getId();//详见sessio的id
Cookie cookie=new Cookie("JSESSIONID",sessionid);
cookie.setPath("/day07");
cookie.setMaxAge(30*60);
response.addCookie(cookie);
session.setAttribute("name", "洗衣机");
}
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
doGet(request,response);
}
}
结账Servlet,会将session中存入的商品信息拿出来
SessionDemo2:
package cn.edu.session;
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 SessionDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
HttpSession session=request.getSession();
String product=(String)session.getAttribute("name");
out.write("您购买的商品是"+product);
}
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
doGet(request,response);
}
}
模拟了购买一个商品进购物车的场景。后面还有一个模拟购物车的项目,是自选商品进购物车,比这更完善一些:http://blog.csdn.net/acmman/article/details/43451029。
转载请注明出处:http://blog.csdn.net/acmman
上一篇: rundeck调度工具的使用
下一篇: 简单购物车的实现