Initializing textbox value on jsp page with java variable

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

     <% 
        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>
+3
source share
4 answers
  <%!   int ID=5; String Name="Riyana"; 
%>

value = "<% = Name%>"

Please use <%! %> for the initialization variable.

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

Do not use scripts, try taglib instead.

+1
source

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

servlet jsp jstl

.

<%!  

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

%>

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

Use single quotes in the input text value, for example, value='<%=Name%>'instead of double quotes such asvalue="<%=Name%>"

+1
source

Use jstl

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>   
<c:set var="ID" value="5"/>   
<c:set var="Name" value="Riyana"/>   
<td align="right">Name</td>    
<td><input name="name" value="${Name}" size="50" type="text"></td>
+1
source

Source: https://habr.com/ru/post/1784221/


All Articles