JSP goes through a JSP compiler which will convert the JSP page into a servlet, autogenerating the java code.
The JSP directives instructs the JSP compiler where to put what. Everything that is inside <% %> (called JSP scriptlets) will be put inside the service() method of the generated servlet. Everything inside <%! %> (called JSP declarations) will become part of the actual class of the generated servlet, so your getVal() will become an instance method.
The standard request (and session and pageContext etc) object instances are defined inside the service() method so they are, in effect, ONLY available inside JSP scriptlet sections.
If you are running on Tomcat, for instance, you can look at the actual generated Java code for your JSP pages if you look inside the "work" directory in the Tomcat installation. Might be interesting, if not get a better picture about what is happening "under the hood".
Answer from pap on Stack OverflowHello. I am currently learning about JSPs and was trying to find all available methods for request object, but could not find documentation. Maybe somebody knows where I could find this info? Thanks.
how to use "request" object within a function in jsp - Stack Overflow
Request object in JSP EL
object into string using jsp - Programming & Development - Spiceworks Community
JSP request object methods
Videos
JSP goes through a JSP compiler which will convert the JSP page into a servlet, autogenerating the java code.
The JSP directives instructs the JSP compiler where to put what. Everything that is inside <% %> (called JSP scriptlets) will be put inside the service() method of the generated servlet. Everything inside <%! %> (called JSP declarations) will become part of the actual class of the generated servlet, so your getVal() will become an instance method.
The standard request (and session and pageContext etc) object instances are defined inside the service() method so they are, in effect, ONLY available inside JSP scriptlet sections.
If you are running on Tomcat, for instance, you can look at the actual generated Java code for your JSP pages if you look inside the "work" directory in the Tomcat installation. Might be interesting, if not get a better picture about what is happening "under the hood".
request is accessible inside the scriptlet expressions, because it's an argument of the method in which these expressions are evaluated (_jspService). But if you want it to be available in your own methods, you must declare it as an argument:
<%
String fname = request.getParameter("fname");
String username = getVal("lname", request);
%>
<%!
private String getVal(String param, HttpServletRequest request) {
return request.getParameter("fname");
}
%>
Note that you shouldn't be using scriptlets and getting request parameters in JSPs in the first place. JSPs should be used to generate markup. Do your processing in a servlet/action, prepare the data to be displayed by the JSP by creating and populating beans in the request scope, and then dispatch to a JSP, which should use JSP EL, the JSTL and other custom tags exclusively.