赞
踩
项目列表:
实现思路:
前台:
el表达式回显用户名和密码以及提示信息
js:用户名和密码的非空判断
后台:
接受参数
非空判断
空:将不为空的参数以及提示信息存到作用域中
请求转发跳转到login.jsp
JDBC通过用户名查询数据库中的用户信息 user
判断user是否为空
空:将用户名和密码以及提示信息存到域对象中
请求转发跳转到login.jsp
判断前台传过来的密码是否和数据库中的密码一致
不一致:将用户名和密码以及提示信息存到域对象中
请求转发跳转到login.jsp
一致:登录成功
将用户信息user存到session中
将用户名和密码存到Cookie中
重定向跳转到index.jsp
login.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>
- </head>
- <body style="text-align: center">
- <form id="loginForm" action="/loginDemo/login" method="post">
- <br/>
- 账户:<input type="text" id="uname" name="uname" value="${uname }"/><br/><br/>
- 密码:<input type="password" id="upwd" name="upwd" value="${upwd }"/><br/>
- <br/>
- <input id="btn" type="button" value="登录" /><br/><br/>
- <span id="error" style="color: red">${msg }</span>
- </form>
- </body>
- <script type="text/javascript" src="/loginDemo/statics/js/jquery-3.4.1.min.js"></script>
- <script type="text/javascript">
- $("#btn").click(function(){
- // 获取用户名和密码
- var uname = $("#uname").val();
- var upwd = $("#upwd").val();
- // 非空判断
- if(uname == "" || upwd == ""){
- $("#error").html("*用户名或密码不能为空*");
- return;
- }
- $("#loginForm").submit();
- });
- </script>
- </html>
index.jsp
- <%@page import="com.mage.po.User"%>
- <%@ 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>
- </head>
- <body style="background-color: black; color: white;text-align: center">
- <h1>欢迎来到好看的首页</h1>
- <img src="statics/${user.head }">
- <h2>用户名:${user.uname }</h2>
- <h2>密码:${user.upwd }</h2>
- <h2>昵称:${user.nick }</h2>
- <%
- User user = (User)session.getAttribute("user");
- if(user != null && user.getSex() == 1){
- out.write("<h2>性别:男</h2>");
- }else if(user != null && user.getSex() != 1){
- out.write("<h2>性别:女</h2>");
- }else{
- out.write("<h2>性别:不详</h2>");
- }
- %>
- <h2>年龄:${user.age }</h2>
- <h2>地址:${user.address }</h2>
- </body>
- </html>
LoginServlet.java
- package com.mage.servlet;
-
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
-
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import com.mage.po.User;
-
- /**
- * 登录
- */
- @WebServlet("/login")
- public class LoginServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
-
- protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // 接受参数
- String uname = request.getParameter("uname");
- String upwd = request.getParameter("upwd");
-
- //对接收的参数进行非空判断(单独判断),uname判断
- if(uname == null || "".equals(uname.trim())){
- // 如果为空,将不为空的参数以及提示信息存到作用域中
- request.setAttribute("upwd", upwd);
- request.setAttribute("msg", "*账户不能为空*");
- // 请求转发跳转到login.jsp
- request.getRequestDispatcher("/login.jsp").forward(request, response);
- return;
- }
- //upwd判断非空
- if(upwd == null || "".equals(upwd.trim())){
- //如果为空,将不为空的参数以及提示信息存到作用域中
- request.setAttribute("uname", uname);
- request.setAttribute("msg", "*密码不能为空*");
- // 请求转发跳转到login.jsp
- request.getRequestDispatcher("/login.jsp").forward(request, response);
- return;
- }
- //不为空进行数据查询,JDBC通过用户明查询数据库中的用户信息 user
- User user = null;
- Connection conn = null;
- PreparedStatement sta = null;
- ResultSet res = null;
- try {
- // 加载驱动
- Class.forName("com.mysql.jdbc.Driver");
- // 建立连接
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/logindemo", "root", "root");
- // 编写sql
- String sql = "select * from user where uname = ?";
- // 预编译
- sta = conn.prepareStatement(sql);
- // 设置参数,下标从1开始
- sta.setString(1, uname);
- // 执行查询,得到结果集
- res = sta.executeQuery();
- // 分析结果集
- while(res.next()){
- user = new User();
- user.setId(res.getInt("id"));
- user.setUname(uname);
- user.setUpwd(res.getString("upwd"));
- user.setNick(res.getString("nick"));
- user.setHead(res.getString("head"));
- user.setSex(res.getInt("sex"));
- user.setAge(res.getInt("age"));
- user.setAddress(res.getString("address"));
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally{
- try {
- if(res!=null){
- res.close();
- }
- if(sta!=null){
- sta.close();
- }
- if(conn!=null){
- conn.close();
- }
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
-
- // 对user对象判断是否为空
- if(user == null){
- // user对象为空时,将用户名和密码以及提示信息存到域对象中
- request.setAttribute("uname", uname);
- request.setAttribute("upwd", upwd);
- request.setAttribute("msg", "*账户不存在*");
- // 请求转发跳转到login.jsp
- request.getRequestDispatcher("/login.jsp").forward(request, response);
- return;
- }
-
-
- // 判断前台传过来的密码是否和数据库中的密码一致
- if(upwd.equals(user.getUpwd())){
- // 一致:登录成功
- // 将用户信息user存到session中
- request.getSession().setAttribute("user", user);
- // 将用户名和密码存到Cookie中
- Cookie cookie = new Cookie("user",uname + "-" +upwd);
- cookie.setMaxAge(7*24*60*60);
- response.addCookie(cookie);
- // 重定向跳转到首页
- response.sendRedirect("/loginDemo/index.jsp");
- return;
- }
- // 不一致:将用户名和密码以及提示信息存到域对象中
- request.setAttribute("uname", uname);
- request.setAttribute("upwd", upwd);
- request.setAttribute("msg", "*用户名或密码错误*");
- // 请求转发跳转到login.jsp
- request.getRequestDispatcher("/login.jsp").forward(request, response);
-
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。