当前位置:网站首页>Complete web login process through filter
Complete web login process through filter
2022-07-06 10:19:00 【Artificial intelligence cockroach】
Complete web login process through filter
frame
Only users who get tickets can log in successfully , Tickets will be confiscated upon cancellation

Filter
effect :
Deal with Chinese code
validate logon ….
Add to server and servlet Between , Used to filter requests and responses ;
Guide pack

Specific code
The login page index.jsp
<html>
<body>
<h2> The login page </h2>
<div style="text-align:center">
<%--${pageContext.request.contextPath} Represents the current project web route --%>
<%--method="post" Set request method --%>
<form action="${pageContext.request.contextPath}/login" method="post">
<%@ page pageEncoding="utf-8"%>
user name :<input type="text" name="username"> <br>
password :<input type="password" name="password"><br>
<input type="submit">
</form>
</div>
</body>
</html>
error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> error </title>
</head>
<body>
<h2> The user name does not exist or the password is wrong !</h2>
<a href="/s1/index.jsp"> Return to login page </a>
</body>
</html>
success.jsp
success.jsp Put it in web/sys/
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title> A page </title>
</head>
<body>
<h1> Home page </h1>
<h2 Login successful style="text-align:center"></h2>
<%-- Set button --%>
<a href="../logout"> Cancellation account </a>
</body>
</html>
Sign in Servlet Login
package com.tl.login;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/** * @author tl * Realize page login function : Only by entering the correct password can you login successfully and get tickets , * Otherwise, redirect to error.jsp page */
public class Login extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username=req.getParameter("username");
String password=req.getParameter("password");
if(username.equals("admin")&&password.equals("123456")){
req.getSession().setAttribute(Constant.name,req.getSession().getId());
resp.sendRedirect("/s1/sys/success.jsp");
}
else {
resp.sendRedirect("/s1/error.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
filter LoginFilter
package com.tl.login;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/** * @author tl * filter Filter requests that don't get tickets , Respond to them on the login page */
public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest) request;
HttpServletResponse resp=(HttpServletResponse) response;
if(req.getSession().getAttribute(Constant.name)==null){
resp.sendRedirect("/s1/index.jsp");
}
// Current
chain.doFilter(request,response);
}
@Override
public void destroy() {
}
}
Cancellation Logout
package com.tl.login;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/** * @author tl * After the login page, you can log out Call this class when logging out * Confiscate the user's ticket */
public class Logout extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if(req.getSession().getAttribute(Constant.name)!=null){
// Confiscate tickets
req.getSession().removeAttribute(Constant.name);
resp.sendRedirect("/s1/index.jsp");
}
else{
resp.sendRedirect("/s1/index.jsp");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
Constant class Constant
package com.tl.login;
/** * @author tl * Define constants here */
public class Constant {
static String name="USER_NAME";
}
register web.xml
Filter through filter /s1/sys/* Web page request and response
<?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_4_0.xsd" version="4.0" metadata-complete="true">
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>com.tl.login.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Logout</servlet-name>
<servlet-class>com.tl.login.Logout</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Logout</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>com.tl.login.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<url-pattern>/sys/*</url-pattern>
</filter-mapping>
</web-app>
边栏推荐
- Implement context manager through with
- History of object recognition
- MySQL combat optimization expert 04 uses the execution process of update statements in the InnoDB storage engine to talk about what binlog is?
- Inject common SQL statement collation
- Not registered via @EnableConfigurationProperties, marked(@ConfigurationProperties的使用)
- 美新泽西州州长签署七项提高枪支安全的法案
- 宝塔的安装和flask项目部署
- CAPL script printing functions write, writeex, writelineex, writetolog, writetologex, writedbglevel do you really know which one to use under what circumstances?
- Combined search /dfs solution - leetcode daily question - number of 1020 enclaves
- Implement sending post request with form data parameter
猜你喜欢

C miscellaneous dynamic linked list operation

MySQL combat optimization expert 12 what does the memory data structure buffer pool look like?

Canoe cannot automatically identify serial port number? Then encapsulate a DLL so that it must work

Write your own CPU Chapter 10 - learning notes

A necessary soft skill for Software Test Engineers: structured thinking

Installation of pagoda and deployment of flask project

UEditor国际化配置,支持中英文切换

高并发系统的限流方案研究,其实限流实现也不复杂
![[CV] target detection: derivation of common terms and map evaluation indicators](/img/e8/04cc8336223c0ab2dea5638def88df.jpg)
[CV] target detection: derivation of common terms and map evaluation indicators

Routes and resources of AI
随机推荐
在CANoe中通过Panel面板控制Test Module 运行(初级)
MySQL实战优化高手03 用一次数据更新流程,初步了解InnoDB存储引擎的架构设计
[Julia] exit notes - Serial
MySQL实战优化高手07 生产经验:如何对生产环境中的数据库进行360度无死角压测?
CDC: the outbreak of Listeria monocytogenes in the United States is related to ice cream products
Not registered via @enableconfigurationproperties, marked (@configurationproperties use)
Redis集群方案应该怎么做?都有哪些方案?
C杂讲 双向循环链表
Software test engineer development planning route
Inject common SQL statement collation
CAPL script printing functions write, writeex, writelineex, writetolog, writetologex, writedbglevel do you really know which one to use under what circumstances?
Implement sending post request with form data parameter
高并发系统的限流方案研究,其实限流实现也不复杂
Contest3145 - the 37th game of 2021 freshman individual training match_ B: Password
CANoe CAPL文件操作目录合集
The appearance is popular. Two JSON visualization tools are recommended for use with swagger. It's really fragrant
13 medical registration system_ [wechat login]
① BOKE
Bugku web guide
Not registered via @EnableConfigurationProperties, marked(@ConfigurationProperties的使用)