簡體   English   中英

從“我的服務器”調用另一台服務器的JSP

[英]Call JSP of another server from My Server

我有兩個服務器,我可以從服務器的jsp調用另一個服務器的jsp。

像下面的代碼。 第一服務器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>

當我單擊“提交”時,控件將進入第二台服務器,它將名稱作為參數並將其放入我的第二台服務器的jsp中。

第二服務器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>

我想使用Servlet做完全相同的事情。

我用Servlet的Redirect嘗試過,我的控件將轉到第二台服務器,但是由於Redirect的原因,它不會采用“名稱”設置。

我嘗試使用Forward,但是它也無法正常工作,因為它在第一台服務器中找到了該jsp。

RequestDispatcher dispatcher = request.getRequestDispatcher("http://server2/app1/index.jsp");
        dispatcher.forward(request, response);

我關心的是JSP是Servlet。 如果使用jsp完成此操作,則意味着應該有一些使用servlet的方法。

謝謝。

通過這種方式從Servlet發送表單參數。 下一個JSP將從請求中獲取name parameter

RequestDispatcher dispatcher = request.getRequestDispatcher("http://server2/app1/index.jsp?name=setUserNameHereFromRequest");
        dispatcher.forward(request, response);

與sendRedirect

response.sendRedirect("http://server2/app1/index.jsp?name=setUserNameHereFromRequest");

您可以使用response.sendRedirect(“ url_where_to_redirect”);

在這里,當前請求將結束,並將重定向到另一個上下文。

但是在這里,您可以僅調用一個外部服務器URL。 我認為您需要根據您的請求顯示第二台服務器的頁面,並顯示當前頁面的網址。 如果需要,則應使用一些代碼在第二個服務器應用程序中處理請求。

您可以嘗試使用sendRedirect()

此方法用於將客戶端請求重定向到其他位置以進行進一步處理,新位置可用於不同的服務器或不同的上下文 Web容器對此進行處理並使用瀏覽器傳輸請求,並且該請求在瀏覽器中作為新請求可見。 有時也稱為客戶端重定向。

response.sendRedirect("http://server2/app1/index.jsp"); 

您可以使用HTTP 307(臨時重定向)重定向發布請求。

response.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT );  
response.addHeader("Location","target/example.jsp");

您可以在此處找到有關此主題的更多詳細信息:

Response.Redirect用POST代替Get?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM