3

I've got a GlassFish 3.1.2 app server and I've started using JSP pages so I'm interested in configuring the encoding correctly for UTF-8.

My original working web.xml file started with

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
   "http://java.sun.com/dtd/web-app_2_3.dtd"> 

but then I added the following to it to force UTF-8 encoding

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

and my GlassFish 3.1.2 server log file reported

Element type <jsp-config> must be declared web.xml

In an attempt to fix the error, I changed the start of my web.xml file to be

<?xml version="1.0" encoding="UTF-8"?>  
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
 version="2.4" >

and now I'm getting a GlassFish server error

Invalid Deployment Descriptors in Deployment descriptor file WEB-INF/web.xml ...
One of '{"http://java.sun.com/xml/ns/j2ee":servlet-class, 
         "http://java.sun.com/xml/ns/j2ee":jsp-file}' is expected.

Is my syntax off? How to get <jsp-config> working with header in xml.web file for version 2.3?

UPDATE 1

If I start the web.xml file as

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_3.xsd"
    version="2.3">

<display-name>myApp</display-name>
<description>My Application</description>

<listener>
    <listener-class>...</listener-class>
</listener>

<servlet>
    <servlet-name>MessageBrokerServlet</servlet-name>
    <display-name>MessageBrokerServlet</display-name>
    <servlet-class>...</servlet-class>
    <init-param>
        <param-name>...</param-name>
        <param-value>...</param-value>
   </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet>
  <servlet-name>Reg</servlet-name>
  <servlet-class>com.mydomain.servlet.Reg</servlet-class>
</servlet>

<servlet-mapping>
   <servlet-name>Reg</servlet-name>
   <url-pattern>/reg</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
</welcome-file-list>

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

</web-app>

I see this error:

[#|2014-11-06T13:03:59.779-0800|SEVERE|glassfish3.1.2|javax.enterprise.system.tools.deployment.org.glassfish.deployment.common|_ThreadID=1;_ThreadName=Thread-2;|DPL8015:

Invalid Deployment Descriptors in Deployment descriptor file WEB-INF/web.xml in archive [myapp]. Line 7 Column 41 -- s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'. Saw 'var _U = "undefined"; var g_HttpRelativeWebRoot = "/ocom/";'.|#]

[#|2014-11-06T13:03:59.779-0800|SEVERE|glassfish3.1.2|javax.enterprise.system.tools.deployment.org.glassfish.deployment.common|_ThreadID=1;_ThreadName=Thread-2;|DPL8005: Deployment Descriptor parsing failure : s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'. Saw 'var _U = "undefined"; var g_HttpRelativeWebRoot = "/ocom/";'.|#]

UPDATE 2

Alternatively, I also have a glassfish-web.xml file in WEB-INF directory. If I revert the web.xml file back to original and place the <jsp-config> section in glassfish-web.xml instead of web.xml, the server starts fine. Which file is <jsp-config> supposed to go in, or does it matter?

user46688
  • 733
  • 3
  • 11
  • 29

2 Answers2

3

To configure JSP with UTF-8 you need to use, at the first line of the jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 

You should use this but with your version:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

An example of my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>base</display-name>
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring/spring-mvc-dispatcher.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/spring/spring-database.xml,
            /WEB-INF/spring/spring-security.xml
        </param-value>
    </context-param>
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
Arturo
  • 261
  • 1
  • 4
  • 19
  • thanks @stel, where can i find the right syntax to use with version 2.3? – user46688 Nov 06 '14 at 20:57
  • @user46688 change the _3_0.xsd and version="3.0" to _2_3.xsd and version="2.3" – Arturo Nov 06 '14 at 21:01
  • thanks @stel, with those changes I get this error: `Non-whitespace characters are not allowed in schema elements other than 'xs:appinf o' and 'xs:documentation'. Saw 'var _U = "undefined";` – user46688 Nov 06 '14 at 21:08
  • can you edit your question and put the full web.xml and the full stacktrace? Those only say you have a whitespace that is not allow. – Arturo Nov 06 '14 at 21:11
  • I think what I dont understand you. Sorry. To configure correctly a jsp with UTF-8 you need to add this to the first line of the jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> Rollback the change and delete the tag. Sorry again if I have you confused. – Arturo Nov 06 '14 at 21:29
  • I was trying to make the change globally (as shown here: http://balusc.blogspot.com/2009/05/unicode-how-to-get-characters-right.html#JSPServletRequest). Thanks anyway – user46688 Nov 06 '14 at 21:37
  • I tried to regroup and ask a cleaner question here: http://stackoverflow.com/questions/26791888/jsp-cant-figure-out-how-to-set-utf-8-for-glassfish-3-1-2-response – user46688 Nov 07 '14 at 00:49
0

For servlet 3.1 api level use:

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1"
  metadata-complete="true">
Oleg Mikheev
  • 17,186
  • 14
  • 73
  • 95