Search code examples
jsptomcatdeployment-descriptor

Tomcat : using double-quote character in a context parameter name


I don't find the correct syntax for using the double-quote character in a context parameter name that I want do declare in the deployment descriptor file.

Here's the parameter definition in web.xml :

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

the following JSP code :

<%@ 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 );
  }
%>

triggers this Exception:

HTTP Status 500 – Internal Server Errorh1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}

HTTP Status 500 – Internal Server Error

Type Exception Report

Message javax.servlet.ServletException: javax.management.MalformedObjectNameException: Invalid character '"' in value part of property

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

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)

Root Cause

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)

Root Cause

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)

I have tried changing JASPER strictQuoteEscaping value and different syntaxes without effect. I am running Tomcat 8.5.40 with Java 9 u181

By the way calling this JMX directly is working well :

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

Am I missing anything obvious ?


Solution

  • You do not have to escape the " characters if it's between tags. Basically the string between the <param-name> tags:

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

    is equal to the string (note the three back-slashes):

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

    So when specifying the object name in the web.xml, just do it as you would write it on paper (i.e. without the backslashes):

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

    UPDATE (about the second issue from the comments)

    This doesn't work, this time the exception is org.apache.jasper.JasperException: javax.servlet.ServletException: javax.management.MalformedObjectNameException: Key properties cannot be empty Did you try your suggestion on a Tomcat with JMX correctly configured and enabled ?

    The key properties are the key-value pairs after the colon. In your example you have two keys:

    • type=GlobalRequestProcessor
    • name="http-nio-8080"

    So the only explanation is that this exception is caused either because:

    • using another constructor (unlikely, your code in the question uses the correct one)
    • You have more context parameters than shown, and some of them do not have anything after the :. Note that you are iterating over all context params and some of them might not be object names: while(ipn.hasMoreElements())

    You can find the context param that causes the issue simply by printing its name:

    <%
        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);
            }
        }
    %>