• 渚漪Day20——JavaWeb 10【MVC三层架构&&Filter】


    MVC三层架构

    Model View Controller 模型 视图 控制器

    Model

    • 业务处理:业务逻辑(Service)
    • 数据持久层CRUD(Dao)

    View

    • 展示数据
    • 提供链接发起Servlet请求(a,form,img...)

    Controkler(Servlet)

    • 接受用户的请求(req:请求参数,Session信息。。。。)
    • 交给业务层处理对应的代码
    • 控制视图的跳转
    登录——>接收用户的登录请求——>处理用户的请求(获取用户登录的参数,username,password)——>交给业务层处理业务(判断用户名密码是否正确:事务)——>Dao层查询用户名和密码是否正确——>数据库
    

    Filter

    Filter:过滤器。用来过滤网站的数据

    1、开发步骤

    1. 导包
    2. 实现Filter接口重写Filter方法

    代码

    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">
        <servlet>
            <servlet-name>ss</servlet-name>
            <servlet-class>servlet.showServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>ss</servlet-name>
            <url-pattern>/ss</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>ss</servlet-name>
            <url-pattern>/servlet/ss</url-pattern>
        </servlet-mapping>
    
        <filter>
            <filter-name>filter</filter-name>
            <filter-class>Filter.CharacterEncodingFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>filter</filter-name>
            <url-pattern>/servlet/*</url-pattern>
        </filter-mapping>
    </web-app>
    

    CharacterEncodingFilter.java

    package Filter;
    
    import javax.servlet.*;
    import java.io.IOException;
    
    public class CharacterEncodingFilter implements Filter {
    
        //初始化
        public void init(FilterConfig filterConfig) throws ServletException {
            System.out.println("CharacterEncodingFilter初始化");
        }
    
        //销毁
        public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            //filterChain 链
    
            servletRequest.setCharacterEncoding("utf-8");
            servletResponse.setCharacterEncoding("utf-8");
            servletResponse.setContentType("text/html;charset=UTF-8");
    
            System.out.println("执行前........");
    
            filterChain.doFilter(servletRequest, servletResponse);
    
            System.out.println("执行后........");
        }
    
        public void destroy() {
    
        }
    }
    

    showServlet

    package servlet;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class showServlet extends HttpServlet {
        @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
       // resp.setContentType("text/html");
    
        resp.getWriter().write("你好,世界");
    }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.example</groupId>
        <artifactId>javaweb-Filter</artifactId>
        <version>1.0-SNAPSHOT</version>
        <dependencies>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>4.0.1</version>
            </dependency>
    
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>javax.servlet.jsp-api</artifactId>
                <version>2.3.3</version>
            </dependency>
    
            <dependency>
                <groupId>javax.servlet.jsp.jstl</groupId>
                <artifactId>jstl-api</artifactId>
                <version>1.2</version>
            </dependency>
    
            <dependency>
                <groupId>taglibs</groupId>
                <artifactId>standard</artifactId>
                <version>1.1.2</version>
            </dependency>
    
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>
        </dependencies>
    
    </project>
    

    结果

    未过滤

    过滤

  • 相关阅读:
    海岛争霸
    hihocoder 1089 最短路径·二:Floyd算法
    hihocoder1081 :最短路径·一
    [蓝桥杯][历届试题]蚂蚁感冒
    牛客网Wannafly挑战赛27 A: 灰魔法师
    hdu1875:畅通工程再续
    hdu1863:畅通工程
    hdu1879:继续畅通工程
    hdu1233:还是畅通工程
    洛谷 P1546 最短网络 Agri-Net
  • 原文地址:https://www.cnblogs.com/ijuysama/p/12818044.html
Copyright © 2020-2023  润新知