jsp登录注册页面连接数据库
时间: 2024-06-18 09:04:28 浏览: 18
JSP是Java Server Pages的缩写,它是一种动态网页开发技术,可以将Java代码嵌入到HTML网页中。在JSP中连接数据库实现登录注册功能,需要先配置数据库连接,然后编写JSP页面和相应的Java代码实现功能。以下是一个简单的示例:
1. 配置数据库连接
在WEB-INF目录下创建一个名为web.xml的文件,并在其中添加如下配置信息:
```
<web-app>
<context-param>
<param-name>jdbcDriver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>jdbcUrl</param-name>
<param-value>jdbc:mysql://localhost:3306/mydatabase</param-value>
</context-param>
<context-param>
<param-name>jdbcUser</param-name>
<param-value>root</param-value>
</context-param>
<context-param>
<param-name>jdbcPassword</param-name>
<param-value>password</param-value>
</context-param>
</web-app>
```
其中,jdbcDriver为JDBC驱动程序类名,jdbcUrl为数据库连接字符串,jdbcUser为数据库用户名,jdbcPassword为数据库密码。
2. 编写登录页面
在WebContent目录下创建一个名为login.jsp的文件,并在其中添加如下代码:
```
<%@ page language="java" import="java.sql.*" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h3>Login Page</h3>
<form method="post" action="login.jsp">
Username: <input type="text" name="username"><br/>
Password: <input type="password" name="password"><br/>
<input type="submit" value="Login">
</form>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username != null && password != null) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName(getServletContext().getInitParameter("jdbcDriver"));
conn = DriverManager.getConnection(
getServletContext().getInitParameter("jdbcUrl"),
getServletContext().getInitParameter("jdbcUser"),
getServletContext().getInitParameter("jdbcPassword"));
pstmt = conn.prepareStatement(
"SELECT * FROM users WHERE username=? AND password=?");
pstmt.setString(1, username);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if(rs.next()) {
session.setAttribute("username", username);
response.sendRedirect("welcome.jsp");
} else {
out.println("Invalid username or password");
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if(rs != null) try { rs.close(); } catch(SQLException e) {}
if(pstmt != null) try { pstmt.close(); } catch(SQLException e) {}
if(conn != null) try { conn.close(); } catch(SQLException e) {}
}
}
%>
</body>
</html>
```
其中,从页面获取用户名和密码,然后使用PreparedStatement查询数据库中的用户表,如果查询到匹配的记录,则将用户名保存到session中,并重定向到welcome.jsp页面,否则提示“Invalid username or password”。
3. 编写注册页面
在WebContent目录下创建一个名为register.jsp的文件,并在其中添加如下代码:
```
<%@ page language="java" import="java.sql.*" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<html>
<head>
<title>Register Page</title>
</head>
<body>
<h3>Register Page</h3>
<form method="post" action="register.jsp">
Username: <input type="text" name="username"><br/>
Password: <input type="password" name="password"><br/>
<input type="submit" value="Register">
</form>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username != null && password != null) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName(getServletContext().getInitParameter("jdbcDriver"));
conn = DriverManager.getConnection(
getServletContext().getInitParameter("jdbcUrl"),
getServletContext().getInitParameter("jdbcUser"),
getServletContext().getInitParameter("jdbcPassword"));
pstmt = conn.prepareStatement(
"INSERT INTO users (username, password) VALUES (?, ?)");
pstmt.setString(1, username);
pstmt.setString(2, password);
pstmt.executeUpdate();
session.setAttribute("username", username);
response.sendRedirect("welcome.jsp");
} catch(Exception e) {
e.printStackTrace();
} finally {
if(pstmt != null) try { pstmt.close(); } catch(SQLException e) {}
if(conn != null) try { conn.close(); } catch(SQLException e) {}
}
}
%>
</body>
</html>
```
其中,从页面获取用户名和密码,然后使用PreparedStatement向数据库中的用户表插入一条新记录,如果插入成功,则将用户名保存到session中,并重定向到welcome.jsp页面。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)