user3678804
user3678804

Reputation: 13

how to get id from <a> tag of html into a jsp file?

I have an html:

<a href="mobile.jsp" id="s5" >Galaxy s5</a>
<a href="mobile.jsp" id="iphone">Iphone 4S</a>
<a href="mobile.jsp" id="xpr">Xperia</a>

I want to get different items of one type in one jsp

so mobile.jsp should be like:

if(CONDITION WHERE id=="s5") {

    //do something

}

what should be this condition?

Upvotes: 0

Views: 2722

Answers (1)

JB Nizet
JB Nizet

Reputation: 691943

If I understand correctly, you want to know, in mobile.jsp, which phone link has been clicked. So you need to send a parameter to the JSP. The links should thus be

<a href="mobile.jsp?id=s5">Galaxy s5</a>
<a href="mobile.jsp?id=iphone">Iphone 4S</a>

And in the mobile.jsp, you need to test the value of the request parameter named "id". This should be done in a servlet, not in a JSP, with the following Java code:

String clickedPhoneId = request.getParameter("id");

The JSP shouldn't contain any Java code. It can use the JSTL and the EL to get a parameter value and test it though:

<c:if test="${param.id == 's5'}">S5 has been clicked</c:if>

Upvotes: 2

Related Questions