欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-04-06 19:44:48
本文实例为大家分享了jsp+servlet实现简单登录注册查询的具体代码,供大家参考,具体内容如下1、注册功能:制作一个注册页面用户输入:用户名密码年龄注册成功:——>跳转至登录页面进行登录注册...

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

1、注册功能:

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

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

2、简易查询:

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

演示

1.启动进入登陆页面

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

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

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

失败则提示

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

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

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

登录失败显示提示信息

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

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

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

代码

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

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>

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

相关标签: jsp 登录 注册