jsp8
1.登陆 login.jsp
输入用户名密码,判断用户名和密码相同,登陆成功,session中保存用户的用户名,进入主页main.jsp,主页有一个退出按钮,点击,回到登陆页login.jsp。要求:退出登录后,如果在浏览器直接输入主页main.jsp,访问不了,直接跳到登陆页。
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme() + "://" 5 + request.getServerName() + ":" + request.getServerPort() 6 + path + "/"; 7 %> 8 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 10 <html> 11 <head> 12 <title>My JSP 'index.jsp' starting page</title> 13 </head> 14 15 <body bgcolor=#ccffcc> 16 <script type="text/javascript"> 17 function validate() { 18 if (loginForm.uname.value == "") { 19 alert("账号不能为空!"); 20 return; 21 } 22 if (loginForm.upwd.value == "") { 23 alert("密码不能为空!"); 24 return; 25 } 26 loginForm.submit(); 27 } 28 </script> 29 30 31 <form name="loginForm" action="two.jsp" method="post"> 32 33 用户名:<input type="text" name="uname"><br> 密码: <input 34 type="password" name="upwd"> <input type="button" value="登录" 35 onClick="validate()"> 36 37 38 39 40 41 </form> 42 </body> 43 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme() + "://" 5 + request.getServerName() + ":" + request.getServerPort() 6 + path + "/"; 7 %> 8 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 10 <html> 11 <head> 12 <title>My JSP 'index.jsp' starting page</title> 13 </head> 14 15 <body > 16 <% 17 request.setCharacterEncoding("utf-8"); 18 String uname = request.getParameter("uname"); 19 String upwd = request.getParameter("upwd"); 20 if (uname.equals(upwd)) { 21 session.setAttribute("uname", uname); 22 request.getRequestDispatcher("three.jsp").forward(request, 23 response); 24 } else { 25 response.sendRedirect("four.jsp"); 26 } 27 %> 28 </body> 29 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme() + "://" 5 + request.getServerName() + ":" + request.getServerPort() 6 + path + "/"; 7 %> 8 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 10 <html> 11 <head> 12 <title>My JSP 'index.jsp' starting page</title> 13 </head> 14 15 <body bgcolor=#FFccff> 16 <% 17 String uname = (String) session.getAttribute("uname"); 18 19 if (uname == null) 20 response.sendRedirect("one.jsp"); 21 %> 22 欢迎你<%=uname%><br> 23 <br> 24 <a href="one.jsp">退出登录</a> 25 </body> 26 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <title>My JSP 'index.jsp' starting page</title> 11 </head> 12 13 <body bgcolor=#bgcclo> 14 <h1>登陆失败!!</h1> 15 <%response.setHeader("refresh", "5;url=one.jsp");%> 16 </body> 17 </html>
2.购物车
和上一题一起,在main.jsp中做一个购物车,里面显示3个商品名和价格 每一个后面有一个加入购物车按钮,main.jsp中有一个按钮(或者超链接)可以显示购物车。(选作:在购物车中加删除按钮删除商品)
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <html> 3 <head> 4 <title>My JSP 'index.jsp' starting page</title> 5 </head> 6 <body> 7 8 <% 9 String uname = (String) session.getAttribute("uname"); 10 //如果他是空,说明没登陆,直接访问该页面了 11 if (uname == null) 12 response.sendRedirect("login.jsp"); 13 %> 14 15 欢迎你<%=uname%> 16 17 18 <hr> 19 商品展示: 20 <br> 21 <hr> 22 <form action="six.jsp" method="post"> 23 请选择要加入购物车的商品:<br> <input type="checkbox" name="list" value="苹果"> 24 苹果 ¥9.9<br> <input type="checkbox" name="list" value="橙子"> 25 橙子 ¥7.88<br> <input type="checkbox" name="list" value="橘子"> 26 橘子 ¥6.88<br> 27 <hr> 28 <input type="submit" value="加入购物车"> 29 30 </form> 31 32 33 </body> 34 </html>
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 3 <html> 4 <head> 5 6 7 <title>My JSP 'index.jsp' starting page</title> 8 9 </head> 10 11 <body> 12 <h2>购物车添加的商品有:</h2> 13 <% 14 request.setCharacterEncoding("utf-8"); 15 String listName[] = request.getParameterValues("list"); 16 if (listName == null) { 17 out.print("购物车为空!"); 18 } else { 19 for (int i = 0; i < listName.length; i++) { 20 out.print("(" + (i + 1) + ")" + listName[i] + "<br>"); 21 } 22 } 23 %> 24 </body> 25 </html>