第八次
1.登陆
输入用户名密码,判断用户名和密码相同,登陆成功,session中保存用户的用户名,进入主页main.jsp,
主页有一个退出按钮,点击,回到登陆页login.jsp。要求:退出登录后,如果在浏览器直接输入主页main.jsp,
访问不了,直接跳到登陆页。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<script type="text/javascript">
function yz(){
if (fm.uname.value=="") {
alert("请输入账号");
return;
}
else if (fm.pas.value=="") {
alert("请输入密码");
return;
}
fm.submit();
}
</script>
<body>
<form action="logindo.jsp" name="fm">
用户名:<input type="text" name="uname" />
密码:<input type="password" name="pas" />
<input type="button" value="登录" onclick="yz()" />
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
String uname=request.getParameter("uname");
String pas=request.getParameter("pas");
if(uname.equals(pas)){
session.setAttribute("uname", uname);
request.getRequestDispatcher("session.jsp").forward(request, response);
}
else{
response.sendRedirect("no.jsp");
}
%>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
session.invalidate();
response.setHeader("refresh", "5;url=login.jsp");
%>
退出成功!
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
String uname=(String)session.getAttribute("uname");
//如果他是空,说明没登陆,直接访问该页面了
if(uname==null)
response.sendRedirect("login.jsp");
%>
欢迎你<%=uname %>
<a href="loginout.jsp">退出登录</a>
</body>
</html>


2.购物车
和上一题一起,在main.jsp中做一个购物车,里面显示3个商品名和价格 每一个后面有一个加入购物车按钮,
main.jsp中有一个按钮(或者超链接)可以显示购物车。(选作:在购物车中加删除按钮删除商品)
<%@page import="entity.ShanPin"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <% request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); List<ShanPin> lsps = new ArrayList<ShanPin>(); lsps.add(new ShanPin()); lsps.add(new ShanPin()); lsps.add(new ShanPin()); lsps.add(new ShanPin()); lsps.add(new ShanPin()); lsps.add(new ShanPin()); lsps.add(new ShanPin()); lsps.add(new ShanPin()); lsps.add(new ShanPin()); lsps.add(new ShanPin()); request.setAttribute("lsps", lsps); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <% String uname=(String)session.getAttribute("uname"); //如果他是空,说明没登陆,直接访问该页面了 if(uname==null) response.sendRedirect("login.jsp"); %> 欢迎你<%=uname %> <a href="loginout.jsp">退出登录</a> <table style="width: 600px; border: 1px red solid;"> <thead> <tr> <th>商品名称</th> <th>商品描述</th> <th>商品价格</th> </tr> </thead> <tbody> <c:if test="${empty lsps}"> <tr> <td colspan="3">请访问商品列表</td> </tr> </c:if> <c:if test="${!empty lsps}"> <c:forEach items="${requestScope.lsps}" var="sp"> <tr> <td>${sp.label}</td> <td>${sp.spdesc}</td> <td>${sp.price}</td> <td><a href="putinto.do?id=${sp.id}">放入购物车</a> </td> </tr> </c:forEach> </c:if> </tbody> </table> <a href="car.jsp">去购物车结算</a> </body> </html>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Collection"%>
<%@page import="entity.CarItem"%>
<%@page import="java.util.HashMap"%>
<%@page import="entity.CarController"%>
<%@page import="java.util.ArrayList"%>
<%@page import="entity.ShanPin"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>我的购物车</title>
</head>
<body>
<table style="width: 600px; border: 1px red solid;">
<thead>
<tr>
<th>商品名称</th>
<th>商品描述</th>
<th>商品价格</th>
<th>商品数量</th>
</tr>
</thead>
<tbody>
<c:if test="${empty sessionScope.car}">
<tr>
<td colspan="4">请访问商品列表,加入购物车</td>
</tr>
</c:if>
<c:if test="${!empty sessionScope.car}">
<c:forEach items="${sessionScope.car}" var="caritem">
<tr>
<td>${caritem.value.shanPin.label}</td>
<td>${caritem.value.shanPin.spdesc}</td>
<td>${caritem.value.shanPin.price}</td>
<td>${caritem.value.num}</td>
</tr>
</c:forEach>
</c:if>
</tbody>
<tfoot>
<tr>
<%
Object object=session.getAttribute(CarController.CAR);
int sum=0;
if(object!=null){
HashMap<String, CarItem> car =(HashMap<String, CarItem>) object;
Collection<CarItem> ccis=car.values();
Iterator<CarItem> iterator=ccis.iterator();
while(iterator.hasNext()){
CarItem carItem=iterator.next();
sum+=carItem.getShanPin().getPrice()*carItem.getNum();
}
}
request.setAttribute("sum", sum);
%>
<td colspan="4">总价为:<%=sum %></td>
</tr>
</tfoot>
</table>
<a href="session.jsp">继续加购</a>
</body>
</html>
package entity; import java.io.IOException; import java.util.HashMap; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.sun.java.swing.plaf.windows.resources.windows; import entity.CarItem; import entity.ShanPin; /** * Servlet implementation class CarController */ @WebServlet("/putinto.do") public class CarController extends HttpServlet { public static String CAR="car"; private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public CarController() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String id=request.getParameter("id"); HttpSession session=request.getSession(); Object object=session.getAttribute(CAR); HashMap<String, CarItem> car =null; if (object==null) { car=new HashMap<String, CarItem>(); session.setAttribute(CAR, car); } else { car=(HashMap<String, CarItem>) object; } CarItem carItem=car.get(id); if (carItem == null) { carItem=new CarItem(); ShanPin shanPin=new ShanPin(); shanPin.setId(Integer.parseInt(id)); carItem.setShanPin(shanPin); carItem.setNum(0); car.put(id, carItem); } carItem.setNum(carItem.getNum()+1); String javaScript="<script type=\"text/javascript\">\r\n" + " window.history.go(-1);\r\n" + "</script>"; response.getWriter().append(javaScript); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package entity; public class CarItem { private ShanPin shanPin; private Integer num=0; public ShanPin getShanPin() { return shanPin; } public void setShanPin(ShanPin shanPin) { this.shanPin = shanPin; } public Integer getNum() { return num; } public void setNum(Integer num) { this.num = num; } @Override public String toString() { return "CarItem [shanPin=" + shanPin + ", num=" + num + "]"; } }
package entity; import java.util.Date; import java.util.Random; public class ShanPin { private static Random random=new Random(new Date().getTime()); private Integer id=random.nextInt(Integer.MAX_VALUE); private String label ="java"+id; private String spdesc="汇编语言"; private Integer price= 25; public static Random getRandom() { return random; } public static void setRandom(Random random) { ShanPin.random = random; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getLabel() { return label; } public void setLabel(String label) { this.label = label; } public String getSpdesc() { return spdesc; } public void setSpdesc(String spdesc) { this.spdesc = spdesc; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } @Override public String toString() { return "ShanPin [id=" + id + ", label=" + label + ", spdesc=" + spdesc + ", price=" + price + "]"; } }



浙公网安备 33010602011771号