第八周Jsp作业
1.登陆
输入用户名密码,判断用户名和密码相同,登陆成功,session中保存用户的用户名,进入主页main.jsp,
主页有一个退出按钮,点击,回到登陆页login.jsp。要求:退出登录后,如果在浏览器直接输入主页main.jsp,
访问不了,直接跳到登陆页。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% 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="dologin.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+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> </head> <% request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); %> <body> <% request.setCharacterEncoding("utf-8"); String uname = request.getParameter("uname"); String password = request.getParameter("pas"); if (uname.equals(password)) { session.setAttribute("uname", uname); request.getRequestDispatcher("main.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> <head> </head> <body> <% session.invalidate(); response.setHeader("refresh", "5;url=index.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> <head> </head> <body> <% String uname=(String)session.getAttribute("uname"); //如果他是空,说明没登陆,直接访问该页面了 if(uname==null) response.sendRedirect("index.jsp"); %> 欢迎你<%=uname %> <a href="exit.jsp">退出登录</a> <br> </body> </html>
2.购物车
和上一题一起,在main.jsp中做一个购物车,里面显示3个商品名和价格 每一个后面有一个加入购物车按钮,
main.jsp中有一个按钮(或者超链接)可以显示购物车。(选作:在购物车中加删除按钮删除商品)