Two Laptop Bag*
The moose likes JSP and the fly likes problem with jsp accessing a servlets static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "problem with jsp accessing a servlets static methods" Watch "problem with jsp accessing a servlets static methods" New topic
Author

problem with jsp accessing a servlets static methods

Peter Straw
Ranch Hand

Joined: Jan 08, 2002
Posts: 79
I am getting this message when trying to access a servlets static method in a jsp.
error: File C:\Jakarta\tomcat\webapps\myApp\Web-Inf\classes\JspUtils.java does not contain type JspUtils as expected. Please adjust the class path so that the file does not appear in the unnamed package.
I am not sure what this means as there is no package declaration in the JspUtils class. I had thought that the classpath was ok as jsps and servlets can forward data to and fro. Also I have this import statement in the jsp:
<%@ page import ='JspUtils%>
Any suggestions appreciated!
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Try to make a package to 'JspUtils' and then import this package.
e.g.
com.some.package.JspUtils
<%@ page import ="com.some.package.*" %>
Rene


Regards, Rene Larsen
Dropbox Invite
Sean MacLean
author
Ranch Hand

Joined: Nov 07, 2000
Posts: 621
Just to clarify, this seems to be an issue with a couple of app servers. They don't recognise classes that are in a the default (i.e. no package) package. The solution is as simple as declaring your classes in some sort of package.
Sean
xue shen
Greenhorn

Joined: Jul 05, 2002
Posts: 23
Rene, I have the same problem but I still do not get it.
I have a StringBean.class file in examples/WEB-INF/classes
the StringBean.java has package coreservlets; declared.
Then, I have StringBean.jsp in /examples/jsp
in the StringBean.jsp I have added:
<%@ page import ="coreservlets.*"%> before <html> tag
the jsp:useBean is:
<jsp:useBean id="stringBean" class="coreservlets.StringBean" />
I still get the same error of class not found as before. Please shade some light on what did I do wrong?
Thanks
x.s
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Let's create a simple JavaBean that works with a JSP and a servlet. We will make:
1. a main.html that takes a String parameter and forwards it to
2. a mainController.jsp that instantiates a
JavaBean and populates it with the String parameter, then forwards it to a servlet
3. a JavaBean called InputBean that has a property called "input"
4. an OutputServlet that takes the property from InputBean and displays it on the browser.
For main.html we have:

Let's suppose the Tomcat4 install directory is catalina. We'll put this html in the app root, C:\catalina\webapps\myContext.
For the JSP we have:

For more information on JSP tags, refer to the Sun link. Basically, since I will create the bean inside a package I will import it using the page directive, instantiate it with useBean, populate its "input" property with setProperty, and then finally forward it to the OutputServlet with servlet URL "/output". We'll also put this JSP in the app root, C:\catalina\webapps\myContext.
For the JavaBean, go to C:\catalina\webapps\myContext\WEB-INF\classes and create a folder named beans. Inside beans, create a JavaBean named InputBean:

Note that the public no-args constructor, as well the private fields and public accessor methods are part of the JavaBean specs.
Finally, we create the OutputServlet:

Note that we instantiate the actual JavaBean, retrieve its property and then display it to the client browser. Of course, this servlet resides in C:\catalina\webapps\myContext\WEB-INF\classes
We must register this servlet since the JSP uses a servlet mapping to forward the request. We must make the following changes to the web.xml file:

Note that all the <servlet> elements precede the <servlet-mapping> elements.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem with jsp accessing a servlets static methods
 
Similar Threads
simple jsp problem
Where to place the Bean package in J2EE server
Static method and classes
static import of an enum in Java 1.5?
Static method and classes