javaweb登录注册拦截器数据库连接增删改查代码
时间: 2023-10-15 08:02:25 浏览: 106
基于Javaweb实现的登录、注册、验证码、拦截器filter、增删改查功能源码+sql数据库(课程设计).zip
5星 · 资源好评率100%
好的,我为你提供一个简单的示例代码来实现JavaWeb登录注册功能、拦截器、数据库连接以及增删改查操作。请注意,以下代码仅供参考,你还需要根据具体需求和数据库配置进行适当修改。
1. 创建LoginServlet.java:
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
// 验证用户名和密码是否正确,这里省略具体实现
if (验证成功) {
request.getSession().setAttribute("username", username);
response.sendRedirect("home.jsp"); // 登录成功后重定向到首页或其他页面
} else {
response.sendRedirect("login.jsp?error=1"); // 登录失败,重定向到登录页面并传递错误信息
}
}
}
```
2. 创建RegisterServlet.java:
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
// 将用户名和密码保存到数据库,这里省略具体实现
response.sendRedirect("login.jsp"); // 注册成功后重定向到登录页面
}
}
```
3. 创建拦截器(Interceptor)LoginInterceptor.java:
```java
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginInterceptor implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
// 初始化操作
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
String username = (String) request.getSession().getAttribute("username");
if (username == null) {
response.sendRedirect("login.jsp"); // 未登录,重定向到登录页面
} else {
chain.doFilter(request, response); // 已登录,继续执行后续操作
}
}
public void destroy() {
// 销毁操作
}
}
```
4. 连接数据库和增删改查操作的示例代码:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBHelper {
private static final String url = "jdbc:mysql://localhost:3306/database_name";
private static final String username = "your_username";
private static final String password = "your_password";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
public static void closeConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 插入操作示例
public static void insertData(String username, String password) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = getConnection();
String sql = "INSERT INTO user (username, password) VALUES (?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, username);
stmt.setString(2, password);
stmt.executeUpdate();
} finally {
if (stmt != null) {
stmt.close();
}
closeConnection(conn);
}
}
// 查询操作示例
public static void queryData() throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = getConnection();
String sql = "SELECT * FROM user";
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
String username = rs.getString("username");
String password = rs.getString("password");
System.out.println("Username: " + username + ", Password: " + password);
}
} finally {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
closeConnection(conn);
}
}
// 其他的更新和删除操作类似,可以根据需要进行实现
}
```
以上是一个简单的示例代码,你可以根据需求进行适当调整和扩展。希望对你有所帮助!如果还有其他问题,请随时提问。
阅读全文