File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Linking to servlets using href

 
Subil Abraham
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • 0
  • Mark post as helpful
  • send pies
  • 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
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic