tags:

views:

233

answers:

3

I just got a servlet class working in Eclipse. I was testing it by deploying it using App Engine, and it would prompt me to run as a Web Application. Now I want to run this code from another class. So I made another class and put "TestServlet ts = new TestServlet();" in the main function. When I run it nothing happens. Do I have to make a call to the TestServlet's "doGet" method, or is it something to do with not running the main class as a web application?

A: 

You need a servlet container if you want to run it up for real. I would suggest using Winstone http://winstone.sourceforge.net. It's a small, fast, zero-config container for getting your servlet up and running.

Joe
This is not a good answer. OP _has_ a servlet container as he was testing his servlet deploying it to App Engine. That does not in any way excuse attempts to manually instantiate his servlet.
ChssPly76
He says that he's trying to test it by deploying it to GAE. In my interpretation of the question he's trying to run the servlet on his machine, which was the question I was posting an answer for.
Joe
@Joe - your interpretation of the question is wrong, then. I've nothing against using Winstone instead of GAE to test a servlet - it most certainly is easier / faster; but the question was "how can I run a servlet from a standalone java app's main() method". And the answer is "you shouldn't".
ChssPly76
A: 

Umm... you should not be doing this. Extract common logic into a separate POJO (plain old java object) class and invoke it from both your servlet and your other class.

Manually instantiating / invoking servlets is NOT a good idea.

ChssPly76
The answer to the question depends entirely on what you are trying to achieve. For unit and/or component testing the behaviour of classes, of course you test code without the service layer. For integration testing, running it up (which is what the question is asked) can be necessary.
Joe
I only need the servlet to help extract XML data from a webservice. After that I'll be manipulating the XML in other code. I was hoping to basically run the servlet, get the XML, and shut it off if possible. I wanted to just run one class, but you're saying I should run the web application and the java application separately, right? Really appreciate the help.
Mattk
I believe you can run Winstone 'embedded' (i.e. invoke it from code), doesn't have to be a separate instance (but it can be). (There may be a better way of getting XML from a webservice).
Joe
If you're extracting XML data from webservice (as its client?) why do you need the servlet to begin with? You can run your code as a regular class that connects to web service and parses the result.
ChssPly76
Well, the webservice is facebook, and the xml data is a users (mine). All the example code i could find for java involved some kind of authentication, logging in, and required servlets. Apologies for not representing the question better.
Mattk
Are you using this, perchance? http://code.google.com/p/facebook-java-api/ If so, take a look at FacebookXmlRestClient. While majority of examples do indeed seem to involve servlets for some reason, you definitely don't need one to consume web service results. Take a look at this page as well: http://code.google.com/p/facebook-java-api/wiki/ExamplesFinally, if push comes to shove and you do need to use a servlet (I don't deal with Facebook so I don't know its possible idiosyncrasies) you WILL have to use servlet container to deploy it.
ChssPly76
I know it's possible to avoid servlets with other API's (there's a python one, for example), but I haven't figured out how to do it with Java. At any rate, I'll heed your initial advice and avoid manually invoking servlets, and refactor the common logic into a separate object. Thanks a lot for the time and consideration.
Mattk
By the way, I am using that facebook-java-api and will check out that link later.
Mattk
A: 

Calling doGet (or doPost or any other visible method) in the instance of the servlet object will execute the containing code. However be aware that behaviour may differ from running the servlet in an web container if the code makes use of any of the "wired-in" context variables.

At face value I would suggest you refactor to have the code you require in a common method and call from both your servlet and your main class.

Il-Bhima