Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Servlet Query  RSS feed

 
Paul Herbert
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm new to java and java servlets but I have been thrown into the deep end with this.

I am required to write a servlet that does the following:-

Accept a parameter (part of a url) from an application
Forward the parameter to another external url
Read the response from the external url to an xml file.

Can anyone point me in the right direction to achieve this? Ordinarily I would take my time and read through lots of tutorials before attempting this but on this occasion time is not on my side and I've only got a couple of weeks to try to complete this Any help/examples will be most welcome.

The code that I've managed to cobble together so far is...

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

I've gone ahead and added the code tags for you. See how much easier the code is to read?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you need to generate a new request to an external resource, and get its response, a forward is not the correct approach. You'll need to either generate a new request (and fetch its response) via the classes in java.net, or make it easy on yourself and use a 3rd-party library such as HttpClient.

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand what you are trying to do, you do not want to forward the request, I think you would get all tangled up due to trying to write to the response in two places.

Instead you should generate a new java.net.HttpURLConnection using the parameter supplied, execute that request and read the response in your servlet, then dispose of the result as needed.

Where does the "xml file" reside?

Bill

 
Paul Herbert
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Welcome to the Ranch.

Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

I've gone ahead and added the code tags for you. See how much easier the code is to read?


Thanks for that....
 
Paul Herbert
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William Brogden wrote:If I understand what you are trying to do, you do not want to forward the request, I think you would get all tangled up due to trying to write to the response in two places.

Instead you should generate a new java.net.HttpURLConnection using the parameter supplied, execute that request and read the response in your servlet, then dispose of the result as needed.

Where does the "xml file" reside?

Bill



The xml file will reside on the application server. For my dev purposes it will be on the root of my C:\ drive.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!