第八周jsp
1.登陆
输入用户名密码,判断用户名和密码相同,登陆成功,session中保存用户的用户名,进入主页main.jsp,主页有一个退出按钮,点击,回到登陆页login.jsp。要求:退出登录后,如果在浏览器直接输入主页main.jsp,访问不了,直接跳到登陆页。
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <form action="Interface.jsp" method="post" name="eight"> 请输入账号:<input name="name" type="text"/><br/> 请输入密码:<input name="password" type="password"/><br/> <input type="button" value="登录" onclick="denglu()"/> <script type="text/javascript"> function denglu(){ if(eight.name.value==""){ alert("账号不能为空!"); return; } if(eight.password.value==""){ alert("密码不能为空!"); return; } eight.submit(); } </script> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); String password = request.getParameter("password"); if (name.equals(password)) { session.setAttribute("name", name); request.getRequestDispatcher("logo.jsp") .forward(request, response); } else { request.getRequestDispatcher("no.jsp").forward(request, response); } %> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登录成功</title> </head> <body> <% String name = (String) session.getAttribute("name"); if (name == null) { response.sendRedirect("Interface1.jsp"); } %> 欢迎你<%=name%><br> <br> <form action="exit.jsp"> <input type="submit" value="退出登录"> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> 登录失败 </body> </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>退出界面</title> </head> <body> <% session.invalidate(); response.setHeader("refresh", "0;url=Interface.jsp"); %> </body> </html>
2.购物车
和上一题一起,在main.jsp中做一个购物车,里面显示3个商品名和价格 每一个后面有一个加入购物车按钮,main.jsp中有一个按钮(或者超链接)可以显示购物车。(选作:在购物车中加删除按钮删除商品)
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <form action="eight05..jsp" method="post"> 衣服         价格:50 <input type="checkbox" value="衣服" name="sg" />加入购物车<br /> 鞋子       价格:20 <input type="checkbox" value="鞋子" name="sg"/>加入购物车<br /> 帽子    价格:5.5 <input type="checkbox" value="帽子" name="sg" />加入购物车<br /> <input type="submit" value="购买"/> </form> </script>
<form action="1.1.jsp" method="post" > <input type="submit" value="退出登录" /> </form>
</body> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String itemName[]=request.getParameterValues("gwc"); if(itemName==null){ out.print("未添加购物车"); }else{ out.print("您购买的商品为:"+"<br/>"); for(int i=0;i<itemName.length;i++){ out.print("("+(i+1)+")"+itemName[i]+"<br>"); } } %> <a href="eight05.jsp">返回</a> </body> </html>