Converting a Java application to a Servlet
So here's the deal.
I've been programming in Java for a couple of years, but never used any j2ee stuff, so have absolutely no knowledge of how EJB's servlets work!
I currently have an application that does exactly what i want it do, but for reasons outside my control, I need to be able to deploy it as a server.
Is there a simple way of just writing some sort of Servleyt wrapper class that will just kick off my app. Then I could just write a simple web xml and throw the whole lot up onto my Tomcat server.
Any and all help would be much appreciated.
[587 byte] By [
mossya] at [2007-11-27 8:17:04]

> I've been programming in Java for a couple of years,
> but never used any j2ee stuff, so have absolutely no
> knowledge of how EJB's servlets work!
Obviously - EJBs and servlets don't have much to do with each other.
> Is there a simple way of just writing some sort of
> Servleyt wrapper class that will just kick off my
> app.
Depends on how well you designed your app - that is, how easily you can replace the UI part. If you clrearly separated the business logic, not much of a problem.
> > I've been programming in Java for a couple of
> years,
> > but never used any j2ee stuff, so have absolutely
> no
> > knowledge of how EJB's servlets work!
>
> Obviously - EJBs and servlets don't have much to do
> with each other.
knowledge of how EJB's OR servlets work! Sorry about that!
>
> > Is there a simple way of just writing some sort of
> > Servleyt wrapper class that will just kick off my
> > app.
>
> Depends on how well you designed your app - that is,
> how easily you can replace the UI part. If you
> clrearly separated the business logic, not much of a
> problem.
There really is no UI - its simply a case of a program that needs to be running all the time with no real user interaction.
Id like to just launch it using a servlet. Any ideas on how to go about it? Im stumped!
Cheers for the response!
mossya at 2007-7-12 20:02:10 >

> There really is no UI - its simply a case of a
> program that needs to be running all the time with no
> real user interaction.
> Id like to just launch it using a servlet. Any ideas
> on how to go about it? Im stumped!
If there's no user interaction - what exactly do you need the servlet for?
Anyway, still can't say anything about it since I don't know your app. My inital thought was "let your program run as a server process, and let the servlet contact it if needed."
Servlets are certainly not suited for "apps that should run all the time", they're usually very shortlived. But they're classes like everything else and thus can also call other stuff.
> > There really is no UI - its simply a case of a
> > program that needs to be running all the time with
> no
> > real user interaction.
> > Id like to just launch it using a servlet. Any
> ideas
> > on how to go about it? Im stumped!
>
> If there's no user interaction - what exactly do you
> need the servlet for?
>
> Anyway, still can't say anything about it since I
> don't know your app. My inital thought was "let your
> program run as a server process, and let the servlet
> contact it if needed."
>
> Servlets are certainly not suited for "apps that
> should run all the time", they're usually very
> shortlived. But they're classes like everything else
> and thus can also call other stuff.
> If there's no user interaction - what exactly do you
> need the servlet for?
It was being run on the server using a dos window but people were closing it thinking it wasnt important. So it was suggested that a Servlet be used, because it would be tied into Tomcat and as a result would not be closed down on a whim! Does that make sense?
> Anyway, still can't say anything about it since I
> don't know your app. My inital thought was "let your
> program run as a server process, and let the servlet
> contact it if needed."
What the app does is something like the following - it will download some files from a server in one location and upload to them to another server. It needs to do this 4/5 times a day. The only real interaction with the process thats needed is to send a status email when its finished. And possibly to print out the status as its working - but this could as easily be done by just sending it to a log file.
Hopefully it makes more sense now!
mossya at 2007-7-12 20:02:10 >

Why not invoke this via a cron job/scheduled task. It doesn't sound like it needs to be running all the time.
> It was being run on the server using a dos window but
> people were closing it thinking it wasnt important.
> So it was suggested that a Servlet be used, because
> it would be tied into Tomcat and as a result would
> not be closed down on a whim! Does that make sense?
Not to me. Do you know what a PHP script is? Servlet does the same. Does it make sense to you, considering this?
> What the app does is something like the following -
> it will download some files from a server in one
> location and upload to them to another server. It
> needs to do this 4/5 times a day.
You either want this:
http://wrapper.tanukisoftware.org/doc/english/introduction.html
or just use the Windows scheduler with your app.
> It was being run on the server using a dos window but
> people were closing it thinking it wasnt important.
> So it was suggested that a Servlet be used, because
> it would be tied into Tomcat and as a result would
> not be closed down on a whim! Does that make sense?
Sounds like you need to restrict access to that server and/or teach the people who have access not to kill other processes randomly if they don't know what they're doing.
do you know what curry - you are a man after my own heart!
Couple of probs with that though - using a Windows server so no cron job - windows scheduler is **** and finally my boss is stipulating that I must do it that way! D'oh!
So all I need to do is have a 'shell'/'dummy' servlet that can invoke the logic of the previous application.
Any help on how to do this lads?
So would I just remove the main line from the appropriate class, and create a new one called init() for example that has the same logic as the original main line?
mossya at 2007-7-12 20:02:10 >

If your boss tells you to do it that way, consider him explaining the app design for you, including telling you what's going to invoke the servlet.
I know its riducolously convoluted, but such is my lot! Its the servlet or else me getting hassled!
mossya at 2007-7-12 20:02:10 >

> So would I just remove the main line from the
> appropriate class, and create a new one called init()
> for example that has the same logic as the original
> main line?
Servlet still needs to be invoked. Meaning: something needs to send an request to Tomcat.
Pray tell - how would I do that?
mossya at 2007-7-12 20:02:10 >

You can automatically fire up a servlet by specifiying a load-on-startup tag in the deployment descriptor. So providing you know that a container will be restarted in the event it goes down, you should be ok.
Ok excuse the stupidity, but Im going to try and spell out how I would intend to do it based on all your suggestions.
remove the main line in my original application and replace it with init() with the same logic.
Then within my newly created Servlet, what method do I call in order to be able to call
myOriginalApp.init()
which would kick off what was already being done but now within a Servlet process.
mossya at 2007-7-12 20:02:10 >

The container calls init() as and when it needs a servlet instance. If you use a load-on-startup tag in the web.xml, this should be instansiated as the container starts up. This method is called once only.
> remove the main line in my original application and
> replace it with init() with the same logic.
Please, create a new servlet and have it invoke the main method of your app.
> Then within my newly created Servlet, what method do
> I call in order to be able to call
>
> myOriginalApp.init()
You don't call it. Tomcat does. And if curry_monster says Tomcat will go and instantiate a servlet if you correctly tag the deployment descriptor, then it shall work that way, although personally I'm not quite sure.
So using the Hello, World analogy for example purposes:
Lets say my application consists of one class as follows:
public class HelloWorld {
public static void main(String []args){
System.out.println("Hello World");
}
What would my servlet look like?
mossya at 2007-7-21 22:35:09 >

public void init() { HelloWorld.main(null);}
> What would my servlet look like?
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class HelloWorldServlet extends HttpServlet {
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
PrintWriter out = res.getWriter();
out.println("Hello, world!");
out.close();
}
}
~
> public void init() {> HelloWorld.main(null);> }That's definitely more to the letter than my example.~
As curry_monster stated, init() is only called once, when the Servlet is loaded. If you want the code to run each time you access the Servlet, it is probably best if you override Servlet.service, or better yet, HttpServlet.doPost and HttpServlet.doGet if you are using HTTP.
EDIT: yawmark got to it before I did :P
RATiXa at 2007-7-21 22:35:09 >

> > What would my servlet look like?
>
> import java.io.*;
>
> import javax.servlet.http.*;
> import javax.servlet.*;
>
> public class HelloWorldServlet extends HttpServlet {
> public void doGet (HttpServletRequest req,
> HttpServletResponse res)
>throws ServletException, IOException {
> PrintWriter out = res.getWriter();
>out.println("Hello, world!");
> out.close();
> }
>
>
> ~
But this doesnt reference the actual Hello World class that was created. I dont want to actuall want to just print Hello World - i was just using this example. I want to use the HelloWorld class I've already created to be invoked within a servlet!
mossya at 2007-7-21 22:35:09 >

import mossy.*;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class HelloWorldServlet extends HttpServlet {
public void doGet (HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException {
HelloWorldObject mossy = new HelloWorldObject()
PrintWriter out = res.getWriter();
out.println(mossy.getSomeString());
out.close();
}
just make sure you deploy your class with the web app.
Message was edited by:
curry_monster
> [code]
> import mossy.*;
> import java.io.*;
> import javax.servlet.http.*;
> import javax.servlet.*;
>
> public class HelloWorldServlet extends HttpServlet {
> public void doGet (HttpServletRequest req,
> HttpServletResponse res)
>throws ServletException, IOException {
> lloWorldObject mossy = new HelloWorldObject()
> PrintWriter out = res.getWriter();
>out.println(mossy.getSomeString());
> t.close();
> }
> /code]
>
> just make sure you deploy your class with the web
> app.
>
> Message was edited by:
> curry_monster
Cheers curry - its just that rather than printing anything via the servlet (or implementing any logic in fact) I actually just want to kick off the process! Im probably not making myself very clear!
mossya at 2007-7-21 22:35:09 >

> Cheers curry - its just that rather than printing
> anything via the servlet (or implementing any logic
> in fact) I actually just want to kick off the
> process!
Come on, it can't be that tough for you to to extrapolate.
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
kickOffTheProcess();
}
private void kickOffTheProcess() {
// your implementation here; i.e., call whatever class you like.
}
~
Cheers for all your help lads. Am having issues with Tomcat but such is lfe!Thanks again (especially curry),M
mossya at 2007-7-21 22:35:09 >
