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.
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.
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'"
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 ??
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??
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 ??