简体   繁体   English

在HTML div中从Servlet转发页面

[英]Forward page from Servlet inside a HTML div

I'm trying to make my page more efficient and so I got the question like below: 我试图使我的页面更高效,所以出现了如下问题:

Is it possible to forward a new Jsp page with RequestDispatcher into an HTML div from the Servlet? 是否可以将带有RequestDispatcher的新Jsp页面转发到Servlet的HTML div中?

Like this: 像这样:

someServlet.java someServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException, ServletException {

     // do something to send a page to the HTML div in page.jsp  
}

page.jsp page.jsp

<div> <!-- the forwarded page must inside here --> </div>

That's not possible. 那不可能 You can, instead, make an ajax call to your servlet and the servlet may return the necessary data in a format that will help the view to work. 相反,您可以对servlet进行ajax调用,并且servlet可能以有助于视图工作的格式返回必要的数据。 An example is by writing the response using JSON format, then handle this JSON response in JavaScript to add the necessary data in your view accordingly. 一个示例是通过使用JSON格式编写响应,然后在JavaScript中处理此JSON响应以在视图中相应地添加必要的数据。

You could also return a "text/html" response where you write the content of the <div> with the necessary content to just write the HTML response directly in your view. 您还可以返回“ text / html”响应,在其中编写<div>的内容以及必要的内容,以便直接在视图中直接编写HTML响应。 The response used in this approach is easier to use in your view (JSP), but note that will couple your Servlet request to mere HTML responses only. 这种方法中使用的响应在您的视图(JSP)中更易于使用,但是请注意,这会将您的Servlet请求仅耦合到纯HTML响应。

The decision is up to you. 该决定由您决定。

More info: 更多信息:

Do in this way using include instead of forward in page.jsp . 以这种方式使用include而不是page.jspforward

<div> <!-- insert any one --> </div>

Try any one 尝试任何一个

<jsp:include page="includedPage" />

OR 要么

<jsp:directive.include file="relative url" />

OR 要么

<%@ include file="relative url" >

OR 要么

request.getRequestDispatcher("/WEB-INF/jsp/abc.jsp").include(request, response);

If a page is forwarded then it's the responsibility of the forward page to generate the HTML content but in case of include you can combine multiple HTML/JSP/Servlet outputs to generate the final HTML content. 如果转发了页面,则转发页面负责生成HTML内容,但是如果包含页面,则可以合并多个HTML / JSP / Servlet输出以生成最终的HTML内容。

Read more about JSP - The include Directive 阅读有关JSP的更多信息-包含指令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM