JSP(JavaServer Pages)是一种动态网页技术标准,它允许开发者将Java代码嵌入到HTML页面中,从而实现动态内容的生成。JSP主要用于服务器端的页面渲染,可以与Servlet、JavaBean等技术结合使用,构建完整的Web应用程序。
首先,需要在数据库中创建用户表和留言表。
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
);
CREATE TABLE messages (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
);<form action="registerProcess.jsp" method="post">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Register">
</form><%@ page import="java.sql.*" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
// 简单的密码加密
String hashedPassword = BCrypt.hashpw(password, BCrypt.gensalt());
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdb", "root", "password");
String sql = "INSERT INTO users(username, password) VALUES(?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, hashedPassword);
pstmt.executeUpdate();
conn.close();
response.sendRedirect("login.jsp");
} catch (Exception e) {
e.printStackTrace();
out.println("Registration failed!");
}
%><form action="loginProcess.jsp" method="post">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form><%@ page import="java.sql.*" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdb", "root", "password");
String sql = "SELECT * FROM users WHERE username = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
if (BCrypt.checkpw(password, rs.getString("password"))) {
session.setAttribute("user", username);
response.sendRedirect("message.jsp");
} else {
out.println("Invalid password!");
}
} else {
out.println("User not found!");
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
out.println("Login failed!");
}
%><%
String user = (String) session.getAttribute("user");
if (user == null) {
response.sendRedirect("login.jsp");
}
%>
<form action="messageProcess.jsp" method="post">
Message: <textarea name="content"></textarea><br>
<input type="submit" value="Submit">
</form><%@ page import="java.sql.*" %>
<%
String user = (String) session.getAttribute("user");
String content = request.getParameter("content");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdb", "root", "password");
String sql = "INSERT INTO messages(user_id, content) VALUES((SELECT id FROM users WHERE username = ?), ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user);
pstmt.setString(2, content);
pstmt.executeUpdate();
conn.close();
response.sendRedirect("message.jsp");
} catch (Exception e) {
e.printStackTrace();
out.println("Message submission failed!");
}
%>原因:数据库服务器未启动、URL配置错误、用户名密码错误等。 解决方法:检查数据库服务状态,确认URL、用户名和密码的正确性。
原因:直接拼接SQL语句,未使用预编译语句。
解决方法:使用PreparedStatement代替Statement,避免SQL注入。
原因:未正确设置或检查会话变量。 解决方法:确保在需要的地方设置会话属性,并在访问前进行有效性检查。
通过以上步骤和注意事项,可以实现一个基本的JSP注册登录留言系统。在实际开发中,还需考虑更多的安全性和性能优化措施。
没有搜到相关的文章