File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Linux / UNIX and the fly likes how to call servlet class and it's methods from shell script Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "how to call servlet class and it Watch "how to call servlet class and it New topic

how to call servlet class and it's methods from shell script

rama ilango

Joined: Dec 11, 2008
Posts: 24
i can call java class from shell script .
but i can't call methods of java and sevlet.
please reply me.
[ December 16, 2008: Message edited by: rama ilango ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

Welcome to JavaRanch.

You can run a Java program from a shell script the same way as you do on the command line, by adding a command like:

java packagename.ClassName

to the script (where ClassName is the name of a class that contains a main() method).

Why would you want to call a servlet from a shell script? Please explain in more detail what you want to achieve.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

Servlets are designed to be used inside the operating environment of a JEE web application container. The container provides considerable infrastructure, plus it routes traffic to and from the servlet.

There are 2 reasons I can think of for wanting to run servlet code from a command line:

1. You have some sort of function you wish to use but you want to be able to invoke it via a batch process instead of by way of a web request.

2. You want to test the code in the servlet as part of an offline test suite.

For reason 1, you have a couple of options. If you want the actual servlet to function, use a command-line based utility such as wget to send a request to the server and retrieve the request results. If you want the logic but don't want to have a web server running, the same general rules as reason 2 apply.

For reason 2, where you don't want to talk to a running server, you can set up a batch server simulation such as the Cactus test tool. Cactus can imitate much of the infrastructure that an application server provides, but it's quicker to launch, since it's not a full-blown web server.

If you just want logic functionality, the best thing to do is to move the business logic itself outside of the servlet class and into a POJO business-object class. Then you can have the servlet invoke its functionality for online use, or invoke it directly for offline use.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: how to call servlet class and it's methods from shell script
It's not a secret anymore!