用JSP+Servlet写一个登录注册页面,login.jsp,register.jsp loginServlet
时间: 2024-01-22 13:17:28 浏览: 90
这里提供一个基本的登录注册页面的实现,其中使用JSP作为前端页面,Servlet作为后台处理程序。
1. 创建一个名为 "login.jsp" 的文件,编写登录页面的HTML代码。
```html
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h1>Login</h1>
<form action="loginServlet" method="POST">
<label>Username: </label>
<input type="text" name="username"><br><br>
<label>Password: </label>
<input type="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
<p>New User? <a href="register.jsp">Register here</a></p>
</body>
</html>
```
2. 创建一个名为 "register.jsp" 的文件,编写注册页面的HTML代码。
```html
<!DOCTYPE html>
<html>
<head>
<title>Registration Page</title>
</head>
<body>
<h1>Registration</h1>
<form action="registerServlet" method="POST">
<label>Username: </label>
<input type="text" name="username"><br><br>
<label>Password: </label>
<input type="password" name="password"><br><br>
<label>Confirm Password: </label>
<input type="password" name="confirmPassword"><br><br>
<input type="submit" value="Register">
</form>
<p>Already registered? <a href="login.jsp">Login here</a></p>
</body>
</html>
```
3. 创建一个名为 "LoginServlet.java" 的Servlet文件,用于处理登录请求。
```java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username.equals("admin") && password.equals("1234")) {
HttpSession session = request.getSession();
session.setAttribute("username", username);
response.sendRedirect("welcome.jsp");
} else {
out.println("Incorrect username or password.");
}
}
}
```
4. 创建一个名为 "RegisterServlet.java" 的Servlet文件,用于处理注册请求。
```java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
String confirmPassword = request.getParameter("confirmPassword");
if(password.equals(confirmPassword)) {
HttpSession session = request.getSession();
session.setAttribute("username", username);
response.sendRedirect("welcome.jsp");
} else {
out.println("Passwords do not match.");
}
}
}
```
5. 创建一个名为 "welcome.jsp" 的文件,用于显示欢迎信息。
```html
<!DOCTYPE html>
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<h1>Welcome <%= session.getAttribute("username") %>!</h1>
<p>You have successfully logged in.</p>
<a href="logoutServlet">Logout</a>
</body>
</html>
```
6. 创建一个名为 "LogoutServlet.java" 的Servlet文件,用于处理注销请求。
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LogoutServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.invalidate();
response.sendRedirect("login.jsp");
}
}
```
以上就是一个基本的登录注册页面的实现。需要注意的是,这里只是提供了一个简单的示例,实际的应用中还需要加入更多的安全性和验证机制。
阅读全文