Saturday, May 14, 2011

Declare function in JSP


http://www.jguru.com/faq/view.jsp?EID=1010

Do note that you do not have direct access to any of the JSP implicit objects like request, response, session and so forth from within JSP methods. However, you should be able to pass any of the implicit JSP variables as parameters to the methods you declare. For example:

-----including 'out'--------------------------


<%! 
      public String whereFrom(HttpServletRequest req) {
      HttpSession ses = req.getSession();
      ... 
      return req.getRemoteHost();
     }
%>
<%
     out.print("Hi there, I see that you are coming in from  ");
%>
<%= whereFrom(request) %>



Re: calling methods of an included page

Oystein Ovrebo, Apr 4, 2002  [replies:1]
Maybe you could try this? Works for me:
file1.jsp:
<%@page contentType="text/html"%>

<%!

    public void test(JspWriter writer) throws IOException{
        writer.println("Hello!");
    }

%>

file2.jsp
<%@include file="file1.jsp"%>
<html>
<body>

<%test(out);%>

</body>
</html>

Hope this helps. Oystein

Is this item helpful?  yes  no     Previous votes   Yes: 1  No: 0



Reply to this answer/comment  Help  
Re[2]: calling methods of an included page
Mohan Jadhav, Apr 19, 2002
Yes...this works... But, if you use the JSP action tag for include, it doesn't work. For example, in the code given by Oystein, if I replace <%@include file="file1.jsp"%> with <jsp:include page="file1.jsp" />, it doesn't work.
The reason for this is the page mentioned in the action tag is considered at run time, where as in the first case it is just like inlining of the code.
Hope this clarifies the point...
Mohan.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  

No comments:

Post a Comment