标签:color att and val reg 图片 insert package 布尔值
1.创建EasyMallweb工程
2.导入原有页面
3.导入原有静态资源
4.导入所以jar包和配置文件
5.导入动态资源文件,并修改配置文件
6开发注册功能
regist.jsp RegistServlet
a.修改RegistServlet
//将数据发送至service层做逻辑处理,所以创建UserService类 UserService userService = new UserService(); //参数传递内容较多,可以封装在Javabean中,所以创建User类来封装数据 User user = new User(0,username,password,nickname,email); try { //把错误注册的信息,当做异常来处理,所以当前方法可以不添加返回值。 userService.registUser(user); } catch (MsgException e) { //获取异常对象身上提供的异常信息,作为页面提示。 request.setAttribute("msg",e.getMessage()); request.getRequestDispatcher("/regist.jsp").forward(request, response); return; }
b.创建UserService类
package com.easymall.service; import com.easymall.dao.UserDao; import com.easymall.domain.User; import com.easymall.exception.MsgException; public class UserService { //访问数据时要使用Dao层,所以创建UserDao类 public UserDao userDao = new UserDao(); /** * 注册用户 * @param user 用户信息 */ public void registUser(User user) { //判断用户名是否存在 boolean flag = userDao.findUserByUsername(user.getUsername()); if(flag){ //用户名存在 //创建自定义异常,作为通知,提示用户名已存在 throw new MsgException("用户名已存在"); }else{ //用户不存在 //添加一条数据 userDao.addUser(user); } } }
c.创建异常类---MsgException
public class MsgException extends RuntimeException { public MsgException(String msg){ super(msg); } }
d.创建JavaBean-User
package com.easymall.domain; //封装用户信息的javabean public class User { private int id; private String username; private String password; private String nickname; private String email; public User(){ } public User(int id, String username, String password, String nickname, String email) { super(); this.id = id; this.username = username; this.password = password; this.nickname = nickname; this.email = email; } 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; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
e,创建UserDao 添加addUser、findUserByUsername两个方法
package com.easymall.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.easymall.domain.User; import com.easymall.utils.JDBCUtils; public class UserDao { /** * 添加一条用户信息 * @param user 用户信息对象 * */ public void addUser(User user) { Connection conn = null; PreparedStatement ps = null; try { conn = JDBCUtils.getConnection(); ps = conn.prepareStatement("insert into user values(null,?,?,?,?)"); ps.setString(1, user.getUsername()); ps.setString(2,user.getPassword()); ps.setString(3, user.getNickname()); ps.setString(4, user.getEmail()); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally{ JDBCUtils.close(conn, ps, null); } } /** * 根据用户名查询用户 * @param username 用户名 * @return 用户名是否存在的布尔值 */ public boolean findUserByUsername(String username) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = JDBCUtils.getConnection(); ps = conn.prepareStatement("select * from user where username=?"); ps.setString(1, username); rs = ps.executeQuery(); if(rs.next()){//为true则用户名已存在,不能注册 return true; }else{//为false则用户名不存在,可以注册 return false; } } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(); }finally{ JDBCUtils.close(conn, ps, rs); } } }
7.开发登录功能
a.修改loginServlet
//传输数据到UserService UserService userService = new UserService(); try { User user = userService.loginUser(username,password); //得到user对象,应该放入session域,保留登录状态 request.getSession().setAttribute("user", user); } catch (MsgException e) { //发生异常则证明,需要向前台页面输出错误提示信息。 request.setAttribute("msg", e.getMessage()); request.getRequestDispatcher("/login.jsp").forward(request, response); return; }
b修改UserService
/** * 登录功能 * @param username 用户名 * @param password 密码 * @return 返回用户信息对象(放入session域时使用) */ public User loginUser(String username, String password) { //查询到结果返回user //没有查询结果返回null User user = userDao.findUserByUsernameAndPassword(username,password); if(user == null){//用户名和密码不匹配,使用异常信息,作出提示 throw new MsgException("用户名或密码不正确"); }else{//用户名和密码匹配,返回user对象 return user; } }
c.修改UserDao
/** * 根据用户名和密码查询用户 * @param username 用户名 * @param password 密码 * @return 用户信息对象或null */ public User findUserByUsernameAndPassword(String username, String password) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = JDBCUtils.getConnection(); ps = conn.prepareStatement("select * from user where username=? and password=?"); ps.setString(1,username); ps.setString(2, password); rs = ps.executeQuery(); if(rs.next()){//判断为true,则证明用户名和密码正确,返回一个user对象 User user = new User(); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); user.setNickname(rs.getString("nickname")); user.setEmail(rs.getString("Email")); return user; }else{//判断为false,则证明用户名和密码不正确,返回null return null; } } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(); }finally{ JDBCUtils.close(conn, ps, rs); } }
d.
<c:if test="${sessionScope.user != null }"> <a href="#">欢迎${sessionScope.user.username},回来</a> | <a href="${pageContext.request.contextPath}/LogOutServlet">注销</a> </c:if> <c:if test="${sessionScope.user ==null }"> <a href="${pageContext.request.contextPath}/login.jsp">登录</a> | <a href="${pageContext.request.contextPath}/regist.jsp">注册</a> </c:if>
标签:color att and val reg 图片 insert package 布尔值
原文地址:https://www.cnblogs.com/xuwangqi/p/11379358.html