This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes Calling A  Class From JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling A  Class From JSP" Watch "Calling A  Class From JSP" New topic
Author

Calling A Class From JSP

Navtaj Singh
Ranch Hand

Joined: Jan 30, 2001
Posts: 37
Hi!
I have a JSP page making use of
<jsp:useBean id="cart" class="ShoppingCart" scope="session"/>
property.
Also i have a compiled "ShoppingCart.class" file in the classes
directory.
I have Jdk1.3 & Tomcat 3.2.1 installed.
Problem::
When the Jsp page Instantiates the ShoppingCart ,the server gives "Deprecated method used" and Dos prompt gives
"ShoppingCart.class" file not found.
With Regards,
Parvinder

[This message has been edited by Navtaj Singh (edited January 31, 2001).]
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
Deprecated method used
this is not much of a problem....
When compiled using
javac -deprecation
you also get the source locations where deprecated APIs are used.
The problem with .class not found is what you first need to fix.
You should put the .class file in the web-inf/classes dir
of your application.
regds.
- satya

Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Navtaj Singh
Ranch Hand

Joined: Jan 30, 2001
Posts: 37
Hi! Satya Thanks For Reply.
Can You elaborate on to what exactly you mean by:-
"Javac-Deprecation" and how exactly can i solve the problem.
Thanks
[This message has been edited by Navtaj Singh (edited January 31, 2001).]
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Navtaj,
When you compile the servlet.java or anyother.java file, instead of just javac abcd.java type javac -Deprecation abcd.java. This will print out the methods which are deprecated.
Then you see in Java Doc API what are the new recommended methods for those deprecated ones and recode your java files.
regds
maha anna
Navtaj Singh
Ranch Hand

Joined: Jan 30, 2001
Posts: 37
Hi!
Thanks for the reply
Q1>How to find Java DOC API
Q2>
I have a compiled class called "ShoppingCart.class" file.I am trying to Instantiate the above mentioned class from within the "abc.jsp" page.The code for the "ShoppingCart.java"file is :-

import java.util.*;
public class ShoppingCart {
HashMap map = new HashMap();
public ShoppingCart()
{
}
public void addBook(String bookID,String bookName)
{
map.put(bookID,bookName);
}
public void deleteBook(String bookID)
{
map.remove(bookID);
}
public String[] getList()
{
Iterator iterator = map.keySet().iterator();
ArrayList list = new ArrayList();
String bookID;
String bookName;
while(iterator.hasNext())
{
bookID = (String) iterator.next();
bookName = (String) map.get(bookID);
list.add("ID."+bookID+" Name:"+bookName);
}
if (list.size() > 0)
{
String[] str = new String[list.size()];
list.toArray(str);
return str;
}
return null;
}
}
***************
But the Server gives the following error:-
org.apache.jasper.JasperException: Unable to compile class for JSPC:\jakarta-tomcat-3.2.1\work\localhost_8080%2Fserv\_0002fbookcart_0002faddcart_0002ejspaddcart_jsp_0.java:83: Class bookcart.ShoppingCart not found.
ShoppingCart cart = null;
^
C:\jakarta-tomcat-3.2.1\work\localhost_8080%2Fserv\_0002fbookcart_0002faddcart_0002ejspaddcart_jsp_0.java:86: Class bookcart.ShoppingCart not found.
cart= (ShoppingCart)
^
C:\jakarta-tomcat-3.2.1\work\localhost_8080%2Fserv\_0002fbookcart_0002faddcart_0002ejspaddcart_jsp_0.java:91: Class bookcart.ShoppingCart not found.
cart = (ShoppingCart) Beans.instantiate(this.getClass().getClassLoader(), "ShoppingCart");
^
3 errors
Cynthia Yao
Ranch Hand

Joined: Nov 06, 2000
Posts: 92
1, here is the url for java 2 api: http://java.sun.com/j2se/1.3/docs/api/index.html
here is for servlet: http://java.sun.com/products/servlet/2.2/javadoc/index.html
2. u miss the package declaration in the beginning of shopping cart, also where did u put shoppingcart.class file? the error message seems very interesting. bookcart.ShoppingCart, what files are in your classpath or u servlet class dir?

[This message has been edited by Cynthia Yao (edited February 02, 2001).]
Navtaj Singh
Ranch Hand

Joined: Jan 30, 2001
Posts: 37
Hi! Cyanthia.
Thanks for responding
To answer to your second Question
i have added the "package javax.servlet.jsp.*;"
The "ShoppingCart.java" file does compile to "ShoppingCart.class"
file.
But again if i call the "abcd.jsp" file which instantiate the above class whose code is :-
%@ page info="Displays Book Details" %>
<%@ page session="true" %>
<HTML>
<HEAD>
<TITLE>
Displays Book Detail - JSP
</TITLE>
<% String bookID = request.getParameter("id"); %>
<% String bookName = request.getParameter("name"); %>
</HEAD>
<BODY>
<jsp:useBean id="cart" class="ShoppingCart" scope="session"/>
<jsp:setProperty name="cart" property="*"/>
<% cart.addBook(bookID,bookName);
session.putValue("cart",cart);
%>
<jsp:forward page="displaycart.jsp" />
</body>
</html>
And it gives the same error as before
ie:The "ShoppingCart.class" file not found.

Another question is if i have another Sub-directory under root-directory which inturn contains my jsp pages,do i have to create another /WEB-INF/Classes file for each such sub-directory,
or the root directory - classes remain the same.
Navtaj Singh
Ranch Hand

Joined: Jan 30, 2001
Posts: 37
Ahoy! Ahoy
Some-body help please
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Navtaj Singh:
[...] And it gives the same error as before
ie:The "ShoppingCart.class" file not found.
Another question is if i have another Sub-directory under root-directory which inturn contains my jsp pages,do i have to create another /WEB-INF/Classes file for each such sub-directory,
or the root directory - classes remain the same.

Sounds like you've got problems with paths.
There is just one WEB-INF/ directory, the one in the root of your web application. No matter how many subdirectories you have in the web application itself.
All the .class files you want to use go into WEB-INF/classes/. Beware that you need to convert package names to directory paths! Although you're not telling us, I've got a hunch that your ShoppingCart.class may be in WEB-INF/classes/ rather than WEB-INF/classes/bookcart/.
Finally, if you want to use any .jar files, they should go into WEB-INF/lib. You don't need to use subdirectories there since the class files are packaged in the .jar with full directory path information.
HTH
- Peter
Navtaj Singh
Ranch Hand

Joined: Jan 30, 2001
Posts: 37
Hi! Peter
Thanks for clearing the first question.
As far as the "ShoppingCart.class" file, it is in the classes folder.It is showing Deprecated method used.
Please go thru the "ShoppinCart.java" file code.
Please suggest any new method introduced if any.
Thanks,
Navi
Cynthia Yao
Ranch Hand

Joined: Nov 06, 2000
Posts: 92
Sorry, I am not online when I at home. I did not see a import statement in your jsp. That is sort of like java import statement. try to put this in u jsp:
<%@ page import="pathtoclass/*"%>
Navtaj Singh
Ranch Hand

Joined: Jan 30, 2001
Posts: 37
Hi! Cynthia.
I did introduce the Import statement,but still the server gives
the following error as mentioned before.:-
org.apache.jasper.JasperException: Unable to compile class for JSPC:\jakarta-tomcat-3.2.1\work\localhost_8080%2Fserv\_0002fbookcart_0002faddcart_0002ejspaddcart_jsp_1.java:87: Class bookcart.ShoppingCart not found.
ShoppingCart cart = null;
^
C:\jakarta-tomcat-3.2.1\work\localhost_8080%2Fserv\_0002fbookcart_0002faddcart_0002ejspaddcart_jsp_1.java:90: Class bookcart.ShoppingCart not found.
cart= (ShoppingCart)
^
C:\jakarta-tomcat-3.2.1\work\localhost_8080%2Fserv\_0002fbookcart_0002faddcart_0002ejspaddcart_jsp_1.java:95: Class bookcart.ShoppingCart not found.
cart = (ShoppingCart) Beans.instantiate(this.getClass().getClassLoader(), "ShoppingCart");
^
3 errors
***********
Please help.
Cynthia Yao
Ranch Hand

Joined: Nov 06, 2000
Posts: 92
What i feel interesting is that you do not put ShoppingCart.class under bookcart directory, and it is not in that package (your declaration of package javax.jsp is not right), I wonder where it is come from. May it come from your classpath setting, some file with the same name in some jar files? I am not sure. Would you send me the servlet or jsp to my email address -- ruyiy@yahoo.com. This way it will be faster.
Cynthia Yao
Ranch Hand

Joined: Nov 06, 2000
Posts: 92
Sorry for apply you so late. I read your email today, suddenly all the work start to pill up. Anyway, here is what I found out.
1. session.getValue is depressed to session.getAttribute, so is session.putValue to session.setAttribute. So you should change accodingly. encodeUrl should be encodeURL. here is the online doc for servlet. If you have any doubt, just check that. http://java.sun.com/products/servlet/2.2/javadoc/index.html
2. Your package setting is a little weird. You should put all servlets file together seperate from the Bean files. In order to use ShoppingCart class, you should import it in those servlets which will use it.
I use JBuilder for development and testing. The warning is much easier to spot in it. You should try it sometimes.
Well, happy programming
Navtaj Singh
Ranch Hand

Joined: Jan 30, 2001
Posts: 37
Thanks Cyanthia
I shall use your advice and get back to u, Maam
Navi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling A Class From JSP