5

First, Please suggest me if my question heading is not correct.

Moving on to question: Say I am having below components:

search.jsp - A JSP Page with a Form to Submit Data

Search.java - A controller Servlet having both get() and post() defined separately so that it can acts as a dispatcher for path /search.jsp

searchResults.jspf - A Fragment with some JSTL code to show up the Search Results

What I want here is for every POST request the controller servlet has to do its calculation, set results as Request Attributes and than - forward the request to the view search.jsp that should include the Fragment after its own codes.

So that, I can have a View Defined in such a way as:

search.jsp
+
searchResults.jspf

on a single page.

Problem is, I can either do Forward or Include with the dispatcher as I don't know how can i Include a fragment while forwarding to a JSP into it.

Let me know if I need to post some code if necessary, or need any corrections.

Asif
  • 4,980
  • 8
  • 38
  • 53

2 Answers2

4

In your search.jsp embed your searchResult.jsp using jsp:include:

<jsp:include page="searchResult.jsp"></jsp:include>

Exemple: 1. The servlet:

@WebServlet(name = "Servlet", urlPatterns = "/myForwardTest")
public class Servlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("search.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          doPost(request, response);
    }
 }
  1. search.jsp:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
       <title>In search resust</title>
    </head>
    <body>
     Search.jsp embed searchResult.jsp
    <jsp:include page="searchResult.jsp" />
    </body>
    </html>
    
  2. searchResult.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <body>
      in searchResult
    </body>
    </html>
    
Community
  • 1
  • 1
fabien t
  • 358
  • 1
  • 9
  • How can I make sure it gets included *only* when coming from Servlet's POST method?? – Asif Nov 04 '15 at 16:20
  • have you tried it? When you forward the request from controler, at this time i don't think there's a difference between post and get ... how do you forward your request ? – fabien t Nov 05 '15 at 17:25
  • I tried it, no problem at all.if you want i have am exemple? – fabien t Nov 05 '15 at 17:52
  • Thank You, I got a way around, I used JSTL `c:if` that looks for a `requestAttribute` say `showResult` , if comes `true` it will `include` the `searchResults.jspf` else nothing will happen, when forwading Request through GET, I am setting it as `false` in servlet's `doGet()` method, whereas I am setting it as `true` in `doPost()`!! I think its a right way. . Cheers :) – Asif Nov 09 '15 at 18:22
1

You can include your jspf in your jsp like below:

<%@include file="searchResult.jspf" %>

you can set a statement to execute a certain section only if a particular test evaluates to true .

Ex:

if(.....==true){
<%@include file="searchResult.jspf" %>
}else{
<%@include file="someOther.jspf" %>
}
Madushan Perera
  • 2,568
  • 2
  • 17
  • 36
  • I would like to use JSTL or EL tags rather than JSP code, if you could help me pls. . . – Asif Nov 04 '15 at 16:21
  • You can learn JSTL and EL more through these links given below. They have given examples for using each and every tag in JSTL. **JSTL-->** http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm **EL-->** http://www.tutorialspoint.com/jsp/jsp_expression_language.htm – Madushan Perera Nov 06 '15 at 05:30