Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can i invoke a static method from JSP  RSS feed

 
zheng yu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have defined a class that contains lots of constants and common methods. I want all the servlet/JSP /Java program to share these constants and methods
This class is defined like this
public class Commondef{

17 static public final int ITEMS_PER_PAGE = 10 ;
18
19 static public final String SEARCHFLAG = "SearchFlag" ;
20 static public final String SEARCHSTRING= "SearchString" ;
21 static public final String CURRENTPAGE = "CurrentPage" ;
22 static public final String OPERATION = "Operation" ;
23 static public final String TITLECODE = "TitleCode" ;
24 static public final String DISPLAY = "Display" ;
25 static public final String PAGETOTAL = "PageTotal" ;
26 static public final String RECORDTOTAL = "RecordTotal" ;
...
58 static public byte getMaterialType( String strSearchFlag ) {
59 byte bType = 1 ;
60 if ( strSearchFlag != null ) {
61 if ( strSearchFlag.length() == 4 ) {
62 char chType = strSearchFlag.charAt(1) ;
63
.....

every time when i call getMaterialType method like :
byte bType = getMaterialType( strSearchFlag ) ;
tomcat give me an exception .
Can someone tell me if it is possible or not

ZY
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not saying what exception you're getting, but I'm assuming it's a class not found exception.
You need to import your CommonDef class in an import statement at the top of your JSP like:
<%@ import="CommonDef" %>
then in your page you can use
<% byte bType = CommonDef.getMaterialType( strSearchFlag ) ; %>
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
 
Gerry Giese
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kyle,
I'm mulling over something similar to anil's problem. How about a twist to this: Say I only want this definitions object just loaded once, but accessible/sharable by all the servlets/JSPs in my .WAR file? It won't be user-specific, so sessions are not a good choice. Actually, I could see the need for this type of thing for doing application-wide services, like authorization checks, logging, etc.
Thanks!
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gerry,
You could put objects to do these kind of services in the Application scope (accessed through getServletContext()).
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
 
Gerry Giese
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kyle! I had thought that was only for parameters stored in the XML descriptors. Didn't know it had methods that allow adding to it!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!