整合Servlet三大组件
一、创建Spring Boot项目 - IntegrateThreeComponents01
二、整合Servlet组件
1、编写自定义MyServlet类
2、编写Servlet配置类
3、启动应用,访问http://localhost:8080/hello 测试效果
三、功能扩展 - 返回6位数字字母验证码
1、创建自定义Servlet类 - CodeServlet
package com.hy.lesson12.servlet;import org.springframework.stereotype.Component;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.util.Random;@Component
public class CodeServlet extends HttpServlet {@Overridepublic void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
// resp.setCharacterEncoding("UTF-8");// 解决中文乱码
// resp.setContentType("text/html;charset=utf8");resp.setDateHeader("Expires", -1);// 浏览器不缓存resp.getWriter().println(randomCode(6));}@Overridepublic void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {doGet(req, resp);}/*** 产生指定位数随机码** @param len* @return*/public String randomCode(int len) {char[] codes = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I','J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T','U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4','5', '6', '7', '8', '9'};StringBuffer buffer = new StringBuffer();for (int i = 0; i < len; i++) {int index = new Random().nextInt(35);buffer.append(codes[index]);}return buffer.toString();}
}
2、在Servlet配置类里注册CodeServlet
3、启动应用,访问http://localhost:8080/hello 测试效果
刷新一下
四、整合Filter组件
1、编写自定义Filter类
package com.hy.lesson12.filter;import org.springframework.stereotype.Component;import javax.servlet.*;
import java.io.IOException;@Component
public class MyFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,FilterChain filterChain) throws IOException, ServletException {//过滤器可以对request请求处理之前进行一些预处理servletRequest.setAttribute("hello", "过滤器向你问候:牛年大吉!");//也可以在request请求处理之后进行一些处理servletResponse.setCharacterEncoding("UTF-8");//解决中文乱码servletResponse.setContentType("text/html;charset=utf8");filterChain.doFilter(servletRequest, servletResponse); // 不要忘记了此句,否则程序正常请求流程就不走了}@Overridepublic void destroy() {}
}
2、编写过滤器配置类FileterConfig
package com.hy.lesson12.config;import com.hy.lesson12.filter.MyFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.util.Arrays;@Configuration
public class FilterConfig {@Beanpublic FilterRegistrationBean getFilter(MyFilter myFilter){FilterRegistrationBean filterRegistrationBean= new FilterRegistrationBean<>(myFilter);filterRegistrationBean.setUrlPatterns(Arrays.asList("/hello", "/code")); // 定义要过滤的路径return filterRegistrationBean;}
}
3、修改MyServlet
4、修改CodeServlet
5、访问http://localhost:8080/hello 测试效果
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!