发布时间:2024-09-09 19:01
目录:
(1)拦截器的使用
(2)使用Servlet
(3)过滤器的使用
字符集过滤的两种方式:
(4)字符集过滤器的应用:使用自定义过滤器
(5)在配置文件中设置编码方式
(1)拦截器的使用
创建拦截器的实现类:LoginInterceptor Ctrl+i 只实现preHandle()方法即可:
package com.bjpowernode.web;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//自定义拦截器
public class LoginInterceptor implements HandlerInterceptor {
/*
* 参数:
* handler:被拦截的控制器对象
* 返回值:boolean:
* true:请求能被Controller处理
* false:请求不能被处理器处理,请求被截断
* */
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("执行了LoginInterceptor的preHandle方法");
return true;
}
}
用这个拦截器,需要把这个拦截器加入到容器中才能起作用,创建好了拦截器怎么把它加入到容器中呢?
需要创建一个实现类,实现WebMvcConfigurer接口,这个接口有很多跟springmvc相关的功能,都在这个接口中实现了
原来我们要注册拦截器,需要写springmvc.xml的配文件,现在把文件中的配置移到了这个接口的方法中:
MyAppConfig:
package com.bjpowernode.config;
import com.bjpowernode.web.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyAppConfig implements WebMvcConfigurer {
//用到拦截器需要实现这个方法,添加拦截器对象,注入到容器中
@Override
public void addInterceptors(InterceptorRegistry registry) {
//创建拦截器对象
HandlerInterceptor interceptor=new LoginInterceptor();
//指定拦截的请求url地址
String path []={"/user/**"};
//指定不拦截的地址
String excludePath []={"/user/login"};
/*
* addInterceptor()添加拦截器
* addPathPatterns()添加拦截的地址
* excludePathPatterns() 排除不拦截的地址
* */
registry.addInterceptor(interceptor).addPathPatterns(path).excludePathPatterns(excludePath);
}
}
创建控制器:BootController:
package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class BootController {
@RequestMapping("/user/account")
@ResponseBody
public String userAccount(){
return "访问user/accout地址";
}
//拦截器之歌方法请求不拦截
@RequestMapping("/user/login")
@ResponseBody
public String userLogin(){
return "访问user/login地址";
}
}
主启动类Application:进行启动项目:
package com.bjpowernode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
在地址栏输入: 访问user/account
控制台会有输出:
在地址栏访问:user/login
但是控制台 没有输出:由于此请求没有被拦截,控制台没有输出
注意这里的user/account请求拦截了,但是preHand方法中返回true所以会正常执行,
排除:是指不经过拦截器,也就不会有输出,user/login的请求是不被拦截的
(2)使用Servlet
创建Servlet:MyServlet:
package com.bjpowernode.web;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
//创建Servler类
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);//Get方法中调用doPost这样无论是get请求,还是Post请求都能处理
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//使用HttpServletResponse输出数据,应答结果
resp.setContentType("text/html;charset=utf-8");//设置内容引擎,指定数据格式,编码
PrintWriter out=resp.getWriter();
out.println("执行的是Servlet");
out.flush();
out.close();
}
}
注册Servlet:
创建的Servlet要想能够被访问就得有对象,这个对象Servlet通过框架才能找到Servlet,所以进行Servlet进行注册,注册用到的类是ServletRegistrationBean,通过写一个配置类,定义一个方法返回值ServletRegistrationBean
创建配置类WebApplicationConfig:先使用ServletRegistrationBean有参构造两个参数的
package com.bjpowernode.config;
import com.bjpowernode.web.MyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WebApplicationConfig {
//通过这个方法就可以得到Servlet对象和访问地址的信息
//定义方法,注册Servle对象
@Bean //注解将这个对象放到容器中,通过这个对象可以找到里面存放的Servlet
public ServletRegistrationBean servletRegistrationBean(){
//public ServletRegistrationBean(T servlet, String... urlMappings)
//第一个参数是:Servlet对象 第二个参数是:url地址
ServletRegistrationBean bean=new ServletRegistrationBean(new MyServlet(),"/myservlet");
return bean;
}
}
主启动类:Application,进行启动项目:
package com.bjpowernode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
在浏览器地址栏输入:
使用ServletRegistrationBean没有参构造参数的
WebApplicationConfig:注释掉有参数的
package com.bjpowernode.config;
import com.bjpowernode.web.MyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WebApplicationConfig {
//通过这个方法就可以得到Servlet对象和访问地址的信息
//定义方法,注册Servle对象
@Bean //注解将这个对象放到容器中,通过这个对象可以找到里面存放的Servlet
public ServletRegistrationBean servletRegistrationBean(){
//public ServletRegistrationBean(T servlet, String... urlMappings)
//第一个参数是:Servlet对象 第二个参数是:url地址
//ServletRegistrationBean bean=new ServletRegistrationBean(new MyServlet(),"/myservlet");
ServletRegistrationBean bean=new ServletRegistrationBean();
bean.setServlet(new MyServlet());
bean.addUrlMappings("/login","/test"); //相当于
return bean;
}
}
Servlet:
在浏览器输入:http:localhost:8080/login
输入另外另一个访问地址:http:localhost:8080/test
(3)过滤器的使用
创建过滤器:MyFilter:
package com.bjlpokwernode.web;
import javax.servlet.*;
import java.io.IOException;
//自定义过滤器
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("执行了MyFilter,doFilter");
//要想请求能够执行下去,需要来执行,过滤器那个链filterChain,这样请求就能够往后传递,能够处理请求了
filterChain.doFilter(servletRequest,servletResponse);
}
}
注册过滤器:
创建配置类:WebApplicationConfig:
package com.bjlpokwernode.config;
import com.bjlpokwernode.web.MyFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WebApplicationConfig {
//返回值,过滤器注册的对象
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean bean=new FilterRegistrationBean();
//指定过滤器
bean.setFilter(new MyFilter());
//过滤器地址
bean.addUrlPatterns("/user/*");
return bean;
}
}
创建控制器:
CustomFilterController:
package com.bjlpokwernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class CustomFilterController {
@RequestMapping("/user/account")
@ResponseBody
public String userAccount(){
return "user/account";
}
//不会进行过滤
@RequestMapping("/querty")
@ResponseBody
public String queryAccount(){
return "query";
}
}
主启动类Application:启动项目:
package com.bjlpokwernode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
在浏览器进行访问
输入:
控制台没有输出:
(4)字符集过滤器的应用:使用自定义过滤器
CharacterEncodingFilter过滤器:不是自己写的,在springmvc框架中有这个过滤器类的
创建Servlet:MyServlet:
package com.bjpowernode.web;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out=resp.getWriter();
out.println("在Servlet输出中文");
out.flush();
out.close();
}
}
配置类:WebSystenConfig:
package com.bjpowernode.config;
import com.bjpowernode.web.MyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class WebSystemConfig {
//注册Servlet
@Bean
public ServletRegistrationBean servletRegistrationBean(){
MyServlet myServlet=new MyServlet();
ServletRegistrationBean reg=new ServletRegistrationBean(myServlet,"/myservlet");
return reg;
}
}
主启动类Application:启动项目:
package com.bjpowernode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
在浏览器访问:发现输出的中文乱码
使用字符集过滤器:
WebSystenConfig:注册过滤器
package com.bjpowernode.config;
import com.bjpowernode.web.MyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
@Configuration
public class WebSystemConfig {
//注册Servlet
@Bean
public ServletRegistrationBean servletRegistrationBean(){
MyServlet myServlet=new MyServlet();
ServletRegistrationBean reg=new ServletRegistrationBean(myServlet,"/myservlet");
return reg;
}
//注册过滤器:
@Bean
public FilterRegistrationBean filterRegistrationBean(){
FilterRegistrationBean reg=new FilterRegistrationBean();
//使用框架中的过滤器
CharacterEncodingFilter filter=new CharacterEncodingFilter();
//设置过滤器参数
filter.setEncoding("utf-8");//指定编码方式
filter.setForceEncoding(true);//指定request,response都是用encoding的值
//添加过滤器
reg.setFilter(filter);
//指定过滤器过滤的url地址 /*代表所有地址
reg.addUrlPatterns("/*");
return reg;
}
}
在配置文件application.properties中:
#SpringBoot中默认已经配置了CharacterEncodingFilter,true,默认编码IOS-8859-1
#需要设置enabled=false,作用是关闭系统中配置好的过滤器,使用自定义的CharacterEncodingFliter
server.servlet.encoding.enabled=false
使用过滤器之后:
(5)在配置文件中设置编码方式
MyServlet:
package com.bjpowernode.web;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out=resp.getWriter();
out.println("******在Servlet输出中文,默认编码IOS-8859-1*********");
out.flush();
out.close();
}
}
配置类:WebSystenConfig:
package com.bjpowernode.config;
import com.bjpowernode.web.MyServlet;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
@Configuration
public class WebSystemConfig {
//注册Servlet
@Bean
public ServletRegistrationBean servletRegistrationBean(){
MyServlet myServlet=new MyServlet();
//第一个参数的过滤器对象,第二个是请求方式请求地址
ServletRegistrationBean reg=new ServletRegistrationBean(myServlet,"/myservlet");
return reg;
}
}
配置文件application.properties:
#端口号
server.port=9001
#访问路径
server.servlet.context-path=/myboot
#让系统的CharacterEncodingFilter生效
server.servlet.encoding.enabled=true
#指定编码方式
server.servlet.encoding.charset=utf-8
#设置请求和应答对象都是用charset的值
server.servlet.encoding.force=true
主启动类:启动项目
package com.bjpowernode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
在浏览器输入地址: