使用javaweb写一个新闻发布系统的登录注册页面,不使用xml文件,并在登录后页面跳转至新闻发布系统主页面。
时间: 2023-07-16 16:14:23 浏览: 57
以下是一个简单的登录注册页面的示例代码,使用Java Servlet和JSP技术实现。这个示例没有使用XML文件,而是将用户信息保存在一个Java类中。
首先,我们需要创建一个User类,用于保存用户信息(用户名和密码):
```java
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return this.username;
}
public String getPassword() {
return this.password;
}
}
```
然后,我们需要创建一个Servlet来处理用户的登录和注册请求。在这个示例中,我们将用户信息保存在一个ArrayList中。当用户登录时,我们将遍历该列表以查找匹配的用户。如果找到了匹配的用户,则将用户信息存储在Session对象中,并将用户重定向到主页面。如果未找到匹配的用户,则将用户重定向回登录页面。
```java
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private ArrayList<User> userList = new ArrayList<>();
public LoginServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("login.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = null;
for (User u : userList) {
if (u.getUsername().equals(username) && u.getPassword().equals(password)) {
user = u;
break;
}
}
if (user != null) {
HttpSession session = request.getSession();
session.setAttribute("user", user);
response.sendRedirect("main.jsp");
} else {
response.sendRedirect("login.jsp");
}
}
}
```
最后,我们需要创建一个JSP页面来显示登录和注册表单。在这个示例中,我们使用Bootstrap样式来美化表单。
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Page</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h2>Login</h2>
<form method="post" action="login">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
<hr>
<h2>Register</h2>
<form method="post" action="register">
<div class="form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" id="username" name="username" required>
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<button type="submit" class="btn btn-primary">Register</button>
</form>
</div>
</body>
</html>
```
我们也可以创建一个RegisterServlet来处理用户的注册请求,并将新用户添加到userList中。在这个示例中,我们将用户重定向回登录页面。
```java
@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private ArrayList<User> userList = new ArrayList<>();
public RegisterServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("login.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User(username, password);
userList.add(user);
response.sendRedirect("login.jsp");
}
}
```
这就是一个简单的登录注册页面的实现示例。请注意,此示例仅用于演示目的,并不适用于生产环境。在实际应用中,您需要对用户输入进行验证和安全处理,以避免安全漏洞。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)