aspose file tools*
The moose likes Servlets and the fly likes calling java inside a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "calling java inside a servlet" Watch "calling java inside a servlet" New topic
Author

calling java inside a servlet

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi ,
I am working in asterisk java.i want to pass the value like callerid from the asterisk java program to servlet.but when i tried to call the java in servlet,it shows the following error.i am pasting here,how i tried to call java inside a servlet

the error message follows:

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet execution threw an exception


root cause

java.lang.NoClassDefFoundError: org/asteriskjava/fastagi/AgiScript
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(ClassLoader.java:620)
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1853)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:875)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1330)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1209)
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:169)
InServlet.doGet(InServlet.java:30)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause

java.lang.ClassNotFoundException: org.asteriskjava.fastagi.AgiScript
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1363)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1209)
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(ClassLoader.java:620)
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1853)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:875)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1330)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1209)
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:169)
InServlet.doGet(InServlet.java:30)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.26 logs.


thanks in advance.
Balachandran Vijayarajan
Greenhorn

Joined: Jun 23, 2005
Posts: 29
This class is not there in the classpath - org.asteriskjava.fastagi.AgiScript. make sure that class is there in the path before running the server.

Cheers,
Bala
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

hi,

culprit:
-------------------------------
Class c=Class.forName("Inbound");
--------------------------------

give fullyqualified name for an argument of forName

note: use all variable inside doGet/doPost.this is better practise..please avoid the class variable declarations
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
give fullyqualified name for an argument of forName

I am having the calss named Inbound.java.I dont understand what ia the fully qualified name.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

something like

forName("package1.package2.ClassName");
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
ok.As you said,I put the inbound class in com.example.servlets package and changed the code like this

but now the following is displayed:

HTTP Status 405 - HTTP method GET is not supported by this URL

--------------------------------------------------------------------------------

type Status report

message HTTP method GET is not supported by this URL

description The specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL).



Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

hi,
---------------------------------
h.service( request, channel);
---------------------------------
you may require to change the method name sevice

[i am not sure try this and let me know ]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10519
    
    9

There are two things here.
Your original code is implementing doGet where as this code is implementing doPost. How are you invoking it? by GET or POST? Make changes accordingly.

Also, your original error was
java.lang.NoClassDefFoundError: org/asteriskjava/fastagi/AgiScript
WHat is this AgiScript class? Is it one of your classes? Are you sure this class is in the classpath?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
you may require to change the method name sevice
In my java code i am using the service method only.thats why i am calling the service method here.

WHat is this AgiScript class? Is it one of your classes? Are you sure this class is in the classpath?
I am using doGet method only.I just tried in doPost and pasted that code.that also showing the same eror.
AgiScript class is predefined class in the jar file asteriskjava.0.3.1.jar.In my eclipse ide it is placed under Referenced Libraries-->asterisk-java-0.3.1.jar-->AgiScript.class.

I am placing here my java class Inbound.java

[ August 08, 2008: Message edited by: preethi Ayyappan ]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10519
    
    9

AgiScript class is predefined class in the jar file asteriskjava.0.3.1.jar.In my eclipse ide it is placed under Referenced Libraries-->asterisk-java-0.3.1.jar-->AgiScript.class.


I cant find where this is. I usually place my external jars under
Project->Properties->Java Build Path->Libraries

Are you running Tomcat from eclipse?
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
yes,i am ruunning tomcat from eclipse only.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10519
    
    9

Originally posted by preethi Ayyappan:
yes,i am ruunning tomcat from eclipse only.

Can you move the jar to where I said (Project->Properties->Java Build Path->Libraries) and see what happens?
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
I did the same what you told.i am getting the same error.but i can able to run the program without error when i placed the code in another project.I think the calss is in perfect classpath there.

Thanks
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
i need to get the parameter callerid from the class file inbound.java to the servlet.i have done like this

This is printing null and shows the following error in console

inbound
java.lang.IllegalStateException: Trying to send command from an invalid thread
at org.asteriskjava.fastagi.AgiOperations.getChannel(AgiOperations.java:791)
at org.asteriskjava.fastagi.AgiOperations.streamFile(AgiOperations.java:296)
at com.example.servlets.Inbound.service(Inbound.java:47)
at com.example.servlets.JavaServlet.doGet(JavaServlet.java:45)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:619)


(Inbound.java:47) is streamFile("plase hold th line your call is being trfd to officer");
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

It looks like you ironed out the classpath issues and have a working connection between the servlet and your java class. Unfortunately you came upon another problem:
"java.lang.IllegalStateException: Trying to send command from an invalid thread"

This message is being sent by the asteriskjava code, and should be documented by them to identify why this exception may occur, and what you can do to fix it. It is, however, API specific so unless someone here knows AsteriskJava it will be hard to get an answer.

Try looking at the Asterisk-Java FAQ or asking in their mailing list to see what they say.


Steve
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
thanks for your reply.I am having another doubt here.I need to pass a parameter ,say callerid from my asteriskjava application to a servlet.In java code I tried to pass the parameter using open url connection like the following:-

Is this correct way?If yes, how can i retrieve this parameter in servlet code.please anyone help me in code.
Thanks.
[ August 08, 2008: Message edited by: preethi Ayyappan ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42598
    
  65
Yes, it's possible to do it that way. You can get at the parameter by calling request.getParameter("parameter1") in the doGet method of the servlet that handles this request.


Ping & DNS - my free Android networking tools app
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks.I have tried that.but i am getting output null.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Originally posted by preethi Ayyappan:
Thanks.I have tried that.but i am getting output null.


Is the callerid returned by request.getCallerIdNumber() in your previous example null?
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
yes.In the previous code,I have used request.getCallerId().but eclipse shows that is deprecated method.so i have changed it to request.getCallerIdNumber().Is there any issues due to this?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42598
    
  65
In the previous code,I have used request.getCallerId().but eclipse shows that is deprecated method.so i have changed it to request.getCallerIdNumber().Is there any issues due to this?

That's hard to say, since those methods are 3rd party extensions. The javadocs of those methods should tell you about them, or if not, you can always have a look at the source code.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks.can i display(open) the page http://192.168.1.174:8080/NewWebProject, if i run the stand alone application which uses the openurlconnection?
[ August 11, 2008: Message edited by: preethi Ayyappan ]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Originally posted by preethi Ayyappan:
Thanks.can i display(open) the page http://192.168.1.174:8080/NewWebProject, if i run the stand alone application which uses the openurlconnection?

[ August 11, 2008: Message edited by: preethi Ayyappan ]


Why don't you try it and tell us?
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks.I will try.But i just want to know is there any possibility to open a webpage using url open connection.Because i need to do is,If i run my java code it should open a webpage and the value of parameters in my java code should be displayed in servlet page.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling java inside a servlet