I want to use JSTL within JSP declaration. I have code like:
<%! void fetchData(String .., String ..){
//some code...
pageContext.setAttribute("test",valueForJSTL); // Wont work until I pass pageContext from calling scriptlet
//some code...
%><%
//use pageContext variable set above to user in JSP (c:out, wcf:getData etc)**
%><%
}%>
I wonder why is it not allowed to used scriptlet (line 5-7) within JSP declaration. Or is there any other way to handle this?
I am calling above function from a scriptlet and I want this method to do some manipulation, add a variable in pageContext/request and fetch the variable in JSTL to fetch the data from DB .
You can find complete file at: http://wikisend.com/download/738986/MyView.jsp The JSTL within declaration "fetchCatalogEntries" is not working.
This is because the generated code for the scriptlet <% %>goes in the service method which is like called again and over the same object using multiple request threads
Whereas <%!, goes into the global class space or simply to declare methods and variables global for a JSP page.
Hence in your scriptlet you can call the method say fetchData() or any other like you would inside a service() method like in a normal java class.
You can even declare a field as below and refer it as in the example below :
<%!
private final Logger logger = new Logger(this.getClass());
private UserService userService = null;
public void jspInit() {
userService = new UserService();
}
private String getUserStatus(String userID) {
return userServce.getUserStatus(userID);
}
%>
<%
String userID = request.getParameter("userid");
String userStatus = getUserStatus(userID);
%>
Finally I would recommend you to NOT use scriptlets and use JSTL tags and libraries like the core-tag-libs and many others, these will help you in the long term.