| Author |
Problem to call servlet from Java class
|
fai fai
Greenhorn
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-name>NewServlet</servlet-name>
<url-pattern>/myClass.NewServlet</url-pattern>
</servlet-mapping>
Servlet code:
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3862
|
|
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
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?
|
[uaiHebert.com] [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
|
VIJINDAS
|
 |
Michael Angstadt
Ranch Hand
Joined: Jun 17, 2009
Posts: 272
|
|
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().
|
SCJP 6 || SCWCD 5
|
 |
 |
|
|
subject: Problem to call servlet from Java class
|
|
|