aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Linking to servlets using href Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Linking to servlets using href" Watch "Linking to servlets using href" New topic
Author

Linking to servlets using href

Subil Abraham
Greenhorn

Joined: May 21, 2003
Posts: 4
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

Joined: Nov 07, 2002
Posts: 298
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
 
subject: Linking to servlets using href