How to display a variable value in JSP file

2019-05-01 15:14发布

问题:

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?

回答1:

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}


回答2:

I think this what you are looking for.....

page1.html...

 <a href="page2.jsp?id=5">Go</a>

page2.html

    <% String id = request.getParameter("id"); %>
    <div class="marginTable" data-pubid="<%=id%>" data-count="5"></div>


标签: jsp scriplets