开发者

Initializing text box value in jsp page with java variable

开发者 https://www.devze.com 2023-02-03 08:33 出处:网络
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

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>


  <%!   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.


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">


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

Use single quotes in the value of the input text, like value='<%=Name%>' instead of double quotes like value="<%=Name%>"


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消