Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem to call servlet from Java class

 
fai fai
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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-name>NewServlet</servlet-name>
<url-pattern>/myClass.NewServlet</url-pattern>
</servlet-mapping>

Servlet code:

 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 754
Eclipse IDE Java
 
vijin das
Ranch Hand
Posts: 129
Firefox Browser Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what about httpclient ?.... link
 
Michael Angstadt
Ranch Hand
Posts: 277
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic