JavaWeb-MVC、过滤器

一、MVC架构图

Model

  • 业务处理:业务逻辑(Service)

  • 数据持久层:CRUD(Dao)

View

  • 展示数据

  • 提供连接发起Servlet请求(a,form,img....)

Controller(Servlet)

  • 接收用户的请求:(req:请求参数,Session信息)

  • 交给业务层处理对应的代码

  • 控制视图的跳转
    比如一个登录流程

登录--->接收用户的登录请求--->Servlet处理用户的请求(获取用户登录的参数,username,password)--->交
给业务员层处理登录业务(判断用户名密码是否正确)--->Dao层查询用户名和密码是否正确-->数据库

二、过滤器(Filter重点)

Filter:过滤器,用来过滤网站的数据;
  • 处理中文乱码

  • 登录验证.....

1.Filter开发步骤

(1)导入Maven依赖

<dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <!--            作用域,一般不要-->
          <!--            <scope>provided</scope>-->
      </dependency>
      <!--JSP的依赖-->
      <dependency>
          <groupId>javax.servlet.jsp</groupId>
          <artifactId>javax.servlet.jsp-api</artifactId>
          <version>2.3.3</version>
      </dependency>
      <!--standard标签库-->
      <dependency>
          <groupId>taglibs</groupId>
          <artifactId>standard</artifactId>
          <version>1.1.2</version>
      </dependency>
      <!--JSTL表达式的依赖-->
      <dependency>
          <groupId>javax.servlet.jsp.jstl</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
      </dependency>

(2)编写过滤器

  • 实现Filter接口

  • 重写方法

package com.tang.filter;

import javax.servlet.*;
import java.io.IOException;

public class CharacterEncodingFilter implements Filter {
  //初始化:web服务器启动,就已经初始化了,随时等待过滤器对象出现
  public void init(FilterConfig filterConfig) throws ServletException {
      System.out.println("characterEncoding初始化");
  }
  /*
  * chain 链
  * 1.过滤其中所有代码,在过滤特定请求
  *   (特定请求:比如在Servlet中会在web.xml中进行注册,同理过滤器也一样需要注册和添加映射)的时候都会执行
  * 2.必须要让过滤器继续通行
  *    chain.doFilter(request,response);*/
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
      request.setCharacterEncoding("utf-16");//写utf-8得写setContentType为text/html,否则还会乱码因此我直接写utf-16
      response.setCharacterEncoding("utf-16");
      response.setContentType("text/html;charset=UTF-8");

      System.out.println("setCharacterEncoding执行前。。。");
      chain.doFilter(request,response);//对过滤器起一个放行的作用,如果不写程序到这里就被拦截停止
      System.out.println("setCharacterEncoding执行后。。。");
  }
  //销毁:web服务器关闭的时候,过滤器会销毁
  public void destroy() {
      System.out.println("characterEncoding销毁");
  }
}

(3)在web.xml中配置filter

   <servlet>
        <servlet-name>showservlet</servlet-name>
        <servlet-class>com.tang.servlet.ShowServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>showservlet</servlet-name>
        <url-pattern>/servlet/show</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>showservlet</servlet-name>
        <!--一个请求设置两个访问路径-->
        <url-pattern>/show</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>com.tang.filter.CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <!--只要是/servlet的任何请求都会经过这个过滤器-->
        <url-pattern>/servlet/*</url-pattern>
    </filter-mapping>

ShowServlet代码如下

package com.tang.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-16");
        resp.getWriter().write("唐三是挂,谁惹谁挂");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

运行结果图
同一个请求,以servlet开头的访问会被过滤,另一个去访问则不会被过滤,结果如下

2.常见应用

(1)用户登录之后才能进入主页!用户注销后就不能进入主页了,用过滤器实现
实现大体思路:

  • 用户登录之后,向Session中存放用户的数据

  • 进入主页的时候要判断用户是否已经登录

我的目录结构如下图


因为我的web目录写在src的main下因此以下代码中重定向的时候加上了Tomcat部署中应用上下文的路径
LoginServlet代码

package com.tang.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 LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //获取前端请求的参数
        System.out.println("进入了登录");
        //String username = req.getParameter("username");
        if(req.getParameter("username").equals("admin")){//登录成功
            System.out.println("登录成功");
            //USER_SESSION为一个常量,通常自己新建util包,并放在新建constant的类中
            req.getSession().setAttribute("USER_SESSION",req.getSession().getId());
            resp.sendRedirect("/Filter_war/sys/success.jsp");

        }else {
            resp.sendRedirect("/Filter_war/error.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

login.jsp代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>登录</h1>
<%--这里如果用post方式提交会出现在前端页面输入信息,但是却得不到前端请求的参数值--%>
<form action="/Filter_war/servlet/login" method="get">
    <input type="text" name="username">
    <input type="submit">
</form>
</body>
</html>

LogoutServlet代码

package com.tang.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 LogoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String user_session = (String) req.getSession().getAttribute("USER_SESSION");
        if(user_session != null){//说明用户处于登录状态,因为只有登录状态USER_SESSION不为空也即user_session不为空
            req.getSession().removeAttribute("USER_SESSION");//将该登录状态的用户移除
            resp.sendRedirect("/Filter_war/login.jsp");//注销之后进入登录界面

        }else{
            resp.sendRedirect("/Filter_war/login.jsp");//注销为空也回到该页面
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

success.jsp主界面代码


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    Object user_session = request.getSession().getAttribute("USER_SESSION");
    if(user_session == null){//用户没有登录就不能进如主界面
         //pageContext.forward("login.jsp");//转发
        response.sendRedirect("/Filter_war/login.jsp");
    }
%>
<h1>主页</h1>
<p><a href="/Filter_war/servlet/logout">注销</a></p>
</body>
</html>

除了success.jsp中的方法实现用户没有登录就不能进入主页的方法之外还可以用过滤器的方法实现代码如下

package com.tang.filter;



import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class sysFilter implements Filter {
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request =(HttpServletRequest) servletRequest;
        HttpServletResponse response =(HttpServletResponse) servletResponse;
        if(request.getSession().getAttribute("USER_SESSION")== null){
            response.sendRedirect("/Filter_war/error.jsp");
        }
        filterChain.doFilter(request,response);//这句话一定要写,不然到success.jsp页面就直接停了不会往下走

    }

    public void destroy() {

    }
}

过滤器实现时web.xml中的配置如下

 <filter>
        <filter-name>sysFilter</filter-name>
        <filter-class>com.tang.filter.sysFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>sysFilter</filter-name>
        <url-pattern>/sys/*</url-pattern>
    </filter-mapping>

error.jsp代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>错误</h1>
没有权限用户名或者密码错误
<a href="/Filter_war/login.jsp">返回登录界面</a>
</body>
</html>

web.xm代码如下

<servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>com.tang.servlet.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/servlet/login</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>logoutServlet</servlet-name>
        <servlet-class>com.tang.servlet.LogoutServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>logoutServlet</servlet-name>
        <url-pattern>/servlet/logout</url-pattern>
    </servlet-mapping>

运行结果图

posted @ 2022-07-28 22:52  剑断青丝ii  阅读(147)  评论(0编辑  收藏  举报