Reputation: 1651
I have two server and i can call jsp of another server from my server's jsp.
like following code. First Server JSP.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<form method="post" action="http://localhost:8080/Second_App/index.jsp">
Name : <input type="text" name="name"/>
Surname : <input type="text" name="surname"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
When i click on Submit the control will go in second server it will take name as parameter and put it into my Second Server's jsp.
Second Server JSP.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%
String name = (String) request.getParameter("name");
String surName = (String) request.getParameter("surname");
%>
Name : <%= name %>
Surname : <%= surName %>
</body>
</html>
i want to do exact same thing using Servlet.
I tried with Servlet's Redirect my control will go to Second server but because of Redirect it will not take "name" peramater.
I tried with Forward but it is also not working because it is finding that jsp in first server.
RequestDispatcher dispatcher = request.getRequestDispatcher("http://server2/app1/index.jsp");
dispatcher.forward(request, response);
My concern is JSP is Servlet. If this is done with jsp that means there should be some way for doing it with servlet.
Thanks.
Upvotes: 0
Views: 5091
Reputation: 23352
Send you form parameter from Servlet in this way. Next JSP will get name parameter
from request.
RequestDispatcher dispatcher = request.getRequestDispatcher("http://server2/app1/index.jsp?name=setUserNameHereFromRequest");
dispatcher.forward(request, response);
With sendRedirect
response.sendRedirect("http://server2/app1/index.jsp?name=setUserNameHereFromRequest");
Upvotes: 1
Reputation: 901
You can redirect post requests using the HTTP 307, temporary redirect.
response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT );
response.addHeader("Location","target/example.jsp");
You can find more detailed information on this topic here:
Response.Redirect with POST instead of Get?
Upvotes: 0
Reputation: 487
You can use response.sendRedirect("url_where_to_redirect");
Here the current request will get end and it will redirect to another context.
But here you can call one external server url that's all. I think you need to show page from second server according to your request with url from your current page. If you need that then you should use some code to handle the request in the second server application.
Upvotes: 0
Reputation: 6479
You can try with sendRedirect()
This method is used to redirect client request to some other location for further processing ,the new location is available on different server or different context. The web container handle this and transfer the request using browser, and this request is visible in browser as a new request. Some time this is also called as client side redirect.
response.sendRedirect("http://server2/app1/index.jsp");
Upvotes: 0