• 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

sending back a URL

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
reponse.sendRedirect()
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



The person?
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yap. the person as in the end user who initiates the request.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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 ??



 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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??
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have the url in printwriter, client should be able to get it from the response.
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why all the HTML goo?
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok how would you write it ??
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your attitude has resulted in my interest in this issue dwindling.
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic