• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Forwarding from doPost to doGet

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a login servlet that, as the name would suggest, handles the login functionality of my webapp. If the login is approved, I'm currently forwarding to a static JSP page, but I would rather forward to another servlet that generates a list and let the user proceed from there.

The servlet to generate the list already works, as does the login servlet, it's just a matter of forwarding from one to the other.

The login servlet only has a doPost method, the doGet just returns the request and response objects. In the other servlet, we'll call MyServlet, it uses both doPost and doGet and the listing functionality exists within the doGet method.

Everytime I try to use the RequestDispatcher to forward to the servlet using my servlet mapping, I get an error. Is it possible to go from doPost to doGet? Or would I have to duplicate the list code inside the doPost method?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the request type (GET, POST, etc...) is determined before your first servlet is ever called and is part of that request for the entire cycle.

A simple way to work with this is to combine your doPost and doGet methods (just have one call the other) so it won't matter which is called.

A more complex way of doing this is to wrap the request object by extending HttpRequestWrapper and overriding getMethod.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequestWrapper.html

If you add a setMethod or changeMethod method, you could change the requests method, and pass the wrapper to the next servlet as an argument to RequestDispatcher's forward method.
 
Jason Kwok
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ben,

Thanks for the advice, I'll check your suggestion out. I've never done something like that before and it sounds rather interesting.

Thanks,
Jason
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic