• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem to call servlet from Java class

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And, now it's working?
 
Ranch Hand
Posts: 129
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what about httpclient ?.... link
 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
reply
    Bookmark Topic Watch Topic
  • New Topic