Java 如何将 servlet 的响应重定向到我们收到请求的同一个 jsp 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18831659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:28:00  来源:igfitidea点击:

How to redirect the response of a servlet into the same jsp page from where we got the request

javaspringjspservletsmodel-view-controller

提问by user2266817

How can I redirect a response of a servlet to the same jsp page from where I get the request. Suppose I have a tab called status on my jsp page http://localhost:8080/Example/status.jsp. Now when I send a request and when I get a response,it should display the response on the same page, ie. it should show response on
http://localhost:8080/Example/status.jsp. But it is showing me the response in
http://localhost:8080/Example/Statuswhere Status is the url-pattern in web.xml file. Please any help is appreciated. below is my code. How do I get the response in status.jsp.

如何将 servlet 的响应重定向到我收到请求的同一个 jsp 页面。假设我的 jsp 页面上有一个名为 status 的选项卡 http://localhost:8080/Example/status.jsp。现在,当我发送请求并收到响应时,它应该在同一页面上显示响应,即。它应该在
http://localhost:8080/Example/status.jsp上显示响应。但它向我展示了响应,
http://localhost:8080/Example/Status其中 Status 是 web.xml 文件中的 url-pattern。请任何帮助表示赞赏。下面是我的代码。如何在 status.jsp 中获得响应。

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws 
IOException {

             try{

                 String showstatus =req.getParameter("showstatus");
                 PrintWriter out = res.getWriter();   
                 Runtime rt = Runtime.getRuntime();
                 Process pr = rt.exec("C:\tools\server\util stat -a" );
                 BufferedReader stdInput = new BufferedReader(new   
InputStreamReader(pr.getInputStream()));

                BufferedReader input = new BufferedReader(stdInput);
                String stat = "";
                    while((stat = input.readLine()) != null){
                        out.println(stat);
                    }
             }
                catch (Throwable t)  
                  {  
                    t.printStackTrace();  
                  }  


                finally {  

                }  }

JSP CODE:

代码:

<div id= "status" style="display:none;">
            <FORM action="status" METHOD="POST">
                <input type=submit name=showstatus 
id=txtSubmit value=Status />

            </FORM>
        </div>

WEB.XML:

网页.XML:

<servlet>       
    <servlet-name>Status</servlet-name>
    <servlet-class>com.emc.clp.license.Status</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Status</servlet-name>
    <url-pattern>/status</url-pattern>
</servlet-mapping>

采纳答案by Tap

In your servlet, rather than printing the output of utildirectly to the response, hold it in memory and pass it to the jsp:

在您的 servlet 中,不是将 的输出util直接打印到响应中,而是将其保存在内存中并将其传递给 jsp:

String stat;
StringBuffer utilOutput = new StringBuffer();
while((stat = input.readLine()) != null){
    utilOutput.append(stat + "\n");
}
req.setAttribute("utilOutput", utilOutput.toString());
req.getRequestDispatcher("/Example/status.jsp").forward(req, res);

In your jsp, I assume you have a div or something where you want to see the output:

在您的 jsp 中,我假设您有一个 div 或想要查看输出的内容:

<div id= "status" style="display:none;">
    <form action="/status" method="POST">
        <input type="submit" name="showstatus" id="txtSubmit" value="Status" />
    </form>
</div>
<div id="result">
    <pre>
        ${requestScope.utilOutput}
    </pre>
</div>

Notice that I added a forward slash in the form action. If your user is submitting the form from /Example/status.jsp, then the relative path would have the post going to /Example/status, but your servlet only handles requests to /status.

请注意,我在表单操作中添加了一个正斜杠。如果您的用户从 提交表单/Example/status.jsp,则相对路径会将帖子发送至/Example/status,但您的 servlet 仅处理对/status.

Your request object, req, has a map of attributes, and you can put any relevant objects in it using the setAttribute() method on the request. The request dispatcher forwards your request and response objects on to another resource (your jsp) for processing. The jsp can access these values by name, using the EL notation above.

您的请求对象req具有属性映射,您可以使用请求的 setAttribute() 方法将任何相关对象放入其中。请求调度程序将您的请求和响应对象转发到另一个资源(您的 jsp)进行处理。jsp 可以使用上面的 EL 表示法按名称访问这些值。

The display:nonein your inline style will always prevent that form from being seen on the page if it loads that way. I think the most apparent way to display it would be with javascript on the client side. You'd need to change the display value when the tab is clicked. You don't mention what your tab elements are, but let's say they're divs. You could write a javascript function to load the form, or you could even do it all inline:

display:none你的内联样式将始终防止形式被看到的页面上,如果它加载的方式。我认为最明显的显示方式是在客户端使用 javascript。单击选项卡时,您需要更改显示值。你没有提到你的标签元素是什么,但假设它们是 div。您可以编写一个 javascript 函数来加载表单,或者您甚至可以内联完成所有操作:

<div id="mytab" onclick="document.getElementById('status').style.display = 'block';">Load the form</div>