• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Linking to servlets using href

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a JSP page which contains 3 links shown below
<a href=".../MyServlet">Function1</a>
<a href=".../MyServlet">Function2</a>
<a href=".../MyServlet">Function3</a>
and they reference the same Servlet(viz MyServlet). I was wondering if the Servlet(MyServlet) is capable of identifying which link is clicked. I know it possible using forms but i wanted to know if i could associate a unique action for each link. Any kind of help would be appreciated.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:
<a href=".../MyServlet?action=function1">Function1</a>
<a href=".../MyServlet?action=function2">Function2</a>
<a href=".../MyServlet?action=function3">Function3</a>
and at your servlet 'MyServlet.java' try this:
public class MyServlet extends HttpServlet(){
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletExcetption,IOException{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String function=request.getParameter("action"); //this is that parameter we passed within the link

if(function.equals("function1")){
//code for function 1
}else if(function.equals("function2")){
//code for function 2
}else if(function.equals("function3")){
//code for funtction 3
}
}
}
reply
    Bookmark Topic Watch Topic
  • New Topic