简体   繁体   中英

JSP UTF-8 char encoding error on Glassfish 3.01

I have a rather starange problem. In my web application I use UTF_8 encoding. This works fine until I put a jsp page into my /Protected/ folder. The exact same jsp file works just fine uder my document root. To reach the /protected/ folder I use a Filter. Like this:

    public void doFilter(ServletRequest request, ServletResponse response,
                     FilterChain chain)
throws IOException, ServletException {

    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse res = (HttpServletResponse) response;
    HttpSession session = req.getSession();
    PrintWriter out = res.getWriter();
    if (session.getAttribute("user") == null){
        // redirect the user to the login page and give error message
             res.sendRedirect("/myApp/login.jsp");
         }
    else {

    chain.doFilter(request, response);

    }
}

In my sun-web.xml I have this specified:

<locale-charset-info default-locale="">
<parameter-encoding default-charset="UTF-8"/>
</locale-charset-info>

And In my jsp files I have this specified:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

plus:

Any help is much appreciated! Thanks in advance!

Get rid of the following line in the filter.

PrintWriter out = res.getWriter();

You only don't need it in a filter at all, but also when the outputstream or writer of the response is been created for the first time, you cannot alter its character encoding afterwards anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM