GeeCON Prague 2014*
The moose likes Servlets and the fly likes How to call servlet which is inside the jar?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "How to call servlet which is inside the jar??" Watch "How to call servlet which is inside the jar??" New topic
Author

How to call servlet which is inside the jar??

Siddharth Ramavat
Ranch Hand

Joined: Feb 03, 2005
Posts: 41
Hello all,

I have a little proble while calling my servlet..

Actually I have my package structure like it.com.chat.java Now, My all the servlets are inside the java folder. now i have make a jar file, which is exactly same as my structure i mean when u will extract that jar u will find all the servlets inside the it.com.chat.java

If I want to call the LoginServlet inside that .jar file , then how can I do this...?

waiting for ur reply,
Thanks in advance.
Siddharth
Anil Sadi
Greenhorn

Joined: Jan 09, 2001
Posts: 23
Hi Siddharth,

Make the servlet declaration and servlet-mapping entries for the servlet that you wish to call in deployment descriptor(web.xml file).

Anil.
Siddharth Ramavat
Ranch Hand

Joined: Feb 03, 2005
Posts: 41
Thanx for ur reply,

Actually I have some question regarding the DD(web.xml).what should be written in web.xml??? and ya another question is that at where i should placed my .jar file?? I mean where in tomcat directory....?

waiting for ur reply,
Siddharth
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

Have you read the Tomcat documentation on how to set up a web app? It answers all these questions and more.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Siddharth Ramavat
Ranch Hand

Joined: Feb 03, 2005
Posts: 41
Thank You for your reply,

Now I came to know that where to put my .jar inside the Tomcat directory... that is under WEB-INF/LIB isn't it? But still i am facing some problem while running the servelt inside that .jar file..? I mean it is showing me "Page not found error....".

Could you please give me the answer of the following questions?

1) Is there any changes or updation is necessary in my web.xml?
2) If yes then what will be the changes?
3) If I want to access that servlet by using URLConnection object then what should be URL path that i have to pass inside the URL constructor while creating the new URL ?

Waiting for your reply,
Siddharth
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

As Bear said, the documentation covers all of this.
You'll need to map your servlet in web.xml.

If you want to see an example of a mapped servlet, get SimpleServlet from
http://simple.souther.us. The classes are not jarred up but the mapping in web.xml is the same either way.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
put the jar file in $TOMCAT_HOME/webapps/<app_name>/WEB-INF/classes

and make the mapping to something like this in the web.xml file:

<servlet>
<servlet-name>AnyName</servlet-name>
<servlet-class>it.com.chat.java.LoginServlet</servlet-class>
</servlet>


<servlet-mapping>
<servlet-name>AnyName</servlet-name>
<url-pattern>/servlet/Login</url-pattern>
</servlet-mapping>

after this, the servlet should b accessible via the URI

http://<host>:<port>/<app_name>/servlet/Login
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

put the jar file in $TOMCAT_HOME/webapps/<app_name>/WEB-INF/classes



That is wrong. Jar files need to to in:
TOMCAT_HOME/webapps/<app_name>/WEB-INF/lib
Siddharth Ramavat
Ranch Hand

Joined: Feb 03, 2005
Posts: 41
Thank you all,

Now I can access my servlet which inside .jar.

Now I am facing another problem , by changing the code inside the servlet. The servlet gets compile very nicely.It means there is no error in the servlet...now i am not able to access it. But when I make undo to the servlet , then it is working fine.

Now if by changing the servlet code i am not able to access the servlet , actually what it shows? If there is a problem with servlet then i think it should not compile, am I right? But as it gets compile then what will be the problem , I am not able to find it out.

So help me regarding the same.

Thank You,
Siddharth
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

javac is good but there are still things that slip by it.

Look in your app server's logs for evidence of run time exceptions.
Siddharth Ramavat
Ranch Hand

Joined: Feb 03, 2005
Posts: 41
Thank you, Mr. Ben, I got ur point to keep watchin my server's log.. but what is actully happned is that Before modify my servlet I have put a message which will tell me that my application is properly deployed on server or not... and when I start my Tomcat server it will show me that message that my application has been deployed successfully.. but after modifying my servelt even I was not able to see that message. It means there is some problem occured after doing this. So I am not able to catch the error over here..b'cz it is not showing any error or any message on log.

Please provide help regarding the same.

Thank You,
Siddharth
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There are no log files in TOMCAT_HOME/logs?
Usually if there is a deployment problem Tomcat will log it.
Maybe if you post your servlet code, someone can point out the problem.
Siddharth Ramavat
Ranch Hand

Joined: Feb 03, 2005
Posts: 41
Thank you, Mr. Ben , I got the error. The tomcat had written error in log file . It was about deployment. Now I have found it, but when I am try to run it, it shows me error like
"HTTP Status 405 - HTTP method GET is not supported by this URL"

But I am confused that I don't have any GET mehod in my servlet..? What will be the problem??

Please provide assistance.

Thank You.
Siddharth
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

When you write servlet code, you choose which service methods (doGet, doPost, etc..) you want to override.

The default behaviour for doPost and doGet is to throw an exception with the error message you have posted.

If you want to be able to make a GET request (which is the default way browsers make requests) you will need to override the doGet Method.

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServlet.html
Siddharth Ramavat
Ranch Hand

Joined: Feb 03, 2005
Posts: 41
hello
Thanks for reply
I have overridden the doPost() method in my servlet
I dont have overridden doGet() method in the servlet
Then too i am getting this error which i had speciied to u
As I have asked query in JSP forum abt changing the client from JSP to Swing I am getting error of stream and i am not even able to run the servlet from the browser also
The error reads ::: "HTTP 405 THE GET METHOD IS NOT SUPPORTED BY THIS URL"

Thanks
Regards
Siddharth
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
Siddharth, as Ben pointed out in his earlier post....

--
If you want to be able to make a GET request (which is the default way browsers make requests) you will need to override the doGet Method.
--

you have to override the doGet() method in the servlet to make it work.
OR change the submission method in your JSP to POST instead of the default GET.

Ben,

i have worked with a project where i have put the jar file in classes and it works. yes, may not be good practice, since all jars should be in lib, but it works.
Siddharth Ramavat
Ranch Hand

Joined: Feb 03, 2005
Posts: 41
Hello
thanks niraj
As u told me to use doGet() Why cant i use doPost() method as i am sending data with my button click event and if i want to use doPost then i have to override it . and that i have done in my servlet. Still i am getting error of doGet()

Thanks
Regards
Siddharth
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

In your form, have you set the method to be POST?

<form name="myform" method="post">
Siddharth Ramavat
Ranch Hand

Joined: Feb 03, 2005
Posts: 41
hello Ben
thanks for reply ans as u have replied in JSP forum to change the client from JSP to Swing. according to ur answer i want to call servlet from the Swing interface and when i do this thing i dont think so that i require to specify the menthod name(ie dopost or doget) Just creating an http url connection as been given in the link u specified :: jakarta.apache.org/commons/httpclient

when i do this i get cooneted to the servlets successfully but iu m not able to do I/O with servlets Actually my stream at the client is not getting initialized and it throws an I/O exception ::
HTTP RESPONSE CODE 405 <URL NAME> NOT FOUND

This exception is generated at the client side
Help regarding the same


Regards
Siddharth

David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Siddharth, please reduce the number of abbreviations in your posts. (u, ans, ur etc)
You don't have and we won't force you to, but we prefer that they aren't used since they make posts harder to read for visitors who don't have English as their first language. When combined with spelling and grammar errors they can make posts almost impossible to understand.

We appreciate your assistance.
Dave.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

This is the article/tutorial I used to learn this.

http://www-128.ibm.com/developerworks/java/library/j-tunnel/
Siddharth Ramavat
Ranch Hand

Joined: Feb 03, 2005
Posts: 41
Thank You, Mr. Ben for providing me such a nice tutorial on HTTP Tunneling.. Now I got whole the idea that what should I have to do .?

Now, When I defined stream at the client side I am still getting problem with it. I am sending u the code .. Please see it and tell me what is the problem with my code...?



Now in above code it shows error when I am trying to declare a new ObjectInputStream and gives the error which I had mentioned in my previous post.

So, Please help me regarding the same.

Thank You.
Siddharth
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Siddharth,
To be honest with you, I don't do this often enough to be able to debug your code without looking all of this up. I do know that I followed that tutorial and was able to stream objects back and forth from a Swing Applet to a servlet. This was quite a while ago.

At this point, your problem is not really a Servlet one. It has more to do with calling a web resource from a Swing app. I'm not sure which forum would be the best for this (maybe Distrbuted Java or IO/Streams).
[ April 03, 2005: Message edited by: Ben Souther ]
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
Here is what i think is the problem Sidharth:

since you have alrready read the tutorial that Ben recommended, i assume you are now familiar with the request-response model.

now, in your case, you are connecting from the Swing Applet to the Servlet.

So the Applet has to first send a request to the Servlet and then get response.

In your code, if you first open an output stream from your Applet to the servlet and then open the input stream, i think it will work.
Siddharth Ramavat
Ranch Hand

Joined: Feb 03, 2005
Posts: 41
Thanks Neeraj for replying me.

Actually I am not calling the servlet from my applet but it's a swing application. Now , I have all the thing workig nicely that I am able to send request from my swing app. to servlet and getting response back from the servlet...

Now, a question that is giving me little bit harrasment is that if I want to call a method which is declared inside servlet and I want to call that method directly from my client application by just connection it with URL...So is it possible....???


Another question is that how many streams I can open in a servlet to talk with the clients...?

Waiting for your reply,
Thank You.
Siddharth
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

No, use the standard service methods (doPost and doGet).

You would probably be interested in web services.
Take a look at http://xml.apache.org/axis.
It's a webservice engine that has been implemented as a servlet (as well as standalone). With it, you can make the type of remote procedure calls over HTTP that you are describing.
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
hi Sidharth!

if you mean 'how many clients can talk to the servlet at one time', then the answer would be: as many as the servlet container can handle. i read in one of these forums that for apache Tomcat this comes to be about 2 requests per second.

But if you mean how many streams of one servlet you can open for one client, then the answer would be one input stream and one output stream.
Vikky Techfreak
Greenhorn

Joined: Apr 13, 2005
Posts: 1
Hi,
you can just add your path it.com.chat.java appended with .ServletName, which you wanna call, and add that jar to classpath of your application server. so that it may really work.
For creating objects of your Servlet class(which you wanna call), you can just reference it by it.com.chat.java.ServletName,
you understand what I mean, you can just append the name of your servlet class to the end of your it.com.chat.java, and do whatever u want.
Vikky
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to call servlet which is inside the jar??