使 <jsp-config> 标签工作的标准 web.xml java 头是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26789165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:36:19  来源:igfitidea点击:

What is standard web.xml java header to get <jsp-config> tag working?

javajspglassfishglassfish-3web.xml

提问by user46688

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.

我有一个 GlassFish 3.1.2 应用服务器,并且我已经开始使用 JSP 页面,所以我对正确配置 UTF-8 编码很感兴趣。

My original working web.xmlfile started with

我的原始工作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"> 

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

但后来我添加了以下内容以强制使用 UTF-8 编码

<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

和我的 GlassFish 3.1.2 服务器日志文件报告

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

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

为了修复错误,我将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" >

and now I'm getting a GlassFish server error

现在我收到 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.

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

我的语法关闭了吗?如何<jsp-config>使用xml.web2.3 版文件中的头文件?

UPDATE 1

更新 1

If I start the web.xmlfile as

如果我将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>

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/";'.|#]

[#|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 = "undefined"; 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/";'.|#]

UPDATE 2

更新 2

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

或者,我glassfish-web.xmlWEB-INF目录中也有一个文件。如果我将web.xml文件恢复为原始文件并将该<jsp-config>部分放在glassfish-web.xml而不是 中web.xml,则服务器启动正常。<jsp-config>应该放入哪个文件,或者这重要吗?

采纳答案by Arturo

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

要使用 UTF-8 配置 JSP,您需要使用,在 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

我的 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>

回答by Oleg Mikheev

For servlet 3.1 api level use:

对于 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">