jsp declaration and request
Can we use request in jsp:declaration?
In my program request cannot be recognised unless it
is inside of <jsp:scriptlet>
So what do we set inside of jsp:declaration?
If I want to set a method that sets value to a parameter from the request how will I do it since request is not recognised in jsp:declaration?
for example:
public String getName(){
String str = (String)request.getAttribute("name");
return str;
}
where will I set the above method?Inside of jsp:scriptlet?
The code that you put inside the jsp:scriptlet is inside a method called _jspService(request, response). The request method is local to the service method.
To get it into the declared method you have to pass it as a parameter to the method:
String getName(HttpServletRequest request) {
String str = (String)request.getAttribute("name");
return str;
}
and then call it like:
String name = getName(request);
That being said, you should do whatever you can to remove the whole method declarations from the JSP you can. You can do what you have above by using JSTL and EL:
${request.name}
No method declarations, no passing request around, nice clean code. Look up JSTL and EL (Expression Language).
Ok I understood.
However since you reffered in JSTL and EL.
I've read about JSTL tags and etc.
<jsp:expression>,<jsp:scriptlet> is not part of the JSTL but part of the
JSP specification correct?
And sth last.I am trying to use EL and in my web.xm
I set <jsp-config>
<jsp-property-group>
<url-pattern>/jsp/scriptlet.jsp</url-pattern>
<scripting-invalid>true</scripting-invalid>
<include-prelude>/jsp/copyright.html</include-prelude>
</jsp-property-group>
</jsp-config>
However server always recognises scriptlets although I set true to the tag <scripting-invalid>.
Any suggestions?
yes my web.xml starts with:
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
the script-invalid refers only to the <jsp:scriptlet> or as well also in
the <jsp:expression> ?