cancel
Showing results for 
Search instead for 
Did you mean: 

Can i create client program for a servlet(without browser)

Former Member
0 Kudos

Hai All,

I have to call a servlet in my normal java program.Please send me code.

Thanks

Madhu

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

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

former_member184385
Active Participant
0 Kudos

Hi Madu,

to send a http get or post to a servlet or other http resource, consider using Apache Jakarta’s 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

Former Member
0 Kudos

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