1

I have a servlet that is called from a link on another page. The link actually references the servlet which then SHOULD write xml to the screen (outputting RSS XML information). Right now the link properly references and loads the servlet but because I have the code in the doPost method with nothing actually calling the doPost method nothing happens. (I'm new to Java EE) So how do I make that code execute without actually have a form that references the servlet through the "action =.." tag?

Can I call an init or main method that always executes on page refresh/load?

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Randnum
  • 1,360
  • 4
  • 32
  • 48

2 Answers2

2

You can implement that logic in your doGet method. It has the same method signature as your doPost method.

Please see this thread

doGet and doPost in Servlets

For the difference between get vs post please see this article.

http://stevenclark.com.au/2008/01/12/get-vs-post-for-the-beginner/

Community
  • 1
  • 1
r0ast3d
  • 2,639
  • 1
  • 14
  • 18
  • Thank you, that worked great. I'll accept the answer in 4 minutes after the timer allows me to. – Randnum Nov 07 '11 at 23:23
1

You can also override Servlet.service method which is entry point for serving requests. This way you will handle both POST and GET requests.

Alternatively, you can implement logic in doGet method and invoke doGet from doPost:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
    // do request processing
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  
{
    doGet(request, response);
}
Igor Nikolaev
  • 4,597
  • 1
  • 19
  • 19