title: Servlet to jsp communication best practice
body: <p>I'm learning how to write java servlets and jsp pages on google app engine. I'm attempting to use an MVC model but I'm not sure if I'm doing it right. Currently, I have a servlet that is called when a page is accessed. The servlet does all the processing and creates a HomePageViewModel object that is forwarded to the jsp like this:</p>
  
  <pre><code>// Do processing here
  // ...
  HomePageViewModel viewModel = new HomePageViewModel();
  req.setAttribute("viewModel", viewModel);
  
  //Servlet JSP communication
  RequestDispatcher reqDispatcher = getServletConfig().getServletContext().getRequestDispatcher("/jsp/home.jsp");
  reqDispatcher.forward(req, resp);
  </code></pre>
  
  <p>Over on the jsp side, I have something like this:</p>
  
  <pre><code>&lt;%@ page contentType="text/html;charset=UTF-8" language="java" %&gt;
  &lt;%@ page import="viewmodels.HomePageViewModel" %&gt;
  &lt;%
    HomePageViewModel viewModel = (HomePageViewModel) request.getAttribute("viewModel");
    pageContext.setAttribute("viewModel", viewModel);
  %&gt;
  
  &lt;html&gt;
    &lt;body&gt;
    &lt;% out.println(((HomePageViewModel)pageContext.getAttribute("viewModel")).Test); %&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>So my question is two fold. First, is this a reasonable way to do things for a small webapp? This is just a small project for a class I'm taking. And second, in the jsp file, is there a better way to access the viewmodel data?</p>