拦截response的响应内容,应该用什么表达?(如果响应只有一段话)
package com.filter.second;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FilterSecond implements Filter {
private String ecode;
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest req=(HttpServletRequest) request;
HttpServletResponse res=(HttpServletResponse)response;
req.setCharacterEncoding(ecode);
res.setContentType("text/html;charset="+ecode);
System.out.println("拦截到"+req.getRequestURI());
System.out.println("拦截到");
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
ecode=filterConfig.getInitParameter("ecode");
System.out.println("初始化完成,且ecode的值为:"+ecode);
}
}
正在回答
同学你好,response是指向浏览器中发送响应,是无法被拦截.
同学可以使用response.setCharacterEncoding("utf-8");设置响应的编码方式,
或者使用response.getWriter().write("aaa");向浏览器发送响应内容。在浏览器中查看响应的内容。
祝:学习愉快~
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星