0

Ok, I got this jsp file & it works ok

<html>
<head><title>Account</title></head>
<body>
<%@ include file="header.jsp" %>
</body>
</html>

Now, I got a servlet

 public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
   resp.getWriter().println("<html>"+

                    "<head><title>Account</title></head>"+
                    "<body>"+

                    "<%@ include file=\"header.jsp\" %>"+

                    "</body"+
                    "</html>"
    }

The servlet print out <%@ include file="header.jsp" %> as a text on the page & could not understand that it is a <%@ include file="header.jsp" %>.

How can let servlet to print out the result exactly like the jsp file I mentioned above?

Tum
  • 3,614
  • 5
  • 38
  • 63
  • 1
    Because a JSP file isn't the same thing as a response string. A JSP is turned into a servlet, and directives/tags do things. Here you're sending a string to the client-the client knows nothing about JSP. Why are you generating HTML in a servlet in the first place? – Dave Newton Jan 16 '16 at 07:42
  • @DaveNewton, how to avoid generating html from servlet? – Tum Jan 16 '16 at 07:51
  • Use JSP (or other view-layer technology). – Dave Newton Jan 16 '16 at 08:03

1 Answers1

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

But you should avoid this type of a situation.

m.aibin
  • 3,528
  • 4
  • 28
  • 47