Java web:用户登录与登出
来自CloudWiki
任务分析
根据用户登录登出的功能概述,在商城首页index.jsp用户输入用户名和密码后提交表单,将表单提交的信息存放在request对象中,进入doLogin.jsp页面后,获取表单传递的参数后进行用户验证处理,验证通过后将用户登陆信息保存在session中,返回index.jsp页面进行显示。
任务完成
用户登陆表单
在开发中试用basePath能够很好的避免出现路径的问题。basepath可称作web全路径。 页面中加了basepath,无论当前页面处于什么目录路径下,如manage/index.jsp,指的是web根目录下的manage文件夹下index.jsp。图片、链接引用的路径都是从当前应用程序的根路径开始的!如果没有basepath,写的路径是表示从相对于当前页面的路径,manage/index.jsp指的是当前目录下manage文件夹下的index.jsp
login.jsp:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <html> <head> <style type="text/css"> #layer2{width:400px; height:auto; padding:30px; margin:30px auto 0; background:#ddf; border-radius:8px;} h3{font-size:16pt; color:#a00; text-align:center;} #login { width:70%;margin:0 auto; } #login td { height:40px; } #login .col1{ width:35%;text-align:center; } #login .col2{ text-align:center; } #login .col3{ text-align:center; } </style> </head> <body> <div id="layer2"> <h3>用户登录页面</h3> <form method="post" action="<%= path %>/doLogin.jsp"> <table id="login"> <tr> <td class="col1">用户名</td> <td class="col2"><input type="text" name="uName"/></td> </tr> <tr> <td class="col1">密码</td> <td class="col2"><input type="password" name="pass" /></td> </tr> <tr> <td colspan="2" class="col3"><input type="submit" value=" 提 交 " /> <input type="submit" value=" 取消 " /></td> </tr> </table> </form> </div> </body> </html>
doLogin.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> <p>Success !</p> </body> </html>
创建JavaBean
创建包model,在src中右击New->Package,在弹出的对话框中输入要创建的包名,点击finish完成创建。
创建JavaBean,在刚刚新建的包中右击,在弹出的菜单中选择new ->class ,在弹出的对话框中输入类名User,点击Finish完成创建。
User代码如下:
package model; public class User { private int id;// 会员标识号 private String userName;//会员姓名 private String password;//会员密码 public User() { super(); // TODO Auto-generated constructor stub } public User(int id, String userName, String password) { super(); this.id = id; this.userName = userName; this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
接受参数进行用户验证
修改doLogin.jsp如下:
<%@ page language="java" import="java.util.*,model.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String username = ""; String password = ""; String loginInfo=""; request.setCharacterEncoding("utf-8"); username = request.getParameter("uName"); password = request.getParameter("pass"); if(username.equals("tom")){ User user = new User(); user.setUserName(username); user.setPassword(password); session.setAttribute("user",user); response.addCookie(new Cookie("userName",username)); response.addCookie(new Cookie("password",password)); loginInfo = "登陆成功!"; response.sendRedirect("index.jsp"); }else{ loginInfo = "登陆失败!"; } %> <!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> <%= loginInfo %> </body> </html>