首页 > 基础资料 博客日记
Java Web实现登录注册(超详细附代码)
2024-07-08 08:00:05基础资料围观502次
4.登录的数据流程
登录操作的主要调用流程图
Login.jsp
LoginServlet
UserDB
第一步
第二步
第三步
第四步
5.部分代码的展示
5.1注册
register.jsp注册页面 通过表单实现跳转到servlet
<meta name=“viewport” content=“width=device-width”,initial->
world message board of the futureRegister
${message}
复制代码
RegisterServlet.java
业务层:处理注册业务
package Sevlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javaBean.User;
import useBean.UserDB;
@WebServlet(“/RegistServlet”)
public class RegistServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
String message =“”;
public RegistServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding(“utf-8”);
//获取隐藏域的信息
String action = request.getParameter(“action”);
String url =“register.jsp”;
if(action.equals(“regist”))
url = registerUser(request,response);
getServletContext().getRequestDispatcher(url).forward(request, response);
}
//注册函数
private String registerUser(HttpServletRequest request,HttpServletResponse response) {
String username = request.getParameter(“username”);
String password = request.getParameter(“password”);
String password2 = request.getParameter(“password2”);
String message = “”;
//得到HttpSession类型的对象
HttpSession session = request.getSession();
//将数据存储于User对象
User user = new User();
user.setusername (username);
user.setpassword(password);
String url = “/login.jsp”;
if(password.equals(password2))
{
if( !UserDB.UserExists(username) ) {
message = “Registration successful! and login in”;
session.setAttribute(“message”, message);
//将注册用户信息写入数据库
UserDB.insert(user);
return url;
}
else
{
message = “The user name already exists”;
url = “/register.jsp”;
session.setAttribute(“message”, message);
return url;
}
}
else
{
message = “The password is inconsistent”;
session.setAttribute(“message”, message);
url = “/register.jsp”;
return url;
}
}
}
复制代码
Dao层实现判断用户名是否存在和插入数据库的方法 UserDB.java
//注册时判断用户名是否存在
public static boolean UserExists(String username) {
//可以建立一个连接池保存一定数量的连接,当有对象需要数据库连接时,直接将这个连接返回给该对象,
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String qr = "SELECT username FROM User "+ “WHERE username = ?”;
try {
ps = connection.prepareStatement(qr);
ps.setString(1, username);
rs = ps.executeQuery();
return rs.next();
} catch (SQLException e) {
System.out.println(e);
return false;
} finally {
DBUtil.closeResultSet(rs);
DBUtil.closePreparedStatement(ps);
pool.freeConnection(connection);
}
}
//将注册用户信息保存至数据库
public static int insert(User user) {
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps = null;
String qr = “INSERT INTO User (username, password)”+“VALUES (?, ?)”;
try {
ps = connection.prepareStatement(qr);
ps.setString(1, user.getusername());
ps.setString(2, user.getpassword());
return ps.executeUpdate();
} catch (SQLException e) {
System.out.println(e);
return 0;
} finally {
DBUtil.closePreparedStatement(ps);
pool.freeConnection(connection);
}
}
复制代码
5.2登录
login.jsp登录界面 通过表单实现跳转到servlet
<meta name=“viewport” content=“width=device-width”,initial->
world message board of the futurelogin
register
${message}
复制代码
LoginServlet.java 业务层:处理登录业务
package Sevlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
最后
金三银四到了,送上一个小福利!
orm>
${message}
复制代码
LoginServlet.java 业务层:处理登录业务
package Sevlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
最后
金三银四到了,送上一个小福利!
[外链图片转存中…(img-yaImbvoz-1714309681690)]
[外链图片转存中…(img-umNcoe1u-1714309681690)]
[外链图片转存中…(img-NRZXbUfw-1714309681691)]
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签: