1

I am stuck with this problem from last few hours. I am using Servlets and jsp pages. In the below code I used scriplet to use values passed to jsp page from servlet.

display is a string of array in the form of id, image-path, id2, imag-path2, id3, image-path3, id4 ......

I am using these images as a href. Now I need to pass id values to the href servlet.

Could you please suggest what is the best way I can achieve that?

<div class="get-images-class">
                <%
                   String[] display = (String[])request.getAttribute("images");
                   if (display != null && display.length != 0)
                   {
                        for(int i=0; i<display.length; i++)
                        {   out.println(display[i]);
                            i++;%>
                            <a href="ImageBrowseServlet"><img src="<%=display[i]%>" alt="image missing"></a>
                        <%}
                   }
                %>
            </div>
halfer
  • 19,824
  • 17
  • 99
  • 186

3 Answers3

2

u can use url passing parameters

<a href="ImageBrowseServlet?val1=A&val2=B"> <img src="<%=display[i]%>" alt="image missing"> </a>

in servlet

String value1 =  req.getParameter("val1");
String value2 =  req.getParameter("val2");
Sandun Chathuranga
  • 2,242
  • 2
  • 13
  • 27
0

Simply pass the value in url parameter with servlet like-

<div class="get-images-class">
                <%
                   String[] display = (String[])request.getAttribute("images");
                   if (display != null && display.length != 0)
                   {
                        for(int i=0; i<display.length; i++)
                        {   out.println(display[i]);
                            i++;%>
                            <a href="ImageBrowseServlet?id=<%=display[i]%>"><img src="<%=display[i]%>" alt="image missing"></a>
                        <%}
                   }
                %>
            </div>
Altmish-E-Azam
  • 1,561
  • 1
  • 13
  • 24
0

Use

<img src="ImageBrowseServlet?Id=<%=display[i]%>" alt="image missing">

assuming your servlet has parameter Id to receive sent value like this :-

String Id=request.getParameter("Id");
Gaurav Mahindra
  • 424
  • 2
  • 6
  • 21