19

I have a .jsp page that passes the variable from the servlet. Lets assume that I have a .jsp page like

...
<body>
${Variable}
</body>
...

and lets also assume that variable = "oh my god". This is OK. But how I can put this variable to the .html page that the browser will show the value of the variable?

6 Answers6

22

You need to do this:

<%= Variable %>

The resulting HTML will be:

<body>
oh my god
</body>
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • if I put this into html page, then as a result I get <%= Variable %> and not the value of the variable –  May 07 '12 at 20:13
  • 2
    @Bob: you put that in the **JSP** file, not in a **HTML** file. The resulting **HTML** will be rendered by the Servlet/JSP engine. – Pablo Santa Cruz May 07 '12 at 20:14
6

Actually currently best voted answer and solution posted there (<%= Variable %>) acts exactly the same as code that you provided in the question (${Variable}). The only difference is that yours is the one that should be used, because it's more readable and it's not any damn scriptlet!

For my point of view the, if you want to your JSP variable in play html page, you will need javascript to retrieve that variable out of html rendered by jsp, and you it in the actual newPage.html. You could put hidden iframe into that newPage.html, embed there jsp page as source of that iframe, and just parse its html with e.g. getElementById()

dantuch
  • 9,123
  • 6
  • 45
  • 68
  • I know this is way later than typical. But I found a problem with using `${Variable}`. If you set a variable that is not a defined attribute, then that variable is null when using expression language, and it only works with <%=Variable%> And as for avoiding Scriptlets entirely... The thing I'm working on doesn't support jstl core. It sucks. But I cannot avoid scriptlets except by completely rearranging code to avoid any actual logic. – RoboticRenaissance Aug 16 '19 at 13:21
1

There are two options, either use scriptlets or expression language, i would suggest go with expression language.

Good Read on why Scriptlets are Bad

Rachel
  • 100,387
  • 116
  • 269
  • 365
0

before accessing variable inside html you need to initialize the variable and then do whatever the calculations and other modifications inside another JSP code block. Now you can access the variable inside the html. This is my first answer for the Stackoverflow.com please experts notify the mistakes i've done.

<body>
<% java.lang.Integer var=0; %>
<%
  int a;
  int b;
  var=a+b;
%>
<% out.print(var);%>
</body>
Lakshan
  • 297
  • 1
  • 3
  • 14
0

Although this question is old, I think, it's still actual, so I'll try to contribute from my side.
Question is quite simple and I think most of the answers are just answering different question - hence - providing a bit of confusion.
As far as I understand, that question is:

Can I have a dynamic variable of JSP (which is btw, an element of Expression Lnaguage) in html, in the same way as I have it in jsp?

And the answer is No.

JSP translates for Java Server Pages, and that's the point here, that the dynamic value is generated and being provided to jsp on server side. You can't make your html dynamic.

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
0
long taskId = 223;
//
<input type="hidden" name="taskId" value=<%=taskId%> />
  • 2
    Please add further details to expand on your answer, such as working code or documentation citations. – Community Sep 02 '21 at 09:46