简体   繁体   中英

How can I use Java variable in HTML tag in JSP?

I'm trying the following code in my .jsp file:

<select>
    <%DBHandler db= new DBHandler();%>
    <%db.init();%>
    <%String[] res= db.getExpertise();%>
    <% for(int i=0;i< res.length;i++){ %>

        <option value="<%=res[i]%>"><%=res[i]%></option>
    <%}%>
</select>

But, I don't know how to assign value in a tag using a variable in java code.

Hope this will help your problem;

1. Directives

<%@page language="java" %> 

2. Declarations

<%!    
int radius = 7; 
double pi = 3.1415; 
%>

3. Scriptlets

<%
    String id, name, dob, email, address;

    id = request.getParameter("id");
    name = request.getParameter("name");
    dob = request.getParameter("dob");
    email = request.getParameter("email");
    address = request.getParameter("address");

    sessionEJB.addClient(id, name, dob, email, address);
 %>

4. Expressions

<%!    
double radius = 7; 
double pi = 22/7;    
double area()
{
    return pi*radius*radius;
}    
%>

<html>
  <body>
    Area of circle is <%= area() %>
 </body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM