Tomcat:在上下文参数名称中使用双引号字符

时间:2019-06-02 13:21:58

标签: jsp tomcat deployment-descriptor

我找不到要在部署描述符文件中声明的上下文参数名称中使用双引号字符的正确语法。

这是web.xml中的参数定义:

<context-param>
<param-name>Catalina:type=GlobalRequestProcessor,name=\"http-nio-8080\"</param-name>
<param-value>requestCount</param-value>
</context-param>

以下JSP代码:

<%@ page import="java.util.*,javax.management.*"%>
JMX testing
<%
  Enumeration ipn = application.getInitParameterNames();

  String ipnName = "";
  while(ipn.hasMoreElements()) {
    ipnName = (String)ipn.nextElement();

    ObjectName on = new ObjectName( ipnName );
  }
%>

触发此异常:

  

HTTP状态500 –内部   服务器错误h1   {font-family:Tahoma,Arial,sans-serif;颜色:白色;背景颜色:#525D76;字体大小:22px;}   h2   {font-family:Tahoma,Arial,sans-serif;颜色:白色;背景颜色:#525D76;字体大小:16像素;}   3小时   {font-family:Tahoma,Arial,sans-serif;颜色:白色;背景颜色:#525D76;字体大小:14px;}   身体   {font-family:Tahoma,Arial,sans-serif;颜色:黑色;背景颜色:白色;}   b   {font-family:Tahoma,Arial,sans-serif;颜色:白色;背景颜色:#525D76;} p   {font-family:Tahoma,Arial,sans-serif; background:white; color:black; font-size:12px;}   一个{color:black;} a.name {color:black;} .line   {height:1px; background-color:#525D76; border:none;}

HTTP   状态500 –内部服务器错误

类型异常报告

消息   javax.servlet.ServletException:   javax.management.MalformedObjectNameException:无效字符   属性的值部分为'“'

说明   服务器遇到意外情况,阻止了它   履行   

异常

org.apache.jasper.JasperException:
javax.servlet.ServletException:
javax.management.MalformedObjectNameException: Invalid character
'"' in value part of property
        org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:598)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:499)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

根本原因

javax.servlet.ServletException:
javax.management.MalformedObjectNameException: Invalid character
'"' in value part of property
        org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:909)
        org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:838)
        org.apache.jsp.ko_jsp._jspService(ko_jsp.java:138)
        org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

根   原因

javax.management.MalformedObjectNameException:
  Invalid character '"' in value part of property
          java.management/javax.management.ObjectName.construct(ObjectName.java:621)
          java.management/javax.management.ObjectName.<init>(ObjectName.java:1406)
          org.apache.jsp.ko_jsp._jspService(ko_jsp.java:122)
          org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
          javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
          org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
          org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
          org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
          javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
          org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

我尝试更改JASPER strictQuoteEscaping的值和其他语法无效。我正在使用Java 9 u181运行Tomcat 8.5.40

通过直接调用此JMX的方式效果很好:

<%@ page import="java.util.*,javax.management.*"%> JMX testing <% ObjectName on = new ObjectName("Catalina:type=GlobalRequestProcessor,name=\"http-nio-8080\""); %>

我错过了明显的东西吗?

1 个答案:

答案 0 :(得分:1)

如果在标记之间,则不必转义"字符。基本上,<param-name>标记之间的字符串:

<param-name>Catalina:type=GlobalRequestProcessor,name=\"http-nio-8080\"</param-name>

等于字符串(注意三个反斜杠):

Catalina:type=GlobalRequestProcessor,name=\\\"http-nio-8080\\\"

因此,在web.xml中指定对象名称时,只需按照将其写在纸上(即不带反斜杠)的方式进行操作即可:

<param-name>Catalina:type=GlobalRequestProcessor,name="http-nio-8080"</param-name>

更新(关于评论的第二个问题)

  

这不起作用,这次是例外   org.apache.jasper.JasperException:javax.servlet.ServletException:   javax.management.MalformedObjectNameException:关键属性不能   是空的您是否使用JMX在Tomcat上正确尝试了您的建议   配置并启用?

key properties是冒号后面的键/值对。在您的示例中,您有两个键:

  • type = GlobalRequestProcessor
  • name =“ http-nio-8080”

所以唯一的解释是此异常是由于以下原因引起的:

  • 使用其他构造函数(问题中的代码不太可能使用正确的构造函数)
  • 您所拥有的上下文参数比显示的要多,其中一些在:之后没有任何内容。请注意,您正在遍历所有上下文参数,其中一些可能不是对象名称:while(ipn.hasMoreElements())

只需打印其名称即可找到导致问题的上下文参数:

<%
    Enumeration ipn = application.getInitParameterNames();

    String ipnName;
    while (ipn.hasMoreElements()) {
        ipnName = (String) ipn.nextElement();
        try {
            ObjectName on = new ObjectName(ipnName);
            out.println("<br>Good param: " + ipnName);
        }catch (Exception e){
            out.println("<br>Bad param: " + ipnName);
        }
    }
%>