用Java和jsp语言实现:只有从登陆界面,登陆成功后,才可以看到显示界面,不能直接看显示界面
时间: 2024-03-10 07:44:47 浏览: 89
好的,那我们可以使用Java Web技术来实现这样的需求。
1. 前端页面
我们需要先创建一个登录页面login.jsp,用户在该页面输入用户名和密码进行登录,代码示例如下:
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<h1>Login Page</h1>
<form action="login" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
```
2. 后端Java代码实现
我们需要创建一个Servlet来处理用户提交的登录请求并进行验证,如果验证通过,则跳转到显示页面;否则,跳转回登录页面。代码示例如下:
```
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 LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String username = "admin";
private String password = "password";
public LoginServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (this.username.equals(username) && this.password.equals(password)) {
HttpSession session = request.getSession();
session.setAttribute("username", username);
response.sendRedirect("display.jsp");
} else {
response.sendRedirect("login.jsp");
}
}
}
```
3. 显示页面
我们需要创建一个显示页面display.jsp,该页面只有在登录成功后才能访问。代码示例如下:
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Display Page</title>
</head>
<body>
<h1>Welcome, <%=session.getAttribute("username")%>!</h1>
<p>This is the display page.</p>
</body>
</html>
```
4. 配置web.xml
我们需要在web.xml中配置Servlet和登录验证过滤器。代码示例如下:
```
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/display.jsp</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
```
5. 配置登录验证过滤器
我们需要创建一个登录验证过滤器LoginFilter,用于对未登录用户进行拦截,只有登录成功后才能访问显示页面。代码示例如下:
```
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginFilter implements Filter {
public void init(FilterConfig fConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
HttpSession session = req.getSession(false);
if (session == null || session.getAttribute("username") == null) {
resp.sendRedirect("login.jsp");
} else {
chain.doFilter(request, response);
}
}
public void destroy() {
}
}
```
以上就是使用Java和jsp语言实现,只有从登陆界面,登陆成功后,才可以看到显示界面,不能直接看显示界面 的方法。
阅读全文