JSP(JavaServer Pages)是一种用于创建动态Web内容的技术,它允许开发者将Java代码嵌入到HTML页面中,从而实现服务器端的动态处理。下面是一个简单的JSP登录注册系统的源代码示例。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2>
<form action="loginProcess.jsp" method="post">
Username: <input type="text" name="username"><br><br>
Password: <input type="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
</body>
</html><%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Register</title>
</head>
<body>
<h2>Register</h2>
<form action="registerProcess.jsp" method="post">
Username: <input type="text" name="username"><br><br>
Password: <input type="password" name="password"><br><br>
<input type="submit" value="Register">
</form>
</body>
</html><%@ page import="java.sql.*" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password");
pstmt = conn.prepareStatement("SELECT * FROM users WHERE username=? AND password=?");
pstmt.setString(1, username);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if (rs.next()) {
out.println("Login successful!");
} else {
out.println("Invalid username or password.");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try { rs.close(); } catch (Exception e) {}
try { pstmt.close(); } catch (Exception e) {}
try { conn.close(); } catch (Exception e) {}
}
%><%@ page import="java.sql.*" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password");
pstmt = conn.prepareStatement("INSERT INTO users (username, password) VALUES (?, ?)");
pstmt.setString(1, username);
pstmt.setString(2, password);
pstmt.executeUpdate();
out.println("Registration successful!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try { pstmt.close(); } catch (Exception e) {}
try { conn.close(); } catch (Exception e) {}
}
%>希望这些信息对你有所帮助。如果有更多具体问题,欢迎继续提问。
没有搜到相关的文章