response writer 向页面输出信息

//得到response 对象

HttpServletResponseWrapper response = (HttpServletResponseWrapper)FacesContext.getCurrentInstance().getExternalContext().getResponse();

//输出信息
response.getResponse().getWriter().write("<script>alert('a')</script>");

 

注意:1 .如果输出信息两边有空格,可能导致无法输出的情况

        2. 可能输出中文的时候由于编码而成乱码,解决办法:

           

//得到response 对象

HttpServletResponseWrapper response = (HttpServletResponseWrapper)FacesContext.getCurrentInstance().getExternalContext().getResponse();
//设置转码
ServletResponse myResponse=response.getResponse();
myResponse.setCharacterEncoding("utf-8");
//输出信息
myResponse.getWriter().write("<script>alert('a')</script>");

 

    

你可能感兴趣的:(response)