7

I'm trying to print a string variable via my jsp file, here is my code:

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.lang.*;"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>


<!DOCTYPE html>

<html>
<head>
<title>why are you not working</title>
<meta charset="utf-8" />
</head>

<body>
    <%
        String test = "<b><u>bold and underlined</u></b>";
     %>

    <c:set var="test1" value="<u>underlined</u>" />
    <c:set var="test2" value="${test}" />

    <c:out value="${test}" escapeXml="false" />
    <c:out value="${test1}" escapeXml="false" />
    <c:out value="${test2}" escapeXml="false" />

</body>
</html>

output:

enter image description here

Is there a way to print test or test2 using JSTL ? As you can see in the code above I've managed to print the variable test1 but nothing appear on the page for the variables test or test2.

PS: why I want to use JSTL ? Because it offers a way to evaluate html tags and not escape them

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
coldistric
  • 303
  • 1
  • 4
  • 14
  • 1
    Are you learning JSP by a decade old JSP 1.x book? Since JSP 2.0 (2003!) you do not need `` at all. Just `${test}` was been sufficient. Make sure you're using up to date resources to learn JSP. Start at our wiki page: http://stackoverflow.com/tags/jsp/info – BalusC Oct 17 '15 at 08:30

2 Answers2

14

yes there is.You can set your variable test in page scope using pageContext object.

<body>
    <%
        String test = "<b><u>bold and underlined</u></b>";
        pageContext.setAttribute("test", test);
     %>

    <c:set var="test1" value="<u>underlined</u>" />
    <c:set var="test2" value="${test}" />

    <c:out value="${test}" escapeXml="false" />
    <c:out value="${test1}" escapeXml="false" />
    <c:out value="${test2}" escapeXml="false" />

</body>

Output

bold and underlined underlined bold and underlined

JSTL works entirely with scoped variables where scope can be request,session or page.By default scope is page. While scriplet is raw java which is inserted into the service method of the JSP page’s servlet. So if you want to access any scriplet variable in JSTL,you need to set in scope.

See Also

Community
  • 1
  • 1
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35
5

The JSP EL "variables" are not local variables. When you write

${test}

the JSP EL looks for an attribute named "test", in the page scope, or in the request scope, or in the session scope, or in the application scope.

So, it's basically equivalent to the following Java code:

<%= out.print(pageContext.findAttribute("test")); %>

You can't access a local variable as you're trying to do with the JSP EL. And there's no reasong to want to, because you shouldn't have Java code (i.e. scriptlets) in the JSP anyway. The controller, written in Java, should store attributes in the request, and the JSP EL should display the values stored in those attributes.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thank you, I didn't know if the use of scriptlets was discouraged.Now I know why thanks to this http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files?lq=1 now I know why. – coldistric Oct 17 '15 at 08:31