Discussions

Web tier: servlets, JSP, Web frameworks: Include Servlet in JSP

  1. Include Servlet in JSP (4 messages)

    I wish to include a file in JSP, but the file is result from database query.
    Therefore, i would like to call a servlet in JSP using the following tag:
    <%@ include file="XXX" %>

    However, the result came out with the following exception:
     generated the following parse exception: org.apache.jasper.compiler.CompileException:
     Bad file argument to include

    You may suggest that i straightfowardly include another jsp file and inside that jsp
    file call the database to get the data and present it. However, i wish to it in separate
    tier (business tier) in my servlet and business class. I feel weird when my JSP creating
    connection to the database because it suppose to be the presentation layer.

    thanks.

    Threaded Messages (4)

  2. Include Servlet in JSP[ Go to top ]

    Hi,

    I don't think you can (or should) include a servlet in a JSP. You can do either of 2 things:

    - use the servlet directly (without the JSP)
    - from within the JSP, call a Java-class which responses with the desired data to display, eg:
     <%= someClass.someMethod(someArg) %>
  3. Include Servlet in JSP[ Go to top ]

    You should use <jsp:include page="XXX"/> instead of <%@ include file="XXX" %>. The latter just means merging before compilation, which apparently not what you want to do. 'jsp:include' actually includes response from XXX into 'parent' JSP response.
  4. Include Servlet in JSP[ Go to top ]

    Thanks for the through explanation.
    I had solved my problem.
  5. Include Servlet in JSP[ Go to top ]

    why don't you have the servlet do the DB call and then forward on to the JSP instead of including the Servlet call in JSP. That would be more of a MVC II paradigm.