首页 > 编程语言 > Jsp+Servlet实现简单登录注册查询
2020
10-10

Jsp+Servlet实现简单登录注册查询

本文实例为大家分享了Jsp+Servlet实现简单登录注册查询的具体代码,供大家参考,具体内容如下

1、注册功能:

制作一个注册页面
用户输入:

用户名
密码
年龄
注册成功:——>跳转至登录页面进行登录
注册失败:——>文字或其他形式的提示皆可

2、简易查询:

制作一个查询页面
输入用户名
显示该用户的用户名、密码、年龄

演示

1.启动进入登陆页面

2.点击注册,进入注册页面,成功跳转到登录页面

失败则提示

回到登录页面,登录成功进入查询页面

登录失败显示提示信息

输入用户名->显示该用户的用户名、密码、年龄

代码

dao

public class UserDao {
 private Connection conn = null;
 private PreparedStatement ps=null;
 private int result=0;
 private ResultSet rs=null;

 //用户注册
 public int register(User user){
 String sql="insert into users(name,password,age) value (?,?,?)";
 try {
 //获取数据库连接对象
 conn= JDBCUtil.getConnection();
 //获取数据库操作对象
 ps=conn.prepareStatement(sql);
 ps.setString(1,user.getName());
 ps.setString(2,user.getPassword());
 ps.setInt(3,user.getAge());
 //执行sql
 result=ps.executeUpdate();
 } catch (Exception e) {
 e.printStackTrace();
 }finally {
 JDBCUtil.close(null,ps,conn);
 }
 return result;
 }

 //登录验证用户信息
 public int login(String userName,String password){
 String sql ="select count(*) from users where name=? and password=?";
 try {
 conn=JDBCUtil.getConnection();
 ps=conn.prepareStatement(sql);
 ps.setString(1,userName);
 ps.setString(2,password);
 rs=ps.executeQuery();
 while (rs.next()){
 result=rs.getInt("count(*)");
 }
 } catch (Exception e) {
 e.printStackTrace();
 } finally {
 JDBCUtil.close(rs,ps,conn);
 }
 return result;
 }

 //根据用户名 显示用户名、密码、年龄
 public User findByName(String userName){
 String sql="select name,password,age from users where name=?";
 User user = null;
 try {
 conn=JDBCUtil.getConnection();
 ps=conn.prepareStatement(sql);
 ps.setString(1,userName);
 rs=ps.executeQuery();
 while (rs.next()){
 String name = rs.getString("name");
 String password = rs.getString("password");
 int age = rs.getInt("age");
 user = new User(name,password,age);
 }

 } catch (Exception e) {
 e.printStackTrace();
 }finally {
 JDBCUtil.close(null,ps,conn);
 }
 return user;
 }
}

entity 实体类

public class User {
 private int id;
 private String name;
 private String password;
 private int age;
//set...
//get...
//constructor...
}

service

public class UserServiceImpl implements UserService {
 UserDao userDao = new UserDao();
 // 注册
 @Override
 public int register(User user) {
 return userDao.register(user);
 }
 // 登陆
 @Override
 public int login(String userName, String password) {
 return userDao.login(userName,password);
 }

 // 根据用户名查找信息
 @Override
 public User findByName(String userName) {
 return userDao.findByName(userName);
 }
}

servlet

// FindByNameServlet
public class FindByNameServlet extends HttpServlet {
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 String name = request.getParameter("name");

 UserService userService = new UserServiceImpl();
 User user = userService.findByName(name);

 //将查询结果放入request作用域
 request.setAttribute("userInfo",user);
 request.getRequestDispatcher("/jsp/index.jsp").forward(request,response);
 }
}

// LoginServlet
public class LoginServlet extends HttpServlet {
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 //1 获取
 String userName = request.getParameter("userName");
 String password = request.getParameter("password");

 //2 service调用dao对数据库操作
 UserService userService = new UserServiceImpl();
 int result = userService.login(userName, password);

 //3 成功跳转到查询页面,失败跳转到失败页面
 if (result>0){
 response.sendRedirect("/jsp/index.jsp");
 }else{
 response.sendRedirect("/login_error.html");
 }
 }
}
// RegisterServlet
public class RegisterServlet extends HttpServlet {
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 UserService userService = new UserServiceImpl();
 User user = null;
 int result = 0;
 //1【调用请求对象】读取【请求头】参数信息,得到用户注册信息
 String userName, password, age;
 userName = request.getParameter("userName");
 password = request.getParameter("password");
 age = request.getParameter("age");
 user = new User(userName, password, Integer.valueOf(age));
 //2 调用userService——>userDao
 // 先查询用户是否存在
 User byName = userService.findByName(userName);
 if (byName!=null){
 request.setAttribute("info","用户已存在!");
 request.getRequestDispatcher("/jsp/register.jsp").forward(request,response);
 }
 // 注册
 result = userService.register(user);

 //3 设置编码格式,防止乱码
 response.setContentType("text/html;charset=utf-8");
 PrintWriter out = response.getWriter();

 //注册成功:——>跳转至登录页面进行登录
 //注册失败:——>注册页面提示:注册失败
 if (result == 1) {
 response.sendRedirect("/login.html");
 } else {
 request.setAttribute("info","注册失败!");
 request.getRequestDispatcher("/jsp/register.jsp").forward(request,response);
 }
 }
}

JDBCUtil

public class JDBCUtil {
 private JDBCUtil(){}
 //静态代码块在类加载时执行,并且执行一次。
 static{
 try {
 Class.forName("com.mysql.cj.jdbc.Driver");
 } catch (ClassNotFoundException e) {
 e.printStackTrace();
 }
 }
 //获取数据库连接对象
 public static Connection getConnection() throws Exception{
 String url="jdbc:mysql://127.0.0.1:3306/zy?&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
 String user="root";
 String password="rootroot";

 return DriverManager.getConnection(url,user,password);
 }
 /**
 *关闭资源
 * @param conn 连接对象
 * @param ps 数据库操作对象
 * @param rs 结果集
 */
 public static void close(ResultSet rs, Statement ps, Connection conn){
 if (rs != null) {
 try {
 rs.close();
 } catch (SQLException e) {
 e.printStackTrace();
 }
 }
 if (ps != null) {
 try {
 ps.close();
 } catch (SQLException e) {
 e.printStackTrace();
 }
 }
 if (conn != null) {
 try {
 conn.close();
 } catch (SQLException e) {
 e.printStackTrace();
 }
 }
 }
}

index.jsp

<%@ page import="entity.User" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
 <title>查询页面</title>
</head>
<body>
<div align="center">
 <h2/>输入用户名,查询信息
 <form action="/findByName" method="get">
 <input type="text" name="name" id="name">
 <input type="submit" value="查询">
 </form>
 <%
 User userInfo = (User) request.getAttribute("userInfo");
 %>
 <%
 if (userInfo != null) {
 %>
 <table border="3">
 <tr>
 <th>用户名</th>
 <th>密码</th>
 <th>年龄</th>
 </tr>
 <tr>
 <td> &nbsp; &nbsp; <%=userInfo.getName()%> &nbsp; &nbsp;</td>
 <td> &nbsp; &nbsp; <%=userInfo.getPassword()%> &nbsp; &nbsp;</td>
 <td> &nbsp; &nbsp; <%=userInfo.getAge()%> &nbsp; &nbsp;</td>
 </tr>

 </table>
 <%
 }
 %>
</div>
</body>
</html>

register.jsp

<%@ page import="com.mysql.cj.util.StringUtils" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
 <title>Title</title>
</head>
<body>
<br>
<br>
<%
 String info =(String) request.getAttribute("info");
%>
<%
 if (!StringUtils.isNullOrEmpty(info)){
%>
 <h1 style="color: red;text-align: center" ><%=info%></h1>
<%
 }
%>

<div align="center">
 <form action="/register" method="post">
 <table border="2">
 <tr>
 <th>用户名</th>
 <td><input type="text" name="userName"/></td>
 </tr>
 <tr>
 <th>密码</th>
 <td><input type="password" name="password"/></td>
 </tr>
 <tr>
 <th>年龄</th>
 <td><input type="text" name="age"/></td>
 </tr>
 <tr>
 <td colspan="2" align="center">
  <input type="submit" value="注册"/>
  <input type="reset" value="清空"/>
 </td>
 </tr>
 </table>
 </form>
</div>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 version="4.0">
 <servlet>
 <servlet-name>LoginServlet</servlet-name>
 <servlet-class>servlet.LoginServlet</servlet-class>
 </servlet>
 <servlet>
 <servlet-name>RegisterServlet</servlet-name>
 <servlet-class>servlet.RegisterServlet</servlet-class>
 </servlet>
 <servlet>
 <servlet-name>FindByNameServlet</servlet-name>
 <servlet-class>servlet.FindByNameServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>LoginServlet</servlet-name>
 <url-pattern>/login</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>RegisterServlet</servlet-name>
 <url-pattern>/register</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>FindByNameServlet</servlet-name>
 <url-pattern>/findByName</url-pattern>
 </servlet-mapping>

 <!--设置默认欢迎文件规则-->
 <welcome-file-list>
 <welcome-file>login.html</welcome-file> <!--servlet 作为默认欢迎文件 ‘/'需要去掉-->
 </welcome-file-list>
</web-app>

login.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>登陆界面</title>
</head>
<body>
<div align="center">
 <font size="10px" color="#00008b">用户登录</font>
 <form action="/login" method="post">

 <table border="2">
 <tr>
 <th>用户名</th>
 <td><input type="text" name="userName"/></td>
 </tr>
 <tr>
 <th>密码</th>
 <td><input type="password" name="password"/></td>
 </tr>
 <tr>
 <td colspan="2" align="center">
  <input type="submit" value="登录"/>
  <input type="reset" />
 </td>
 </tr>
 </table>
 </form>
 <a href="/jsp/register.jsp" style="text-align: left">立即注册</a>


</div>
</body>
</html>

login_error.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>登录验证</title>
</head>
<body>
<div align="center">
 <font size="10px" color="#00008b">用户登录</font><br>
 <font size="5px" color="red">登录信息不存在,请重新登陆!!!</font>
 <form action="/login" method="post">
 <table border="2">
 <tr>
 <th>用户名</th>
 <td><input type="text" name="userName" /></td>
 </tr>
 <tr>
 <th>密码</th>
 <td><input type="password" name="password" /></td>
 </tr>
 <tr>
 <td colspan="2" align="center">
  <input type="submit" value="登录"/>
  <input type="reset">

 </td>
 </tr>
 </table>
 </form>
 <a href="/jsp/register.jsp" style="text-align: left">立即注册</a>

</div>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧