session案例:使用Session完成简单的购物功能
程序员文章站
2022-03-14 22:49:45
...
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%--sale.jsp--%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<a href="<%=request.getContextPath() %>/servlet/BuyServlet?prod=洗衣机">洗衣机</a>
<a href="<%=request.getContextPath() %>/servlet/BuyServlet?prod=电视机">电视机</a>
<a href="<%=request.getContextPath() %>/servlet/PayServlet">结账</a>
</body>
</html>
package it.com.session;
import java.io.IOException;
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 BuyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String prod=request.getParameter("prod");
prod=new String(prod.getBytes("iso8859-1"),"utf-8");//解决乱码问题
HttpSession session=request.getSession();
session.setAttribute("prod", prod);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
package it.com.session;
import java.io.IOException;
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 PayServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8"); //解决乱码问题
HttpSession session=request.getSession();
String prod=(String)session.getAttribute("prod");
response.getWriter().write("您购买的是:"+prod+"应付10000元");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
上一篇: python 二分查找的实现
下一篇: Cassandra一致性日志分析
推荐阅读
-
使用Angular.js实现简单的购物车功能
-
PHP 实现超简单的SESSION与COOKIE登录验证功能示例
-
使用beaker让Facebook的Bottle框架支持session功能
-
PHP cookie,session的使用与用户自动登录功能实现方法分析
-
ASP.NET 使用application和session对象写的简单聊天室程序
-
Java IO流案例:使用IO流的形式完成简单的登录注册功能
-
Android Studio 爬虫 之 简单实现使用 jsoup/okhttp3 爬取购物商品信息的案例demo(附有详细步骤)
-
为什么在WINDOWS下使用PHP4的SESSION功能老是报路
-
PHP+MYSQL+SESSION实现简单购物车功能
-
ASP.NET中在一般处理程序中使用session的简单介绍