Link to home
Start Free TrialLog in
Avatar of Mark
Mark

asked on

subroutines in jsp?

This may be a silly question, but can I have subroutines in jsp located in the code? In pseudo-java code in might look like:

function showMenu()
{>%
<option selected value="upper(ASSESSOR)">Assessor Name</option>
:
%>
}

// main jsp code

showMenu();
Avatar of malfunction84
malfunction84
Flag of United States of America image

Sorry, but this is not possible.  The JSP you write does get converted into a Java class, but you can't add new methods to this class in the way you're describing.

If you would like to get customized behavior from within JSPs, I recommend looking into writing custom JSP functions.  It's actually pretty straightforward to do, and it will provide the flexibility you're attempting to get.
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPIntro7.html#wp77078

The basic idea is that you define a method in a class somewhere, and then you can use special EL syntax to invoke it with parameters.  (But of course, you could just as easily do that by invoking a static method from within a scriptlet.)

You may encounter some issues with EL and JSP versions.  If you do, you might consider trying it with JSP tags instead.  Sun's tutorial is very thorough:
http://java.sun.com/products/jsp/tutorial/TagLibrariesTOC.html
Avatar of mrcoffee365
Usually one does subroutines in JSP files differently, something like this:

Put your function at the bottom of your .jsp file:

<%!

private String showMenu()
{
    return "<option selected value=\"upper(ASSESSOR)\">Assessor Name</option>";
}
%>

Then refer to it in your code as

  <%=showMenu()%>

If you do indeed want it in every .jsp page, then I suggest that you put the showMenu() method in a separete .jsp file, and include it in every .jsp page with an include directive, like this:

<%@ include file="/generalmethods.jsp" %>

where generalmethods.jsp contains the showMenu method.
in jsp, the functions and subroutines to be written in the below syntax,

<%!
private String showMenu()
{
    return "<option selected value=\"upper(ASSESSOR)\">Assessor Name</option>";
}
%>

also it has to be called in

 <%=showMenu()%>

what else u want.?
logudotcom -- there's no need to duplicate my response.
sorry, mrcoffee365... since i had the same idea, i have mentioned..
Wow, I was completely wrong about adding methods.  I guess that's what I get for never using scriptlets.  :P

Depending on the version of JSP you're using and how your deployment descriptor is configured, the scriptlet-like solutions might not be allowed.  In that event, a custom JSP function will work for you.
Avatar of Mark
Mark

ASKER

mrcoffee365: I tried adding a private function to the end of my jsp file:

private String mkDbString(String inStr)
{...}

And I got the following error, which I'm not sure I understand:

org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 383 in the jsp file: /pensionAddr.jsp
Illegal modifier for the variable mkDbString; only final is permitted
382:
383: private String mkDbString(String inStr)
ASKER CERTIFIED SOLUTION
Avatar of mrcoffee365
mrcoffee365
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
By the way, the best book I've found on writing servlets and JSP is the Head First Servlets & JSP book from O'Reilly.  But for the kinds of things you're running into, you might want to look at an article from Sun on the basics on JSP:
http://java.sun.com/developer/technicalArticles/Programming/jsp/