File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Problem to call servlet from Java class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem to call servlet from Java class" Watch "Problem to call servlet from Java class" New topic

Problem to call servlet from Java class

fai fai

Joined: Dec 18, 2002
Posts: 4
Dear expert:

I had created a simple java class to call a simple servlet without any error however the servlet do not have any display(suppose my servlet will display output to Tomcat console for every invokement). Could anyone could explain this issue ???

Following is my JAVA class

JAVA Class

For Servlet side, I had configure the web.xml with follow
- <servlet-mapping>

Servlet code:

Matthew Brown

Joined: Apr 06, 2010
Posts: 4544

I may be missing something, but...

1. Your servlet doesn't appear to be producing any content - you don't write anything to the PrintWriter.

2. Even if it did, your class reading the servlet doesn't appear to read the content - you need to get hold of the InputStream from the URLConnection and read from it.
fai fai

Joined: Dec 18, 2002
Posts: 4
Thanks Matthew Brown,

The reason why I don't code any read/write action in servlet because I just want to test the connection from JAVA main class to servlet class.
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

And, now it's working?

[] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
vijin das
Ranch Hand

Joined: Jun 07, 2010
Posts: 129

what about httpclient ?.... link

Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 277

In the deployment descriptor, you must also include a <servlet> tag, which allows you to specify the fully-qualified class name of the servlet. You have only posted the <servlet-mapping> tag in your forum post.

In order to actually send the HTTP request using the URLConnection class, I think that you have to call getInputStream(). This will send the request and allow you to access the body of the response.

Also, I see that you are setting the request's content type...since you are not putting anything in the request body, you do not need to do this. The servlet will still accept your request just fine.

In addition, you should not be overriding init(ServletConfig). The method you should override is init().

I agree. Here's the link:
subject: Problem to call servlet from Java class
It's not a secret anymore!