0

I just don't know how to put the response of the RequestDispatcher inside the html body of my jsp. That is my model:

RequestDispatcher view = request.getRequestDispatcher("/Overview.jsp");
    view.forward(request, response);
    PrintWriter out= response.getWriter();

    response.setContentType("text/html");

    if (count!=0){
        int i = 1;
        while (i <= count){
            out.println("<div>Name: "+batch.getName()+"</div>");
            i++;
        }
    }
    else{
        out.println("<div><font colour=blue>No Batch</font</div>");
    }

When I run the Servlet my jsp looks something like this:

<html>...
<body>
....
</body>
</html>
<div>....

How am I able to put the divs inside of the body?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Herrids
  • 3
  • 3
  • That's not how it's supposed to be done (the composition of the page to be shown will be done in the JSP based on variables prepared in the servlet, BEFORE calling the dispatcher), I highly recommend you read [a good tutorial on JSP](https://docs.oracle.com/javaee/5/tutorial/doc/bnagx.html) – fvu Jul 11 '15 at 13:34

1 Answers1

0

Ideally,You should forward the request to a JSP for display. JSP is a view technology which provides a template to write plain vanilla HTML/CSS/JS in and provides ability to interact with backend Java code/variables with help of taglibs and EL. see Generate an HTML Response in a Java Servlet

You have mixed two approaches using JSP as view and also writing html content in servlet which is bad practice as its full of clutter

still if you wanna to for second approach. you should remove below lines

RequestDispatcher view = request.getRequestDispatcher("/Overview.jsp");
    view.forward(request, response);
Community
  • 1
  • 1
M Sach
  • 30,322
  • 72
  • 198
  • 300