wood burning stoves*
The moose likes Servlets and the fly likes sending back a URL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "sending back a URL" Watch "sending back a URL" New topic
Author

sending back a URL

nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
hi all,
i was wondering how to send back a URL from my servlet ?? i have a servlet which needs to generate a URL which it needs to send back to the requester who will then redirect the browser to this new URL.

any suggestions would be appreciated.

ta.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

reponse.sendRedirect()


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
nope. that's not going to work. sendRedirect() will just redirect the request to my servlet to the new URL. what i need is to send a URL back for the initial request and they will redirect the user to that URL constructed by my servlet.

1) my servlet will receive a request.
2) my servlet will construct a URL
3) my servlet will send this URL back in response
4) the person who request the URL in step one will then redirect user to the URL.

so i wanted to know how to do step 3.
samir singha
Greenhorn

Joined: Mar 17, 2008
Posts: 23
Here is your step by step answer.
1) use request.getParameter() to obtain the parameters from the request.
2) construct the url based upon your request parmeter or whatever procedure you want to construct the url and store the url in a String variable.
3) set the reuests attribute by using request.setAttribute() or request.getSession().setAttribute() as per your scope requirement.
4) use RequestDispatcher or response.sendRedirect() to send the url to the client page.
5) retrive the url using request.getAttribte() method.
6) Redirect the url using response.sendRedirect or html's "location.href='http://newurl'"

Hope this will help you.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

nikil shar wrote:4) the person who request the URL in step one ...


The person?
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
yap. the person as in the end user who initiates the request.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18709
    
    8

Does that mean you want to send back an HTML page containing an element which is a link to that URL so that person can click on the link if they so choose?

If so then what specifically is your problem with doing that?
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
am trying to construct a URL and send it back to another servlet. so heres the flow :

end user clicks on a link on their browser.

1) some servlet which is meant to handle the request makes a call to my servlet.
2) my servlet is meant to construct a URL and send it back to that servlet
3) that original servlet is meant to redirect the end user to this URL.


so i know how to construct a URL, am just not sure how to send it back to the original servlet.

i tried setting the url object as a string as an attribute :



but they are getting a 'nullpointer' when they try request.getAttribute("url");

so am not sure if its because i have already responded to the request in the redirect() method which makes the attribute null or whats going on ??



Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

whats going on ??

That's what I also want to know . How did your servlet was called by "their" servlet?? Was it using a requestDispatcher.include?? In your code, you've written setAttribute("url", url);, where did this setAttribute method come from?? Try calling request.setAttribute("url",url). If what you are supposed to do is just return a URL, then you should not redirect the request. The servlet that called your servlet is supposed to do that. And why are you using a servlet to do this in the first place. Why don't you just create a method which returns a String and call it from the other servlet??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
the other servlet is being written by another team which is deployed in a seperate server/container and i dont know how they are calling my servlet. have tried request.setAttribute(); but still same issue.

i know how to return a URL within html tags but i am not sure how to return a url as a string which this other servlet would be able to read ??

is what i want to do really hard to do with servlets ??
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18709
    
    8

nikil shar wrote:i know how to return a URL within html tags but i am not sure how to return a url as a string which this other servlet would be able to read ??


Then just return the URL without any HTML tags around it. Write the URL to the response's output stream and nothing else.
Arun Kumar
Ranch Hand

Joined: Oct 10, 2007
Posts: 30
Have the url in printwriter, client should be able to get it from the response.
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
ok just for my understanding, how would the other team read my URL if i send it in a printwriter ??

e.g if my code is like




how would the other team read this in order to get the URL ??


thanks for all your help so far.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

Why all the HTML goo?
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
ok how would you write it ??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

Your attitude has resulted in my interest in this issue dwindling.
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
no seriously i was wanting to know how would you write it ?? i can only code according to how i know so someone elses point of view would be much appreciated.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18709
    
    8

Apparently you're serious. Okay. Here's how I would write it:


But I would also ask the person who asked me to do this a question like "What do you mean by returning a URL?"
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
but thats how i have written mine as well so whats the difference ?? and my sincere apologies if my comments came out as negative, it wasn't intentional.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

nikil shar wrote:but thats how i have written mine as well so whats the difference ??


You really don't see a difference between:

and

?
nikil shar
Ranch Hand

Joined: May 25, 2008
Posts: 116
ah now you pointed it out its obvious hey thanks a lot for your help and please accept my apology for any distress caused by my previous comments.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sending back a URL