我有一个GlassFish 3.1.2应用服务器,我已经开始使用JSP页面,所以我有兴趣为UTF-8正确配置编码。
我的原始工作web.xml
文件以
<?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">
然后我添加了以下内容以强制使用UTF-8编码
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
</jsp-property-group>
</jsp-config>
报告我的GlassFish 3.1.2服务器日志文件
Element type <jsp-config> must be declared web.xml
为了修复错误,我将web.xml
文件的开头更改为
<?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" >
现在我收到了GlassFish服务器错误
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.
我的语法是否关闭?如何让<jsp-config>
使用版本2.3的xml.web
文件中的标题?
更新1
如果我将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_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>
我看到了这个错误:
[#|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:
部署描述符文件中的无效部署描述符 归档[myapp]中的WEB-INF / web.xml。第7行第41栏 - s4s-elt-character:模式中不允许使用非空白字符 'xs:appinfo'和'xs:documentation'以外的元素。看到'var _U = “不确定”; var g_HttpRelativeWebRoot =“/ ocom /”;'。|#]
[#| 2014-11-06T13:03:59.779-0800 |严重| glassfish3.1.2 | javax.enterprise.system.tools.deployment.org.glassfish.deployment.common | _ThreadID = 1; _ThreadName = Thread- 2; | DPL8005: 部署描述符解析失败:s4s-elt-character: 其他架构元素中不允许使用非空白字符 比'xs:appinfo'和'xs:documentation'。看到'var _U =“undefined”; var g_HttpRelativeWebRoot =“/ ocom /”;'。|#]
更新2
或者,我在glassfish-web.xml
目录中也有一个WEB-INF
文件。如果我将web.xml
文件还原为原始文件并将<jsp-config>
部分放在glassfish-web.xml
而不是web.xml
中,则服务器启动正常。哪个文件<jsp-config>
应该进入,或者它是否重要?
答案 0 :(得分:3)
要使用UTF-8配置JSP,您需要在jsp的第一行使用
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
你应该使用它,但你的版本:
<?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">
我的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>
答案 1 :(得分:0)
对于servlet 3.1 api级别使用:
<?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">