javaweb登录注册页面
时间: 2023-09-12 11:06:34 浏览: 72
好的,下面是一个简单的 JavaWeb 登录注册页面的示例代码:
### 登录页面(login.jsp)
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<h1>登录页面</h1>
<form action="login" method="post">
<label for="username">用户名:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="登录">
</form>
<p>还没有账号?<a href="register.jsp">点击注册</a></p>
</body>
</html>
```
### 注册页面(register.jsp)
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<h1>注册页面</h1>
<form action="register" method="post">
<label for="username">用户名:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password"><br><br>
<label for="confirm_password">确认密码:</label>
<input type="password" id="confirm_password" name="confirm_password"><br><br>
<input type="submit" value="注册">
</form>
<p>已有账号?<a href="login.jsp">点击登录</a></p>
</body>
</html>
```
### 登录处理(LoginServlet.java)
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
// TODO: 在这里进行登录验证
if (登录验证成功) {
// 将用户名保存在 Session 中
request.getSession().setAttribute("username", username);
// 跳转到首页
response.sendRedirect("index.jsp");
} else {
// 跳转回登录页面并提示错误信息
response.sendRedirect("login.jsp?error=1");
}
}
}
```
### 注册处理(RegisterServlet.java)
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String confirm_password = request.getParameter("confirm_password");
// TODO: 在这里进行注册验证
if (注册验证成功) {
// 将用户名保存在 Session 中
request.getSession().setAttribute("username", username);
// 跳转到首页
response.sendRedirect("index.jsp");
} else {
// 跳转回注册页面并提示错误信息
response.sendRedirect("register.jsp?error=1");
}
}
}
```
以上示例代码仅为演示用途,实际开发中需要根据具体需求进行修改和完善。
阅读全文