Search code examples
javajava-ee-6

Is there a better way to pass variables to my view?


My gripe with the below code is that the variables template and urlPrefix have to be called via <%=request.getAttribute('urlPrefix')%>. Is there a way for me to pass a variable so I can just call it like <%=urlPrefix%> ?

public void loadView (PageConfig config) throws ServletException, IOException {
    HttpServletRequest request = config.getRequest();
    HttpServletResponse response = config.getResponse();

    RequestDispatcher dispatcher = request.getRequestDispatcher("base.jsp");

    response.setContentType("text/html;charset=UTF-8");

    request.setAttribute("template", config.getTemplate());
    request.setAttribute("urlPrefix", "/CMS");

    System.out.println("Controller::LoadView()");

    dispatcher.forward(request, response);
}

Solution

  • Use JSP EL. Scriptlets are kind of... frowned upon, and clunky.

    ${urlPrefix}