目录
登录项目
步骤
1.首先写一个前台页面,一个form表单——login.jsp
2.后台获取前台输入的参数,并作校验
3.写一个JDBC工具类,用来连接数据库,本案例使用Druid连接池
4.写一个登录工具类,在数据库中查询数据,再把数据对象返回到主类中
5.在主类中判断是否能登录成功,成功则跳转页面
注册项目
登录项目
步骤
1.首先写一个前台页面,一个form表单——login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<center>
<h1><%=request.getAttribute("msg")==null?"":request.getAttribute("msg")%></h1>
<h1>登录界面</h1>
<form action="/MyServlet20230624_war_exploded/login">
用户名:
<input type="text" name="username" placeholder="请输入用户名">
<br>
密码:
<input type="password" name="password" placeholder="请输入密码">
<br>
<input type="submit" value="登录">
</form>
</center>
</body>
</html>
效果图:
2.后台获取前台输入的参数,并作校验
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username").trim();
String password = request.getParameter("password").trim();
if(username.isEmpty()||username==""||password.isEmpty()||password==""){
response.getWriter().write("<h1 style='color:red'>用户名或密码为空</h1>");
return;
}
3.写一个JDBC工具类,用来连接数据库,本案例使用Druid连接池
package org.xingyun.utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCUtils {
public static DataSource getDataSource() throws Exception {
Properties properties = new Properties();
InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
properties.load(in);
DataSource ds = DruidDataSourceFactory.createDataSource(properties);
return ds;
}
public static void close(Connection conn, Statement stmt, ResultSet rs) throws SQLException {
if (conn != null) {
conn.close();
}
if (stmt != null) {
stmt.close();
}
if (rs != null) {
rs.close();
}
}
}
4.写一个登录工具类,在数据库中查询数据,再把数据对象返回到主类中
package org.xingyun.utils;
import beans.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class LoginServer {
public static User getLogin(String username,String password) throws Exception {
User user;
Connection conn = JDBCUtils.getDataSource().getConnection();
String sql="select * from user where username=? and password=?";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1,username);
preparedStatement.setString(2,password);
ResultSet rs = preparedStatement.executeQuery();
if (rs.next()==false) {
user=null;
} else {
int id = rs.getInt(1);
String uname = rs.getString(2);
String pwd = rs.getString(3);
User userer = new User(id,uname,pwd);
user=userer;
}
JDBCUtils.close(conn,preparedStatement,rs);
return user;
}
}
5.在主类中判断是否能登录成功,成功则跳转页面
package org.xingyun.demo3;
import beans.User;
import org.xingyun.utils.LoginServer;
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 java.io.IOException;
@WebServlet(name = "LoginServlet",value = "/login")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username").trim();
String password = request.getParameter("password").trim();
if(username.isEmpty()||username==""||password.isEmpty()||password==""){
response.getWriter().write("<h1 style='color:red'>用户名或密码为空</h1>");
return;
}
try {
User user = LoginServer.getLogin(username, password);
if (user != null) {
request.setAttribute("username", user.getUsername()+"已登录");
request.getRequestDispatcher("/main.jsp").forward(request, response);
} else {
request.setAttribute("msg","登录失败,请重新输入");
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
注册项目
package org.xingyun.demo3;
import beans.User;
import org.xingyun.utils.JDBCUtils;
import org.xingyun.utils.LoginServer;
import org.xingyun.utils.RegisterServer;
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 java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
@WebServlet(name = "LoginServlet",value = "/login")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username").trim();
String password = request.getParameter("password").trim();
if(username.isEmpty()||username==""||password.isEmpty()||password==""){
response.getWriter().write("<h1 style='color:red'>用户名或密码为空</h1>");
return;
}
try {
boolean flag = RegisterServer.getRegister(username, password);
if (flag == false) {
Connection conn = JDBCUtils.getDataSource().getConnection();
String sql = "insert into user(username,password) values (?,?)";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
int i = preparedStatement.executeUpdate();
if (i > 0) {
request.setAttribute("msg", "注册成功");
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
} else {
request.setAttribute("msg", "该用户名已注册,请重新注册");
request.getRequestDispatcher("/login.jsp").forward(request,response);
}
} catch (Exception e) {
e.printStackTrace();
}
// 登录操作
// try {
// User user = LoginServer.getLogin(username, password);
// if (user != null) {
// request.setAttribute("username", user.getUsername()+"已登录");
// request.getRequestDispatcher("/main.jsp").forward(request, response);
// } else {
// request.setAttribute("msg","登录失败,请重新输入");
// request.getRequestDispatcher("/login.jsp").forward(request,response);
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}