2

I have one application where i have three jsp pages, from index.jsp , control goes to process.jsp and after execution control goes to result.jsp to display data. But i want that instead of displaying data in result.jsp, control will go to another url so that that receiver url will get the requested data. that is: my url is 100.20.3.45:8085/myproject/index.jsp then after processing data i want that result should go to a different url of my same network i.e. 100.20.3.46. How can I send the requested data to this different url?

Ex:

100.20.3.45:8085/myproject/index.jsp

goes to

100.20.3.45.8085/myproject/process.jsp

after processing control will go to 100.20.3.46.

How can I send this data to a different url? what is this mechanism called?

talnicolas
  • 13,885
  • 7
  • 36
  • 56
sujit
  • 251
  • 5
  • 12
  • 23

2 Answers2

6

It's called "redirecting". It's to be achieved by HttpServletResponse#sendRedirect().

response.sendRedirect(url);

If you want to send additional data along, you'd need send it as request parameter(s) in a query string, but they will be visible in the URL in browser's address bar. If that isn't affordable, consider storing it in a shared datastore (database?) and then pass alone the unique key along.

Alternatively, you can also just let the <form> action URL point to that different host directly without the need for an intermediate JSP. As another alternative, you could also play for proxy yourself with help of for example URLConnection.


Unrelated to the concrete problem: having controller/business logic in JSPs is a bad practice. I suggest to take some time to learn about servlets.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • thanks for your reply but by response.sendRedirect(""), only it redirects but it does not contain any value to display, i am trying with this but getting any error: String nextJSP = "/100.20.3.46/cy/process.jsp"; RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP); dispatcher.forward(request,response); – sujit Dec 19 '11 at 05:41
  • 1
    You can't forward to a different host/domain. You can at highest forward to a different context. A real redirect is your best bet. – BalusC Dec 19 '11 at 05:44
  • That's unanswerable as long as the concrete functional requirement is vague/unclear. But it's indeed quite good possible that you're simply looking for a solution in the wrong direction. In the future, try to ask how to solve a problem instead of how to achieve a solution of which you thought that it is the right solution to the real problem. – BalusC Dec 19 '11 at 05:49
  • by response.sendRedirect("") i am only redirecting to a different page but can i show some value in that redirected page after processing? How it can be done? – sujit Dec 19 '11 at 05:50
  • As answered (did you check the updated answer?), just pass it as request parameter(s). – BalusC Dec 19 '11 at 05:51
  • ok i am elaborating my problem here: i have index.jsp and from index.jsp control goes to a servlet and after processing data that it has got from index.jsp page control will redirect to a different page let's say: response.sendRedirect("result.jsp"); but instead of redirecting to this process.jsp, I want that control eill redirect to a different host and will display data there. – sujit Dec 19 '11 at 05:53
  • Yes, I understood that. But why aren't you just submitting to that different host directly? Even more, why is the data supposed to be displayed on a different host in if that different host isn't involved in processing at all? – BalusC Dec 19 '11 at 05:57
  • ya thanks i got you...let me try..if any problem will occur i will ask – sujit Dec 19 '11 at 06:00
  • i am facing one problem here how can i execute in a different host if that host is not in my LAN? it is a host on different place from same dns server,that different host is some where else but when i am giving url of that different host in my action then error is coming that it could not connect to that host. – sujit Dec 19 '11 at 06:10
  • Well, then you've just a network problem. This is not solveable with any Java code. Just report it to the network admin and get him to solve it. – BalusC Dec 19 '11 at 11:35
0

Redirect to URL

window.location.href = "url"

Examples of use

window.location.href = "/process_payment";
var username = @json($username);
window.location.href = '/' + username;
window.location.href = '/{{ $username }}';
Gass
  • 7,536
  • 3
  • 37
  • 41