質問

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?

Like this:

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

<div> <!-- the forwarded page must inside here --> </div>
役に立ちましたか?

解決 2

Do in this way using include instead of forward in page.jsp.

<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.

Read more about JSP - The include Directive

他のヒント

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. 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.

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. 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.

The decision is up to you.

More info:

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top