首页 技术 正文
技术 2022年11月22日
0 收藏 627 点赞 4,403 浏览 3550 个字

实现效果

在编辑框中输入暗号:如果暗号正确,则跳转到正确页面;如果暗号错误,则跳转到错误界面。

【入口界面】

【错误界面】

【成功界面】

【项目结构】

JSP文件

本练习有两个jsp页面

页面1:index.jsp

代码:

<%--
Created by IntelliJ IDEA.
User: xrilang
Date: 30/11/2021
Time: 15:12
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<%-- <a href="${pageContext.request.contextPath}/ServletDemo1" rel="external nofollow" >Demo1</a>--%>
<%-- <br/>--%>
<%-- <a href="${pageContext.request.contextPath}/ServletDemo2" rel="external nofollow" >Demo2</a>--%>
<form action="${pageContext.request.contextPath}/ServletDemo1" method="get">
暗号:<input type="text" name="code"/>
<br/>
<input type="submit" value="sent" />
</form>
</body>
</html>

说明:本页面主要有一个编辑框和一个按钮。编辑框输入暗号,按钮提交

页面2:error.jsp

代码:

<%--
Created by IntelliJ IDEA.
User: xrilang
Date: 01/12/2021
Time: 14:13
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Error</title>
</head>
<body>
<h1 style="color:red">暗号错误</h1>
<a href="index.jsp" rel="external nofollow" >返回</a>
</body>
</html>

说明:这是一个错误界面。

Src

Servlet

文件名:ServletDemo1

代码:

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;import java.io.IOException;@WebServlet(name = "ServletDemo1", value = "/ServletDemo1")
public class ServletDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Servlet Demo1 . doGet() ... ");
response.getWriter().write("Welcome ! It's Servlet Demo1");
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }
}

说明:此界面显示内容为“Welcome ! It’s Servlet Demo1”

过滤器

文件名:FilterDemo1

代码:

import jakarta.servlet.*;
import jakarta.servlet.annotation.*;import java.io.IOException;@WebFilter(filterName = "FilterDemo1")
public class FilterDemo1 implements Filter {
public void init(FilterConfig config) throws ServletException {
// -- 当Filter实例创建之后,服务器立即调用init方法进行初始化的操作.
} public void destroy() {
// -- 在Filter实例销毁之前,执行destroy方法进行善后的处理
} @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
chain.doFilter(request, response);
// -- 当过滤器拦截到对资源的访问时,服务器会调用doFilter方法进行处理.
System.out.println("FilterDemo1.doFilter() ... ");
request.setCharacterEncoding("utf-8");
String code = request.getParameter("code");
if(!"mllt9920".equals(code)){
request.getRequestDispatcher("/error.jsp").forward(request,response);
return;
}
//放行过滤器 ,才可以执行后面资源
chain.doFilter(request,response);
/**
* 参数FilterChain代表多个过滤器组成的过滤器链对象.
* (1)一个资源可以配置多个过滤器进行拦截,多个过滤器执行的顺序是按照Filter在web.xml中对应的filter-mapping标签的先后配置顺序执行的.多个过滤器就组成了一条过滤器链.
* (2)当过滤器拦截到对资源的访问时,如果处理之后放行过滤器,即调用FilterChain中的doFilter方法来放行过滤器. 接着才可以执行后面的资源
* (3)如果后面仍然是过滤器,则也需要在过滤器的doFilter方法中调用FilterChain.doFilter方法才可以放行过滤器,执行后面的资源.
* (4)如果后面没有过滤器,则访问对应的资源. 也就是说当所有的过滤器都调用了FilterChain的doFilter方法时,才可以放行所有的过滤器,才可以访问到对应的资源.
*/
}
}

说明:如果编辑框内容不为“mllt9920”,那么跳转到错误页面

配置文件

web.xml

<?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">
<!--filter setting ... -->
<filter>
<!-- 配置过滤器名字-->
<filter-name>FilterDemo1</filter-name>
<!-- 配置过滤器类的全路径包名-->
<filter-class>FilterDemo1</filter-class>
</filter>
<filter-mapping>
<!-- 配置过滤器名字-->
<filter-name>FilterDemo1</filter-name>
<!-- 配置过滤器要拦截的资源路径(规则)-->
<url-pattern>/ServletDemo1</url-pattern>
<url-pattern>/ServletDemo2</url-pattern>
<!-- 配置拦截指定名字Servlet-->
<servlet-name>ServletDemo1</servlet-name>
<servlet-name>ServletDemo2</servlet-name>
</filter-mapping>
</web-app>

注意:我的文件创建了ServletDemo2的,但是我在本笔记中说要创建ServletDemo2。其实他和1内容一样的,大家可以自己试着创建ServletDemo2做练习

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,985
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,501
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,345
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,128
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,763
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,840