*
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
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?

Tom Barns
Ranch Hand

Joined: Oct 27, 2000
Posts: 138
Hi All
I have my jsp file in the directory: webapps/examples/jsp/dates
also i have my java file in that directory.
the problem is i'm trying to create an instance of that class within my jsp file and i got error that syas that class not found.
i tried to use page directive with import attribute ,it did not work.
please any help with it.
here is my jsp and java file.
<%@ page import="java.util.*" %>
<form action="" method="POST">
<table>
<tr><td>First</td><td><input type="text" name="txt1" ></td> </tr>
<tr><td>Submint</td><td><input type="submit" value="Submit form " ></td> </tr>
</table>
</form>
<%!moh22 m=new moh22(); %>
<br><br><%=m.moh()%>
==================================
public class moh22
{
public int moh22()
{
return 45;
}
}


Thanks for your help.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
"I have my jsp file in the directory: webapps/examples/jsp/dates
also i have my java file in that directory. the problem is i'm trying to create an instance of that class within my jsp file and i got error that syas that class not found."
That is the correct behavior. Compiled class files MUST go under the WEB-INF\classes directory according to the servlet/JSP specs. Library JAR files go under WEB-INF\lib
If you are using Tomcat, look at the way the examples are organized. Everybody fooling around with servlets and JSP should download the API files from Sun - many details are laid out therein.
Bill

------------------
author of:
sandy gupta
Ranch Hand

Joined: Jan 30, 2001
Posts: 228

Hey there,
Why are u not including the class in your JSP,
How do u xpect the app server to look for your class when it is not included in the JSP ( and on top of that it is not even in the classpath ) I hope if u use a include or a import tag in the JSP , things shud become a lot smoother...
HTH
Sahil


Adios
Ajan Balakrishnan
Ranch Hand

Joined: Jan 04, 2001
Posts: 71
Tom,
Do the following
1. Add these 2 lines in your JSP code
<%@ page language="java" import='moh22' %>
<jsp:useBean id='m' scope='session' class='moh22'/>
2. Add your class file directory in your CLASSPATH
Now you should be fine using <%=m.moh()%> in your jsp.
There is no need to instantiate the moh22 explicity in your jsp code. The first 2 lines will do it for you
Note that "scope" can be anything depending on your requirements. It can be either session,page or application.
Let me know if it helps
Ajan
Tom Barns
Ranch Hand

Joined: Oct 27, 2000
Posts: 138
thanks for your time.
it is working.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling a class from jsp?
 
Similar Threads
FileNotFoundException while sending Mail as attachment using javamail
FileNotFoundException while sending mail with attachment using javamail
I have this strange display problem
How to send Parameters from JSP to a Servlet?
tomcat crashes when counting lines in a text file