JSP第八周
1.登陆 login.jsp
输入用户名密码,判断用户名和密码相同,登陆成功,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 + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body bgcolor=#ccffcc> <script type="text/javascript"> function validate() { if (loginForm.uname.value == "") { alert("账号不能为空!"); return; } if (loginForm.upwd.value == "") { alert("密码不能为空!"); return; } loginForm.submit(); } </script> <form name="loginForm" action="dologin.jsp" method="post"> 用户名:<input type="text" name="uname"><br> 密码: <input type="password" name="upwd"> <input type="button" value="登录" onClick="validate()"> </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 + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body > <% request.setCharacterEncoding("utf-8"); String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); if (uname.equals(upwd)) { session.setAttribute("uname", uname); request.getRequestDispatcher("main.jsp").forward(request, response); } else { response.sendRedirect("fail.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 + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body bgcolor=#FFccff> <% String uname = (String) session.getAttribute("uname"); //如果他是空,说明没登陆,直接访问该页面了 if (uname == null) response.sendRedirect("login.jsp"); %> 欢迎你<%=uname%><br> <br> <a href="tuichu.jsp">退出登录</a> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body bgcolor=#bgcclo> <h1>登陆失败!!</h1> <%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 + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> <% session.invalidate(); response.setHeader("refresh", "5;url=login.jsp"); %> <h1>退出登录成功!</h1> </body> </html>
2.购物车
和上一题一起,在main.jsp中做一个商品展示,里面显示3个商品名和价格 ,每一个后面有一个加入购物车按钮,main.jsp中有一个按钮(或者超链接)可以显示购物车show.jsp 。(选作:在购物车show.jsp中加删除按钮删除商品)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@page import="shopping.Product"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body bgcolor=#FFccff> <% request.setCharacterEncoding("utf-8"); String name1=request.getParameter("name"); String price=request.getParameter("price"); if(name1!=null){ String name=new String(name1.getBytes("ISO-8859-1"),"utf-8"); Product p=new Product(name,price); ArrayList<Product> list=(ArrayList<Product>)session.getAttribute("list"); if(list==null){ list=new ArrayList<Product>(); } list.add(p); session.setAttribute("list",list); } String uname = (String) session.getAttribute("uname"); //如果他是空,说明没登陆,直接访问该页面了 if (uname == null){ out.print("您未登录,即将跳到登陆页....."); response.setHeader("refresh", "3;url=login.jsp"); } else { %> 欢迎你<%=uname%>这是首页 <br> <a href="tuichu.jsp">退出登录</a> <hr> 商品展示: <br> 欢迎进入水果商城: <br> <hr> <br>荔枝 9.99 <a href="main.jsp?name=荔枝&price=9.99">加入</a> <br>芒果 8.88 <a href="main.jsp?name=芒果&price=8.88">加入</a> <br>西瓜 10.9 <a href="main.jsp?name=西瓜&price=10.9">加入</a> <br> <form action="show.jsp"> <input type="submit" value="查看购物车"> </form> <% } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="shopping.Product"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body bgcolor=#eeccft> 欢迎你 这是购物车 <a href="tuichu.jsp">退出</a> <br> <% //从session中拿到集合,遍历 ArrayList<Product> list = (ArrayList<Product>) session .getAttribute("list"); if (list != null) { for (int i = 0; i < list.size(); i++) { out.print(list.get(i).getName() + "," + list.get(i).getPrice()); out.print("<a href='del.jsp?name=" + list.get(i).getName() + "'>删除</a><br>"); } } %> <a href="main.jsp">返回购物页面</a> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="shopping.Product"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.jsp' starting page</title> </head> <body> <% String name1 =request.getParameter("name"); String name=new String(name1.getBytes("ISO-8859-1"),"UTF-8"); ArrayList<Product> list=(ArrayList<Product>)session.getAttribute("list"); for(int i=0;i<list.size();i++){ if(list.get(i).getName().equals(name)) list.remove(i); } session.setAttribute("list", list); response.sendRedirect("show.jsp"); %> </body> </html>
package shopping; public class Product { String name; String price; public Product(String name, String price) { super(); this.name = name; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } }