Filter simple questions, please friend gets advice, thank you!

Category: Java EE
 
haopengyou011
2013-08-20 03:36:35

Sponsored Links
FirstFilter is a Filter, filter all url (<url-pattern> / * </ url-pattern>)
a.html is a basic html file, only two Chinese characters, "China", with utf-8 saved
now access http://localhost:8080/TestFilter/a.html , Chinese word garbled display, puzzling, very depressed,
congregation hope to give answers, first thanked! ! ! !

package xdw.filters;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

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 FirstFilter implements Filter
{
private FilterConfig filterConfig = null;
public void init(FilterConfig filterConfig) throws ServletException
{
this.filterConfig = filterConfig;
}

public void doFilter(ServletRequest request,
ServletResponse response,FilterChain chain)
throws IOException, ServletException
{
// Servlet
response.setContentType("text/html;charset=UTF-8");
          response.setCharacterEncoding("UTF-8");
//         request.setCharacterEncoding("UTF-8");
// request.setCharacterEncoding("UTF-8");

                PrintWriter out = rep.getWriter();
                // , ( , servlet
                // getWriter() , getOutputStream() 2 )
                
                // . response.setCharacterEncoding("UTF-8");
                // utf-8 , html UTF-8 , !?

// out.println(" IP :" + request.getRemoteHost() + "<br>");// ,
                                                             //
chain.doFilter(req, rep);
}

public void destroy()
{
this.filterConfig = null;
}
}

Sponsored Links

golon
2013-08-20 03:46:32
1 in IE directly open a.html, not through the http, the direct use of the File Open see page coding in the end is what? I suspect oh! ! !

2 to modify your code in order to
response.setCharacterEncoding ("UTF-8"); / / first specify the output stream coding
PrintWriter out = response.getWriter (); / / then get output objects
response.setContentType ("text / html; charset = UTF-8");

please refer to my previous one article examined this issue!
ikeurey
2013-08-20 03:52:44
The above code wrong place PrintWriter out = rep.getWriter ();
changed ; PrintWriter out = response.getWriter ();
frank_1628
2013-08-20 04:07:40
landlord response , rep and request, req confused with Eclipse can be automatically checked.
As Chinese garbled question, because you put the character set to "UTF-8", should be replaced by "GBK"
or "GB2312"
zhaopeng5213
2013-08-20 04:20:16
to old Shichiku:
I right click a.html-> Open as Notepad -> click Save As -> encoding is utf-8 which proved really is utf-8 character set
After you give a statement transposition can really normal, very strange, still do not know what
Why
response.setContentType ("text / html; charset = UTF-8");
response.setCharacterEncoding ("UTF-8");
PrintWriter out = rep.getWriter ();
will not display properly?
and replace the code for you to be able to?
the code is just for practice, not a project
So we wanted to know the reason
Thanks again, if you can please explain!!!!
ysmion
2013-08-20 04:39:47


agree with this statement ~ ~
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.