on 02-13-2006 7:06 AM
Hai All,
I have to call a servlet in my normal java program.Please send me code.
Thanks
Madhu
Hi,
Try getting a URL connection to the servelt and send the request as
import java.net.URL; // New addition
import com.oreilly.servlet.HttpMessage; // A support class, shown later
private String getServlet() {
try {
// Construct a URL referring to the servlet
URL url = new URL(getCodeBase(), "/servlet/myServlet");
// Create a com.oreilly.servlet.HttpMessage to communicate with that URL
HttpMessage msg = new HttpMessage(url);
// Send a GET message to the servlet, with no query string
// Get the response as an InputStream
InputStream in = msg.sendGetMessage();
// Wrap the InputStream with a DataInputStream
DataInputStream result =
new DataInputStream(new BufferedInputStream(in));
// Read the first line of the response, which should be
// a string representation of the current time
String response = result.readLine();
// Close the InputStream
in.close();
// Return the retrieved time
return response;
}
catch (Exception e) {
// If there was a problem, print to System.out
// (typically the Java console) and return null
e.printStackTrace();
return null;
}
}
Regards,
Uma
Message was edited by: Uma Maheswari
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Madu,
to send a http get or post to a servlet or other http resource, consider using Apache Jakartas HttpClient lib.
Have at look at the example programs, hot to use HttpClient lib here
http://svn.apache.org/viewcvs.cgi/jakarta/commons/proper/httpclient/trunk/src/examples/
Regards
Gregor
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Madhu,
Declare the methods in the servlet as 'public'.Place both the java program(through which u want to call the servlet) and the servlet in the same package.Create an object of the servlet class in ur java program and write the logic.
Else u can import the package(containing the servlet) in the java program.
Hope this helps you.
Regards,
Anuradha.B
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
86 | |
10 | |
10 | |
9 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.