Javaweb+Mysql 用户管理系统项目(二 登录)

实现登陆功能

userlogin.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录界面</title>
<script type="text/javascript">
	function changeCode(){
		//获取验证码元素
		 var checkcode = document.getElementById("checkcode");
		 checkcode.src="CodeServlet?time="+new Date().getTime();
	}
</script>
</head>
<body>
	<form action="UserLoginServlet" method="post">
		<table align="center" border="1" style="border-collapse:collapse;">
			<tr>
				<td colspan="4" align="center">用户登录</td>
			</tr>
			<tr height="40px">
				<td>用户账户:</td>
				<td colspan="3"><input type="text" name="userAccount" size="20" align="left"></td>
			</tr>
			<tr height="40px">
				<td >用户密码:</td>
				<td colspan="3"><input type="password" name="userPassword" size="20" align="left"></td>
			</tr>
		<!-- </table>
		<table align="center"> -->
			<tr height="40px">
				<td>验证码:</td>
				<!-- <td><input type="text" name="code" size="3" align="left"></td>
				<td><a href="#" onclick="changeCode()">换一换</a></td>
				<td><img id=checkcode  alt="验证码" src="CodeServlet" onclick="changeCode()"/></td>
				 -->
				<td colspan="3">
					<input type="text" name="code" size="3" align="left">
					<a href="#" onclick="changeCode()">换一换</a>
					<img id=checkcode  alt="验证码" src="CodeServlet" onclick="changeCode()"/>
				</td>
			</tr>
		<!-- </table>
		<table align="center">	-->		
			<tr>
				<td  colspan="4" align="center">
					<input type="submit" value="登录">
					<input type="button" value="注册" 
						onclick="window.location.href='userregister.jsp'">
					<input type="button" value="恢复账号" 
						onclick="window.location.href='UserRecoverServlet'">
					<a href="RecoverPWDServlet"><font >忘记密码?</font></a>
				</td>
			</tr>
		</table>
		<table align="center">
			<tr>
				<td align="center"><font color="RED">
				<%
					String UDmsg = (String) request.getAttribute("UDmsg");
					String usermsg = (String) request.getAttribute("usermsg");
					String codemsg = (String) request.getAttribute("codemsg");
					if(UDmsg != null){
						out.println(UDmsg);
					}else if(usermsg != null){
						out.println(usermsg);
					}else if(codemsg != null){
						out.println(codemsg);
					}
				%>
				</font></td>
			</tr>
		</table>
		<br>
	</form>
</body>
</html>

Userdao数据库操作

 /**
       * 判断用户密码是否正确
       */
      public User judgeUserPassword(String userAccount,String userPassword) {
    	  Connection conn = null;
    	  PreparedStatement ps = null;
    	  ResultSet rs = null;
    	  User user = null;
    	  
    	  try {
			conn=DBUtils.getConnection();
			String sql="select * from users where user_account=? and user_password=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1, userAccount);
            ps.setString(2, userPassword);
            rs=ps.executeQuery();
            
            while(rs.next()) {
            	user = new User();
            	user.setUserId(rs.getInt("user_id"));
            	user.setUserName(rs.getString("user_name"));
            	user.setUserAccount(rs.getString("user_account"));
            	user.setUserPassword(rs.getString("user_password"));
            	user.setUserDisplay(rs.getString("user_display"));
            	user.setUserDeleteTime(rs.getString("user_deleteTime"));
            }
    	  } catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DBUtils.closeAll(rs, ps, conn);
		}
    	  return user;
      }

LoginServlet

package com.bin.action;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.soap.AddressingFeature.Responses;

import com.bin.dao.UserDao;
import com.bin.entity.User;

/**
 * Servlet implementation class UserLoginServlet
 */
public class UserLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserLoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=ust-8");
		response.setHeader("pragma", "no-cache");
		response.setHeader("cache-control", "no-cache");
		response.setDateHeader("expires", 0);
		HttpSession session=request.getSession();
		//获取用户表单数据 
		String userAccount=request.getParameter("userAccount");
		String userPassword=request.getParameter("userPassword");
		String code=request.getParameter("code");
		String scode=(String) session.getAttribute("scode");
		//实例化
		UserDao ud=new UserDao();
		User user=null;
		String userDisplay = ud.judgeUserDisplay(userAccount);
		//用户登录验证处理
		if(userDisplay == null){
		String UDmsg = "很抱歉,用户不存在!您或许需要重新注册,谢谢!";
		request.setAttribute("UDmsg", UDmsg);
		request.getRequestDispatcher("userlogin.jsp").forward(request ,response);
		}else if(userDisplay.equals("off")){
		String UDmsg = "很抱歉,用户已被删除!您或许需要找回,谢谢!";
		request.setAttribute("UDmsg", UDmsg);
		request.getRequestDispatcher("userlogin.jsp").forward(request ,response);
		}else if((user=ud.judgeUserPassword(userAccount,userPassword))==null){
		String usermsg = "用户名或密码错误,请确认!";
		request.setAttribute("usermsg", usermsg);
		request.getRequestDispatcher("userlogin.jsp").forward(request ,response);
		}else if(!code.equalsIgnoreCase(scode)){
		String codemsg = "验证码错误,请确认!";
		request.setAttribute("codemsg", codemsg);
		request.getRequestDispatcher("userlogin.jsp").forward(request ,response);
		}else if(user != null){//如果账号密码正确,跳转到用户管理界面
		//向session中添加user对象
		String userAccount2 = user.getUserAccount();
		session.setAttribute("userAccount", userAccount2);
		session.setAttribute("user", user);
		//获取用户列表
		ArrayList<User> list = ud.selectNotArrayList();
		request.setAttribute("list", list);
		//转发到用户管理界面
		request.getRequestDispatcher("userlist.jsp").forward(request ,response);
		//String path1 = resp.encodeURL(req.getContextPath()+"/userlist.jsp");
		//resp.setHeader("refresh", "1;url="+path1);
		//resp.sendRedirect(path1);
		}
		}
	}



你可能感兴趣的:(Javaweb+Mysql 用户管理系统项目(二 登录))