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.
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 methodservice
that has parametersrequest
andresponse
... Already the statement (<%!
) places the OP method in the Servlet body (which does not have access torequest
). I mean, your answer unfortunately won’t work.– Anthony Accioly
Malz then, I moved a few months ago with JSP and I stopped.
– Max Rogério