Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java Servlet

Status
Not open for further replies.

crmayer

Programmer
Nov 22, 2002
280
US
I have been looking to see if there is a way to "change" the parameters that are passed to a servlet.
Example:
XXX and YYY are location codes)

What I want to do, is when this string comes to my servlet, I would like to lookup XXX and YYY in our database, and replace them with the actual address of the location.

The new string would be:
2nd ave, chicago, il&dest=456 3rd st, omaha, ne

If this is not possible, I could possible also pass the parameters in with a different name (param1, param2) and then append origin, dest to the end and possibly ignore the param1 and 2 when the servlert works with them.

Probably kind of silly to be doing all this, but I need to replace those values from the pages reaches the client browser.

Thanks in advance
- Chris
 
Changing the request parameters will have no affect whatsoever.

These parameters are prepared afresh with each client/server request and certainly do not get sent back to the client. You need to build a response containing the new values which the servlet then writes out via the ServletResponse (or HttpServletResponse for http servlets) object. For example, generate the content for a new web page containing your return information and send this (replacing the one currently in the client's browser).

If you are submitting the request from a web page, and if you don't want to replace the current web content of the client browser, you might want to look at using the Ajax technique, although this is more advanced you may need to strengthen your understanding of regular web client/servlet techniques first.

Tim
 
I don't understand the question.

Who is attending the first request? It it's the servlet, you can use the data there as you want

Where is the request being redirected after the servlet processing? Back to the client or to another system.

I don't understand why do you want to do such a transformation when you can do all the job in the servlet

Cheers,
Dian
 
I read it as "I want to return a response to the client by changing the values in the request parameters". cymayer, can you please clarify your stated problem so we can assist?

Tim
 
We are creating a web page for the client from a servlet. In this web page there are two fields that the client will fill out (XXX and YYY). They then will submit this to us.

We the servlet receives this request, we would look up the XXX and YYY against our database and see if they are valid location codes, if they are, we would get the pysical street addresses for them and then send them back to the client, where we have a javascript inbeded in the web page (Google Maps) that would send those off to their server to get the directions.

As I type this out, I start to think why I am having a problem. If the user is sending us the parameters, we should be able to do what we want with them and send them back.
The one thing that comes to mind is that they might not be sending us "location codes" but actual physical street addresses, at which time we would just turn around and send back so the google maps javascript can get the directions for the addresses entered.

I think where I was maybe having issues was that I wanted to update the url and send it back as the same page that they filled the fields out in, with the updated data of course (physical addresses, instead of location codes). Which maybe that is where I just rebuild that identical page with the addresses opposed to the location codes????

Or maybe do some type of redirecting where I pass the info back to some "other page" and let the google script handle it from there?

Maybe I have the original servlet called again with a runmode variable or a flag that I can look at and determine what the servlet needs to do, opposed to doing what it did the first time to create that page...

Maybe what I really need here is advice on what the best way to do this is?

Thanks so much for the responses, and If I am still unclear in my explaination, please let me know. Some things are hard to explain via typing....

Thanks again.
- Chris
 
What about some AJAX stuff? You invoke the servlet via a XMLHttpRequest object, return and XML with data (streets, codes ot whatever you need) and use javascript to pass the parameters to google.

Cheers,
Dian
 
I am not familar with that, so I will do some investigating of it.
I want to say that Google maps has an AJAX api, so that is probably the thing to seriously look at.
 
The biggest headache with Ajax is deciding which Ajax-library provider you want to use. Or if you are a JavaScript wiz, to do it all yourself.
Basically, as Dian has said, you use the XMLHttpRequest object in Javascript in the browser to make an http request on the server. The servlet on the server gets this http request and looks at the parameters (as you already have). The servlet does whatever processing it needs to do and then wraps the data it wants to return into an XML packet and sends this back to the browser via the output stream courtesy of the HttpServletResponse object. More Javascript in your web page then extracts this data from the XML is receives and does whatever it needs to with it:- update some visible content/fields, or make a new request on some other service with it (eg. Google Maps).

Tim
 
Thanks for all the replies. I have been testing this Ajax option and so far it seems to be working.

I do see what you are saying timw, I have had to pretty much redo all my javascript and servlet code, but then again, just more practice:)

I will update you if I run into any other problems, but it really seems like this might be the best option, at least with working with Google Maps.

Thanks again....

- Chris
 
I am glad you have found a way forward. Post back here if you get into any more difficulties.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top