Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a variable value in JSP file

Tags:

jsp

scriptlet

I want to display the value of variable named "id" in my code. The code is -

index.html(line 5)-

 <div class="marginTable" data-pubid="<%=id%>" data-count="5">

But whenever i am executing it, it is throwing error An error occurred at line: 5 in the jsp file: /index.html id cannot be resolved to a variable. How to get out of this?

like image 664
Neha Gupta Avatar asked May 22 '14 09:05

Neha Gupta


People also ask

How do I print a JSP file?

The code placed within JSP expression tag is written to the output stream of the response. So you need not write out. print() to write data. It is mainly used to print the values of variable or method.

How do you declare a variable in JSP?

You can declare any number of variables or methods within one declaration tag, but you have to separate them by semicolons. The declaration must be valid in the scripting language used in the JSP file. You can add method to the declaration part.

What is scriptlet expression and declaration in JSP?

Difference between JSP Scriptlet tag and Declaration tag The jsp scriptlet tag can only declare variables not methods. The jsp declaration tag can declare variables as well as methods. The declaration of scriptlet tag is placed inside the _jspService() method.


Video Answer


1 Answers

To display the server side variables in jsp , you can use implicit object out.

some thing like this,

<div class="marginTable"  data-count="5">
  <%=id%>
</div>

But using scriptlets is considered as the bad practice . so you may use EL for more info see this How to avoid Java code in JSP files? as,

${id}
like image 129
Santhosh Avatar answered Oct 14 '22 13:10

Santhosh