variable from .jsp to the html page

后端 未结 5 2058
猫巷女王i
猫巷女王i 2020-12-11 15:01

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

...

${Variable         


        
相关标签:
5条回答
  • 2020-12-11 15:14

    You need to do this:

    <%= Variable %>
    

    The resulting HTML will be:

    <body>
    oh my god
    </body>
    
    0 讨论(0)
  • 2020-12-11 15:22

    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.

    0 讨论(0)
  • 2020-12-11 15:32

    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>
    
    0 讨论(0)
  • 2020-12-11 15:38

    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()

    0 讨论(0)
  • 2020-12-11 15:38

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

    Good Read on why Scriptlets are Bad

    0 讨论(0)
提交回复
热议问题