• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with jsp accessing a servlets static methods

 
Peter Straw
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to make a package to 'JspUtils' and then import this package.
e.g.
com.some.package.JspUtils
<%@ page import ="com.some.package.*" %>
Rene
 
Sean MacLean
author
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic