Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a JSP as a template for a servlet?

I've been intermixing JSPs and Servlets in the web app I'm building and I'm starting to find that my more complex JSPs end up containing a lot of code, which flies in the face of all the MVC lessons that have been pounded into me. I know I can do this by just forwarding to the JSP, but this seems like a stupid hack.

What I'd like to do is use a servlet to do processing and then send a set of values to the JSP to render the HTML and return the response. Something along the lines of:

public class MyServlet extends HttpServlet {
 public void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws IOException {

         // ... Do some processing

         resp.getWriter.print(renderJSP("mypage.jsp", values));

    }

}

I've been poking around Sun's documentation and found this: http://java.sun.com/developer/technicalArticles/javaserverpages/servlets_jsp/ It seems like the JSP Model 2 architecture is exactly what I want to implement, but I cannot find an example of how one can set that up. For technical reasons, I cannot use one of the more advanced template frameworks like Struts.

Is this possible or a lost cause?

like image 804
Sean Lynch Avatar asked Nov 13 '09 20:11

Sean Lynch


People also ask

Can I use JSP in servlet?

In this mode, JSP pages are used for the presentation layer, and servlets for processing tasks. The servlet acts as a controller responsible for processing requests and creating any beans needed by the JSP page. The controller is also responsible for deciding to which JSP page to forward the request.

Why JSP is used instead of servlet?

JSP is slower than Servlet because the first step in the hasJSP lifecycle is the translation of JSP to java code and then compile. Servlet can accept all protocol requests. JSP only accepts HTTP requests. In Servlet, we can override the service() method.

Is JSP a template?

Templates are JSP files that include parameterized content.

Is JSP servlet still used?

One of the original Java web technologies, JSP is still widely used with servlets and JSTL. Here's how to use Jakarta Server Pages to build dynamic web pages that connect to the Java back end.


2 Answers

Put the object(s) in the request, forward the request to the jsp page and then use the object(s) in the jsp to render the response.

In your servlet,

MyObject obj = ... //build somehow
request.setAttribute("myObject", obj);
RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/jsp/my.jsp");
rd.forward(request, response);

If your result JSP should not be accessed directly from a URL you should hide it inside the WEB-INF directory where it can be accessed only through the forward directive.

Then on your jsp you can have,

<% MyObject obj = (MyObject) request.getAttribute("myObject"); %> 

To retrieve the object and used it as needed.

As others suggested, eventually it would be a good idea to learn to use JSTL and maybe an MVC framework like Spring MVC. The tutorial can be found here.

like image 72
Elliot Vargas Avatar answered Sep 27 '22 19:09

Elliot Vargas


Put Java objects in the Request/Response/Session and use a javax.servlet.RequestDispatcher in your servlet, something like that:

RequestDispatcher dispatcher = request.getRequestDispatcher("/test.jsp");
dispatcher.forward(request,response);

A forward is server-side and the target servlet/JSP receives the same request/response objects as the original servlet/JSP. Therefore, you can pass data between them using request.setAttribute().

The other option is to use response.sendRedirect(String location) which is client-side (this method sends a temporary redirect response to the client) so the location URL receives a new request from the client, and the only way to pass data is through the session or with web parameters (url?name=value).

This is basically what MVC frameworks do (and no, it's not a hack).

like image 25
Pascal Thivent Avatar answered Sep 27 '22 19:09

Pascal Thivent