[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

subroutines in jsp?

Posted on 2007-10-19
9
Medium Priority
?
1,336 Views
Last Modified: 2008-01-09
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();
0
Comment
Question by:jmarkfoley
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 8

Expert Comment

by:malfunction84
ID: 20120668
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
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 20125432
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
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 20128782
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 27

Expert Comment

by:mrcoffee365
ID: 20128837
logudotcom -- there's no need to duplicate my response.
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 20128861
sorry, mrcoffee365... since i had the same idea, i have mentioned..
0
 
LVL 8

Expert Comment

by:malfunction84
ID: 20132958
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
 
LVL 1

Author Comment

by:jmarkfoley
ID: 20203913
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
 
LVL 27

Accepted Solution

by:
mrcoffee365 earned 2000 total points
ID: 20204080
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
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 20204124
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Tech giants such as Amazon and Google have sold Alexa and Echo to such an extent that they have become household names. And soon they are expected to be used by commoners in their homes, ordering takeout, picking out a song, answering trivia questio…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question