Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attributes in JSP request become 0

I am attempting to send two attributes to a JSP via a Java HttpServlet. The problem is they are both appearing as 0 (the number) in my JSP file.

Here is the doGet method in my RegisterDeveloper.java file:

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // Fill up default attributes for the template
        req.setAttribute("validation-error", false);
        req.setAttribute("validation-error-message", "");

        // Redirect to register page
        req.getRequestDispatcher("/register.jsp").forward(req, resp);
}

Very simple and when I step through the attributes are added to the request and the browser is redirected to the /register.jsp page.

Here is the problem part of the register.jsp page:

<c:if test="${validation-error}">
    <div id="validationError">
        <span id="errorText">
            ${validation-error-message}
        </span>
    </div>
</c:if>

For some reason the attributes I added to the request have all become '0'.

The error displayed in the browser is:

HTTP Status 500 - javax.el.ELException: Cannot convert 0 of type class java.lang.Long to class java.lang.Boolean

My web.xml incase it's relevant:

<?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/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

    <display-name>RServer</display-name>
    <welcome-file-list>
        <welcome-file>homepage.html</welcome-file>
    </welcome-file-list>

    <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>RegisterDeveloper</servlet-name>
        <servlet-class>com.steven.RegisterDeveloper</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>RegisterDeveloper</servlet-name>
        <url-pattern>/register</url-pattern>
    </servlet-mapping>

</web-app>

I'm using Tomcat6. This all seems very straight forward, and I have very similar code working on other pages, so I'm really struggling to see what I've missed!

Any help, very much appreciated.

I have javax.servlet.jsp.jstl-1.2.1.jar and javax.servlet.jsp.jstl-api-1.2.1.jar in my /WEB-INF/lib/ folder..

like image 213
BitSteven Avatar asked Feb 06 '13 12:02

BitSteven


1 Answers

From a specification I googled

An identifier is constrained to be a Java identifier - e.g., no -, no /, etc.

validation-error is not a Java identifier, but a subtraction operation for validation and error. Since you have not defined those, they are null and this applies:

1.7.1 Binary operators - A {+,-,*} B ■ If A and B are null, return (Long)0

So I guess the test becomes if( (Long)0 == true ).

Try with req.setAttribute("validationError", false); ${validationError}

like image 77
Esailija Avatar answered Sep 28 '22 07:09

Esailija