Q: How to avoid Java Code in JSP-Files?

D: I'm new to Java EE and I know that something like the following three lines
<%= x+1 %>
<%= request.getParameter("name") %>
<%! counter++; %>
is an oldschool way of coding and in JSP version 2 there exists a method to avoid Java code in JSP files. Can someone please tell me the alternative JSP 2 lines, and how this technique is called?

Test Case #4


File ID: #3180202-12-cc


   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        List products = productService.list(); // Obtain all products.
        request.setAttribute("products", products); // Store products in request scope.
        request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response); // Forward to JSP page to display them in a HTML table.
    } catch (SQLException e) {
        throw new ServletException("Retrieving products failed!", e);
    }
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    User user = userService.find(username, password);

    if (user != null) {
        request.getSession().setAttribute("user", user); // Login user.
        response.sendRedirect("home"); // Redirect to home page.
    } else {
        request.setAttribute("message", "Unknown username/password. Please retry."); // Store error message in request scope.
        request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response); // Forward to JSP page to redisplay login form with error.
    }
}
     
   protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
    try
    {
        IAction action = ActionFactory.getAction(request);
        String view = action.execute(request, response);
        if (view.equals(request.getPathInfo().substring(1)))
        {
            request.getRequestDispatcher("/WEB-INF/" + view + ".jsp").forward(request, response);
        }
        else
        {
            response.sendRedirect(view);
        }
    }
    catch (Exception ex)
    {
        LOGGER.error(ex.getMessage());
    }
}

  1. +1 Great answer. But don't go dogmatic, sometime using scriptlets IS ok, but that should be the exception that proves the rule.
  2. It was a pretty good answer, but the doGet and doPost parts are misleading. Those methods are for handling specific request (HEAD, GET and POST) methods and aren't for ""preprocessing"" or ""postprocessing"" requests!
  3. @svachon: Scriptlets are useful for quick prototyping/testing. As far as I know, there's only one ""legitimate"" production use of a scriptlet, namely `<% response.getWriter().flush(); %>` between the `` and the `` to improve webpage parsing performance in the webbrowser. But this use is in turn completely negligible when the output buffer size at the server side is low (1~2KB). [See also this article](http://balusc.blogspot.com/2009/09/webapplication-performance-tips-and.html#FlushTheBufferEarly).

Comments Quality
Accurate?:
Precise?:
Concise?:
Useful?: