• 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

Process both Get and Post requests in the Same Servlet

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I've written an HTTPServlet that send an HTTP request to one HttpListener and receive POST requests from an Http Sender.

This is the code:



When processRequest method is called from doGet I can see:


but when is called from the doPost method it seems that the html content is not sent from the processRequest method.

Any piece of advice would be welcomed.


Thanks!!
 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Having a single method handle both GET and POST is *bad*. Really bad. It likely violates the HTTP specification, and will get you into trouble with HTTP handling components that treat them differently. The simple rule to follow is: do not do it.

NetBeans is the usual culprit for this sort of bug; it should be taken out back and shot for that.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:It likely violates the HTTP specification, and will get you into trouble with HTTP handling components that treat them differently.



Actually, in JEE, an incoming request invokes the HttpServlet service() method, which looks at the the request type, and invokes doGet() if it's a GET and doPost() if it's a POST.

The actual parameters are normalized, so if you make doGet and doPost invoke a common method, you're OK as long as it doesn't attempt to reference resources not appropriate for the request type.

On the other hand, it really isn't a good practice overall. Too much chance that as time goes by someone will start adding request-type specific conditionals into the common method.

One reason why I might combine the two would be something where I'd be doing a "ping" request as a GET with limited parameters, but a POST for a full-function operation or one that had the potential for extended-length parameters. But on the whole, it would be better to have separate handlers for stuff like that.
 
Alba Garcia
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I am a little bit lost.

I will try to show you my scenary:

1. JSP index


2. Servlet Send that make and http request and when it get the response pass the control to another Servlet "Receive"


3. Servlet Receive print the cdsOutput request parameter


My problem is that I only would like to show the JSP when the cdsOutput parameter is not null but debbuging I can see that the first time doGet is called I see the JSP with the text csOutput is null but the second time that this method is called cdsOutput is not null but the JSP is not displayed.

How could I do it?

Thanks in advance!

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but the JSP is not displayed


I'm confused. The only difference the parameter "cdsOutput" makes is in what gets returned as the page content. It has no influence over displaying any JSP. In fact, the Receive servlet method does not use any JSP. Based on that code, how where you expecting any JSP to be displayed?

What also confuses me is how "cdsOutput" could ever have a non-null value, since neither the form submit in the HTML page, nor either of the two servlets, ever set any value for it.
 
Alba Garcia
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The cdsOutput value comes from another application (an ESB) that send it via this url: http://localhost:58080/decision-support/Receive via GET method.

It seems to me that firstly Receive doGet method is invoked because of the code

inside the Send Servlet.

And the second time that this method is invoked is because of the request of the other app.

So I can see the a JSP with a text the first time but not the second becaus the context is lost. How could avoid the lost of the context?

¿Could I wait inside the same servlet until the cdsOutput parameter is null someway?

Thanks!!!
 
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
May be you should use variable as in "Receive" servlet and then append the values in it.
And there is no JSP displaying the output after you "Receive" servlet. It is just the servlet flusing the output stream and giving the response.
 
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic