0

I am trying to create simple project on tomcat7 via eclipse. Here is the code of my jsp page:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
    <body>
        User ID: ${user.userId}<br />
        Username: ${user.username} (${user.username.length()} characters)<br />
        Full Name: ${fn:escapeXml(user.lastName) += ', '
            += fn:escapeXml(user.firstName)}
        <br /><br />
        <b>Permissions (${fn:length(user.permissions)})</b><br />
        User: ${user.permissions["user"]}<br />
        Moderator: ${user.permissions["moderator"]}<br />
        Administrator: ${user.permissions["admin"]}<br />
    </body>
</html>

Here is the outcome from browser window:

org.apache.jasper.JasperException: javax.el.ELException: Failed to parse the expression [${fn:escapeXml(user.lastName) += ', '
        += fn:escapeXml(user.firstName)}]
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
ProfileServlet.doGet(ProfileServlet.java:46)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)

 root cause

 javax.el.ELException: Failed to parse the expression [${fn:escapeXml(user.lastName) += ', '
        += fn:escapeXml(user.firstName)}]
org.apache.el.lang.ExpressionBuilder.createNodeInternal(ExpressionBuilder.java:145)
org.apache.el.lang.ExpressionBuilder.build(ExpressionBuilder.java:171)
org.apache.el.lang.ExpressionBuilder.createValueExpression(ExpressionBuilder.java:216)
org.apache.el.ExpressionFactoryImpl.createValueExpression(ExpressionFactoryImpl.java:66)
org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:943)
org.apache.jsp.WEB_002dINF.jsp.view.profile_jsp._jspService(profile_jsp.java:96)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
ProfileServlet.doGet(ProfileServlet.java:46)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)

  root cause

   org.apache.el.parser.ParseException: Encountered " <ILLEGAL_CHARACTER> "= "" at line 1, column 32.

Targeted runtime is set(tomcat7). Probably something wrong in my tomcat7/lib directory? (jstl 1.2 jar is in the folder)

Also here is imports from base.jspf (Which is header of all JSPs)

  <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

"Regular" jstl tags work well. Problem with functions

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
kurumkan
  • 2,635
  • 3
  • 31
  • 55
  • 2
    why `+=`? Did you try a simple `+`? Note that `fn:escapeXml(user.lastName + ', ' + user.firstName)` makes the thing more readable IMHO –  Jun 21 '16 at 05:37
  • yes I tried. But the same message still occures. Yes, I also think your approach is more readable. But it's just a tutorial from book – kurumkan Jun 21 '16 at 05:42
  • probably something wrong with jasper* files in lib folder? Don't know what to do – kurumkan Jun 21 '16 at 05:44
  • 1
    This is overthought. Just use `${...}, ${...}` instead of `${... += ', ' += ...}` (which would only work in EL 3.0). – BalusC Jun 21 '16 at 07:39
  • Now I tottally got it! – kurumkan Jun 21 '16 at 08:10

1 Answers1

2

Try using core:out like this :

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    User ID: <c:out value="${user.userId}" />
    <br /> Username: <c:out value="${user.username}" /> (<c:out value="${user.username.length}" /> characters)
    <br /> Full Name: <c:out value="${user.lastName}" />, <c:out value="${user.firstName}" />
    <br />
    <br />
    <b>Permissions (${fn:length(user.permissions)})</b>
    <br /> User: <c:out value="${user.permissions.user}" />
    <br /> Moderator: <c:out value="${user.permissions.moderator}" />
    <br /> Administrator: <c:out value="${user.permissions.admin}" />
    <br />
</body>
</html>

It works fine for me and it escapes XML by default.

neomega
  • 712
  • 5
  • 19