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();
LVL 1
jmarkfoleyAsked:
Who is Participating?
 
mrcoffee365Commented:
I imagine you forgot the JSP syntax for declarations.  If you show the code it will help me debug the problem.

You need to have a file something like:

<%  lots of JSP code here
last line of JSP code here
%>
<%!
private String showMenu()
{
    return "<option selected value=\"upper(ASSESSOR)\">Assessor Name</option>";
}
%>


Note the <%! -- the ! tells the jasper compiler to create the code within the JSP brackets as declarations in the containing servlet.
0
 
malfunction84Commented:
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
0
 
mrcoffee365Commented:
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.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Loganathan NatarajanLAMP DeveloperCommented:
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.?
0
 
mrcoffee365Commented:
logudotcom -- there's no need to duplicate my response.
0
 
Loganathan NatarajanLAMP DeveloperCommented:
sorry, mrcoffee365... since i had the same idea, i have mentioned..
0
 
malfunction84Commented:
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.
0
 
jmarkfoleyAuthor Commented:
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)
0
 
mrcoffee365Commented:
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/
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.