-1

I tried this code ,its working fine but i want to know a way to have a variable in the html code .......for example check the below given code.

<%
    int z=2;
    String s;
 if (z==1)
 {
%>
<a href="jaipur.jsp">Click Here</a>
<% 

 }
 else
   {
%>
    <a href="goa.jsp">Click Here</a>

  <%
    }

   %>

instead of hard coding the goa.jsp and jaipur.jsp , I want to use

<a href= "Somevariable" >Click here</a>

Where i can set the value of Somevariable as per the requirement of the program . I tried different ways but failed each time.

Smita Ahinave
  • 1,901
  • 7
  • 23
  • 42
Navankur Chauhan
  • 407
  • 6
  • 22

2 Answers2

1

Do not use Java code in JSP. Use JSTL.

<c:set var="var1" value="${3}"/>
<c:set var="var2" value="${2}"/>

<c:choose>

    <c:when test="${var1 < var2}">
        <c:url value="jaipur.jsp" var="url">
            <c:param name="id" value="${var1}" />
        </c:url>
    </c:when>

    <c:when test="${var2 > var1}">
        <c:url value="other.jsp" var="url">
            <c:param name="id" value="${var2}" />
        </c:url>
    </c:when>

    <c:otherwise>
        <c:url value="goa.jsp" var="url">
            <c:param name="id" value="${var2 + var1}" />
        </c:url>
    </c:otherwise>
</c:choose>

<a href="${url}" />Click here</a>
0

use this

<a href="<%=variable%>" >Click here</a>
shmosel
  • 49,289
  • 6
  • 73
  • 138
KVK
  • 1,257
  • 2
  • 17
  • 48