jsp唯美登录注册界面代码
时间: 2023-10-01 16:06:56 浏览: 32
以下是一个简单的 JSP 登录/注册界面的代码示例,包括 HTML、CSS 和 JSP:
HTML:
```html
<!DOCTYPE html>
<html>
<head>
<title>Login/Register</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container">
<h1>Login</h1>
<form action="login.jsp" method="POST">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<input type="submit" value="Login">
</form>
<hr>
<h1>Register</h1>
<form action="register.jsp" method="POST">
<label for="new_username">Username:</label>
<input type="text" id="new_username" name="new_username" required>
<label for="new_password">Password:</label>
<input type="password" id="new_password" name="new_password" required>
<label for="confirm_password">Confirm Password:</label>
<input type="password" id="confirm_password" name="confirm_password" required>
<input type="submit" value="Register">
</form>
</div>
</body>
</html>
```
CSS:
```css
.container {
margin: 0 auto;
width: 50%;
text-align: center;
}
form {
display: inline-block;
margin-top: 20px;
}
label {
display: block;
margin-top: 10px;
}
input[type=text], input[type=password] {
padding: 10px;
margin-top: 5px;
margin-bottom: 15px;
border: none;
background-color: #f1f1f1;
}
input[type=submit] {
background-color: #4CAF50;
color: white;
padding: 12px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
margin-top: 10px;
}
input[type=submit]:hover {
background-color: #45a049;
}
hr {
border: 1px solid #f1f1f1;
margin-top: 50px;
margin-bottom: 50px;
}
```
JSP - Login:
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ 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/test", "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("<h2>Login Successful!</h2>");
out.println("<p>Welcome, " + username + "!</p>");
} else {
out.println("<h2>Login Failed!</h2>");
out.println("<p>Invalid username or password.</p>");
}
} catch(SQLException se) {
se.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
} catch(SQLException se) {
se.printStackTrace();
}
}
%>
```
JSP - Register:
```jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<%
String new_username = request.getParameter("new_username");
String new_password = request.getParameter("new_password");
String confirm_password = request.getParameter("confirm_password");
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
pstmt = conn.prepareStatement("SELECT * FROM users WHERE username=?");
pstmt.setString(1, new_username);
rs = pstmt.executeQuery();
if(rs.next()) {
out.println("<h2>Registration Failed!</h2>");
out.println("<p>Username already exists.</p>");
} else if(!new_password.equals(confirm_password)) {
out.println("<h2>Registration Failed!</h2>");
out.println("<p>Passwords do not match.</p>");
} else {
pstmt = conn.prepareStatement("INSERT INTO users (username, password) VALUES (?, ?)");
pstmt.setString(1, new_username);
pstmt.setString(2, new_password);
pstmt.executeUpdate();
out.println("<h2>Registration Successful!</h2>");
out.println("<p>Welcome, " + new_username + "!</p>");
}
} catch(SQLException se) {
se.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(conn!=null) conn.close();
} catch(SQLException se) {
se.printStackTrace();
}
}
%>
```
请注意,这只是一个简单的示例,可能不适用于生产环境。在实际使用中,您应该将密码存储为安全的哈希值,并实现更多的安全措施。