JSP - Using request in a method

Asked

Viewed 612 times

3

I am trying to use a request within a method as follows:

<%!                
    public void method()
    {
        RequestDispatcher disp = request.getRequestDispatcher("t2.jsp");
        disp.forward(request, response);
    };
%>

But the same is pointing error in the request.. I believe it may be something related to lack of import for the request object, but I’ve tried some importrs and no effect has appeared..

If I use the request outside of the method the same works, so:

<%    
    RequestDispatcher disp = request.getRequestDispatcher("t2.jsp");
    disp.forward(request, response);
%>

2 answers

3


JSP pages are translated to Servlets and then compiled.

There is a difference about how translation treats Scriptlets (<% %>) and Declarations (<%! %>).

Scritplets are translated into a service method that provides variables such as request and response. For example, your Scriptlet is translated to the building below in Tomcat 8 / Jasper:

public void _jspService(final HttpServletRequest request, 
        final HttpServletResponse response) throws IOException, ServletException {

    final PageContext pageContext;
    HttpSession session = null;
    final ServletContext application;
    final ServletConfig config;
    JspWriter out = null;
    final Object page = this;
    JspWriter _jspx_out = null;
    PageContext _jspx_page_context = null;

    try {
        pageContext = _jspxFactory.getPageContext(this, request, response,
                null, true, 8192, true);
        _jspx_page_context = pageContext;
        application = pageContext.getServletContext();
        config = pageContext.getServletConfig();
        session = pageContext.getSession();
        out = pageContext.getOut();
        _jspx_out = out;

        // Aqui esta o seu codigo real
        RequestDispatcher disp = request.getRequestDispatcher("t2.jsp");
        disp.forward(request, response);
    } catch (Throwable t) {
        // linhas e linhas de boilerplate    
    } finally {
        _jspxFactory.releasePageContext(_jspx_page_context);
    }
}

Note that it is only possible to use the request because it was passed to the service method by parameter.

Declarations in turn are simply copied to the Servlet body during translation, so you will not have access to request by default. Nothing prevents you from receiving request and response as parameters:

<%!
  public void method(HttpServletRequest request, HttpServletResponse response) 
          throws ServletException, IOException {
      RequestDispatcher disp = request.getRequestDispatcher("t2.jsp");
      disp.forward(request, response);
  };
%>

This is equivalent to declaring a method in the body of a Servlet. According to the response of Max Rogério, only declaring the method is not enough. It is necessary to invoke the method that was declared. For this purpose a possible option is to write a Scriptlet that invokes the method by passing on request and response:

<%
  method(request, response);
%>

It does what you want... That said, I see little reason to make a foward this way since JSP provides the tag <jsp:forward> who does the same thing:

<jsp:forward page="t2.jsp" />

In general it is better to avoid Scriptlets and Declarations, besides being a legacy technology, architecturally they are an abomination... Mix business code with navigation rules and the view in a single place creates a salad very difficult to maintain.

0

You create methods or function to not rewrite things over and over again. In your case you just created a method and nothing else. For it to work you have to call it.

// criando metodo ou funcao
public void method()
{
    //sequencia de código que será executada ao chamar metodo ou funcao
    RequestDispatcher disp = request.getRequestDispatcher("t2.jsp");
    disp.forward(request, response);
};

//utilizando metodo ou funcao
method();

In case I was in any class I’d be more or less like this

public class Teste{
// criando metodo ou funcao
public void mudaTela()
{
    //sequencia de código que será executada ao chamar metodo ou funcao
    RequestDispatcher disp = request.getRequestDispatcher("t2.jsp");
    disp.forward(request, response);
};
}

//na pagina do jsp vc faz import da class
Teste t = New Teste();
t.mudaTela();

I haven’t tested the codes. But the idea is that when you create a method, you have to call it to run.

  • Hi Max, there’s a bigger problem here. request will not be available within the method. The only reason why it works in the second OP scriptlet is the way Jsps files are compiled for Servlets... Basically the scriptlet body goes to a method service that has parameters request and response... Already the statement (<%!) places the OP method in the Servlet body (which does not have access to request). I mean, your answer unfortunately won’t work.

  • Malz then, I moved a few months ago with JSP and I stopped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.