How to include page encoding to all the jsp page
Asked Answered
P

4

6

I have included <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> in one jsp page and it displayed french characters properly. But i want to include the page encoding to all the jsp pages. In web.xml i included

<jsp-config>
      <jsp-property-group>
      <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
      </jsp-property-group>
 </jsp-config>

but it will work for tomcat7 i am using tomcat6.

Is there any other solution for this?

Patience answered 29/4, 2015 at 13:5 Comment(1)
is your problem with values from your backend ? static html content in your JSP ? parameters ?Gunderson
G
0

You can use a meta tag instead of adding page encoding in all the jsp file or in web.xml . If you have a common .jsp file include

Globigerina answered 3/6, 2015 at 7:46 Comment(6)
can u give me detail explanation?Panoply
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">Globigerina
you can choose any charset which will be suitable.Globigerina
thank u jessica but i have another problem with french character display. i have a file in which i should save french character i m using Fileinputstream and setting the charset but the french characters are displaying as "?" can u please help me...Panoply
which charset are you using?use a correct charset it will work.Globigerina
problem is in windows system its displaying fine but in linux system its displaying as "?"Panoply
G
1

You could define a new Filter in your web.xml and modify your answers there.

For example, you could put this in your web.xml :

<filter>
    <filter-name>encoding</filter-name>
    <filter-class>com.example.EncodingFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*.jsp</url-pattern> 
    <!-- or any pattern you like to excluse non-jsp resources -->
</filter-mapping>

And create a com.example.EncodingFilter class like this :

public class EncodingFilter implements javax.servlet.Filter {

    // init, destroy, etc. implementation

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 
            throws IOException, ServletException
    {
        response.setCharacterEncoding("utf-8");
        chain.doFilter(request, response); // pass request to next filter
    }
}

You could furthermore add tests around your setCharacterEncoding() call to filter only specific URI or exclude for example a non-HTML jsp or specific parameter, like a JSP outputing PDF if URL paramter format=PDF is provided :

if (!request.getQueryString().contains("format=PDF") {
        response.setCharacterEncoding("utf-8");
}
chain.doFilter(request, response); // pass request to next filter
Gunderson answered 29/4, 2015 at 13:13 Comment(10)
your answer is correct but if you could elaborate a bit it would be better.Tion
@Tion should be better nowGunderson
Thank u preuk for ur reply but i m using only jsp and servlets,i am not using any framework...i cant use filter...Panoply
well, this is only from javax.servlet API ... as long as you have a web.xml it means you could use it ; i have this kind of filter working on pure jsp/servlet apps running tomcat6Gunderson
i am getting "The type EncodingFilter must implement the inherited abstract method Filter.init(FilterConfig) " even after including the javax.servlet-api jarPanoply
that's what I meant with this line : // init, destroy, etc. implementation ; this is about implementing methods from an Interface, which a java language problem not related to servlet specificalyGunderson
in web.xml i am getting "The content of element type "web-app" must match "(icon?,display-name?,description?,distributable?,context-param*,servlet*,servlet-mapping*,session-config?,mime- mapping*,welcome-file-list?,error-page*,taglib*,resource-ref*,security-constraint*,login-config?,security-role*,env-entry*,ejb-ref*)"." when i placed filter tags at the bottom before the web app closing tag.. where should i add the filter tags??Panoply
i guess your web.xml is pretty old and its doctype is outdated. Could you edit your answer with you web.xml (at least web-app tag) ?Gunderson
doctype is:<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "/web-app_2_2.dtd">Panoply
Are you REALLY running J2EE 1.2 ? if not you should consider upgrading your config file to match options offered by later J2EE versions ; as I understand you're french-speaking so take a look at this french page or this page from OracleGunderson
I
0

If your parameters are not UTF-8 encoded when using Tomcat, try to adjust the Connector configuration in Tomcat's server.xml like this:
<Connector port="8080" maxHttpHeaderSize="8192" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8" />

Ingeringersoll answered 29/4, 2015 at 13:12 Comment(1)
that is not going to do what the user is askingTion
G
0

You can use a meta tag instead of adding page encoding in all the jsp file or in web.xml . If you have a common .jsp file include

Globigerina answered 3/6, 2015 at 7:46 Comment(6)
can u give me detail explanation?Panoply
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">Globigerina
you can choose any charset which will be suitable.Globigerina
thank u jessica but i have another problem with french character display. i have a file in which i should save french character i m using Fileinputstream and setting the charset but the french characters are displaying as "?" can u please help me...Panoply
which charset are you using?use a correct charset it will work.Globigerina
problem is in windows system its displaying fine but in linux system its displaying as "?"Panoply
A
0

Another way is to apply encoding to all pages is using filter.

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

        }

        public void init(FilterConfig filterConfig) throws ServletException {
            String encodingParam = filterConfig.getInitParameter("encoding");
            if (encodingParam != null) {
                encoding = encodingParam;
            }
        }

Register this filter in web.xml

Astomatous answered 25/1, 2017 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.