目标:完成用户注册和登录功能 要求: 1:)用户访问/hello(HelloServlet)的时候,判断用户的登录状态,如果已经登录,通过servlet中的重定向功能,跳转到hello.jsp页面,显示“用户名”欢迎您,如“张三欢迎您!”;如果用户未登录,请跳转到“login.jsp”让用户登录。 2)在login.jsp页面中允许用户输入账号密码(只有账号密码两个字段),点击登录后,将表单数据发送到/login(LoginServlet),在LoginServlet中判断用户输入的账号密码是否正确,如果正确跳转到hello.jsp页面;如果错误,跳转到login.jsp页面,并在页面中显示登录错误的原因,如“账号错误/密码错误”。 3)login.jsp登录页面包含“注册”链接,允许用户进行注册操作,当用户点击注册链接时,跳转到register.jsp页面。 3)register.jsp页面中,用户注册时只需要填写账号和密码两个字段,将注册信息提交到/register(RegisterServlet),如果用户输入的账号密码为空或者填写账号已经存在,跳转回register.jsp页面,并显示错误提示;如果账号密码不为空并且账号不存在,将用户的账号名和密码存入到list中,将账号名放入session存储,页面跳转到success.jsp页面,显示注册成功,3秒钟后自动跳转到登录页面。 4)请在程序编写过程中,创建UserBean,用于存储user信息。
时间: 2023-07-16 12:13:15 浏览: 100
创建HelloServlet-Servlet详谈
实现以上功能需要编写以下几个文件:
1. UserBean.java:用于存储用户信息,包括用户名和密码。
```java
public class UserBean {
private String username;
private String password;
public UserBean(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
```
2. HelloServlet.java:判断用户登录状态,如果已经登录,跳转到hello.jsp页面,否则跳转到login.jsp页面。
```java
public class HelloServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
UserBean user = (UserBean) session.getAttribute("user");
if (user != null) {
response.sendRedirect("/hello.jsp");
} else {
response.sendRedirect("/login.jsp");
}
}
}
```
3. LoginServlet.java:处理登录请求,判断用户输入的账号密码是否正确,如果正确跳转到hello.jsp页面;如果错误,跳转到login.jsp页面。
```java
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
List<UserBean> userList = (List<UserBean>) getServletContext().getAttribute("userList");
for (UserBean user : userList) {
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
HttpSession session = request.getSession();
session.setAttribute("user", user);
response.sendRedirect("/hello.jsp");
return;
}
}
request.setAttribute("error", "账号错误/密码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
```
4. RegisterServlet.java:处理注册请求,将用户的账号名和密码存入到list中,将账号名放入session存储,页面跳转到success.jsp页面。
```java
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
List<UserBean> userList = (List<UserBean>) getServletContext().getAttribute("userList");
for (UserBean user : userList) {
if (user.getUsername().equals(username)) {
request.setAttribute("error", "账号已存在");
request.getRequestDispatcher("/register.jsp").forward(request, response);
return;
}
}
if (username.isEmpty() || password.isEmpty()) {
request.setAttribute("error", "账号密码不能为空");
request.getRequestDispatcher("/register.jsp").forward(request, response);
return;
}
UserBean user = new UserBean(username, password);
userList.add(user);
HttpSession session = request.getSession();
session.setAttribute("user", user);
response.sendRedirect("/success.jsp");
}
}
```
5. login.jsp:登录页面,允许用户输入账号密码,点击登录后将表单数据发送到LoginServlet处理。
```html
<form action="/login" method="post">
<div>
<label>账号:</label>
<input type="text" name="username">
</div>
<div>
<label>密码:</label>
<input type="password" name="password">
</div>
<div>
<input type="submit" value="登录">
</div>
<div>
<a href="/register.jsp">注册</a>
</div>
<div>
${error}
</div>
</form>
```
6. register.jsp:注册页面,允许用户填写账号密码,点击注册后将表单数据发送到RegisterServlet处理。
```html
<form action="/register" method="post">
<div>
<label>账号:</label>
<input type="text" name="username">
</div>
<div>
<label>密码:</label>
<input type="password" name="password">
</div>
<div>
<input type="submit" value="注册">
</div>
<div>
${error}
</div>
</form>
```
7. success.jsp:注册成功页面,显示注册成功信息,3秒钟后自动跳转到登录页面。
```html
<p>注册成功!</p>
<script>
setTimeout(function () {
window.location.href = "/login.jsp";
}, 3000);
</script>
```
最后,在web.xml文件中配置各个servlet和jsp页面的访问路径。同时,在ServletContextListener中初始化用户列表。
```xml
<web-app>
<listener>
<listener-class>com.example.UserListListener</listener-class>
</listener>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.example.HelloServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.example.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>com.example.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
```
在ServletContextListener中初始化用户列表。
```java
public class UserListListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
List<UserBean> userList = new ArrayList<>();
sce.getServletContext().setAttribute("userList", userList);
}
public void contextDestroyed(ServletContextEvent sce) {
}
}
```
阅读全文