Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initializing text box value in jsp page with java variable

I have a jsp page having two text field id and name. Now I want to Declare two java variable int ID=5; & String Name="Riyana";. I want to initialize the value of the text fields with these two java variable.How to do this? Please help. This is my code segment of jsp page:

     <% 
        int ID=5;
        String Name="Riyana";
     %>

    <tr>
        <td align="right">ID</td>
        <td><input name="id" value="" size="20" type="text"></td>
    </tr>
    <tr>
        <td align="right">Name</td>
        <td><input name="name" value="" size="50" type="text"></td>
   </tr>
like image 379
riyana Avatar asked Sep 14 '26 22:09

riyana


2 Answers

  <%!   int ID=5; String Name="Riyana"; 
%>

value="<%=Name%>"

Please use <%! %> for variable initialisation.

  <td align="right">Name</td> 
  <td><input name="name" value="<%=Name%>" size="50" type="text">
  </td>       
</tr> 

Do not use scriptlets, try the taglibs instead.

like image 170
Dead Programmer Avatar answered Sep 17 '26 11:09

Dead Programmer


I would strongly suggest you not to use Java code in jsp, its bad MVC design.

You should pass these variable from servlet and on jsp use jstl to access variables

how ever for your code solution is here.

<%!  

 int ID=5; 
 String Name="Riyana"; 

%>

<input name="name" value="<%=Name%>" size="50" type="text">
like image 28
jmj Avatar answered Sep 17 '26 10:09

jmj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!