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

calling a class from jsp?

 
Tom Barns
Ranch Hand
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
}

 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 228
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ajan Balakrishnan
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your time.
it is working.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic