I'm trying to migrate a standalone java application to a webenviroment.
This application consists of multiple classes that get initiated by the good old 'main' method.
My first approach was to just setup one servlet class, change the application's main method in an arbitrary method name, and just call that method from the servlet.
This servlet is very simple, it has a keyword textfield and a submit button.
When I enter some keywords the servlet passes the keywords via get to the method that was the former main method of the application.
I was surprised to see that the application actually returned the output that I want, the first time i ran the servlet.
However, when I rerun the servlet feeding it different keywords the application stops generating the desired results.
The things i learned about this kind of problem browsing the web:
It could be a scope problem, since the servlet has request scope, while all the other classes do not, since they aren't servlets.
I might try using beans, since they can be set to a particular scope.
But to be honest I don't no how the approach this situation in the first place, should I:
make servlets of the entire application? But should I then have to design all interconnections between classes via service requests etc?
assuming that the application objects are still present in between requests, can't I just destroy the application classes or the references in the application if another request is made by the servlet?
or am I approaching this situation entirely wrong.
It is typical to have a servlet (or similar) act as a "conduit" between web requests and the implementation of back-end functionality--in fact, it's the *preferred* way--since that allows the re-use of back-end functionality in other ways, like applications.
Servlets don't have "request scope", but this may just be a definition issue. Servlets are, however, shared across the entire application: servlets should be thread-safe, although there are different ways to go about achieving that.
Joined: Jun 28, 2009
This is what i'm doing, step by step
I have one servlet class called Frontend. This class has one method of the following signature:
public void service(HttpServletRequest request, HttpServletResponse response)
in this method I do the following:
print the html that represents a keyword text field and the submit button, the submit button calls the same Frontend servlet like this:
in the same service method I call the class -that formely contained the main method) of the former non- webbased application with a replacement of the main function like this:
KeywordSearcher ks = new KeywordSearcher(input);
Note that the constructor now functions as the Main method of my non-webbased application.
Also note that the input argument is an array that holds the keywords that are send by the form.
I retrieve this array like this: String input = request.getParameterValues("search_text");
The constructor calls other classes to find records in a data set. Anyway, I call the servlet when loaded for the first time like this.
Note that these two keywords are handed over to the KeywordSearcher constructor as an array named input. See constructor declaration a few lines up.
Now this what happens: if I call the servlet for the first time the KeywordSearcher returns the desired results and prints them via the response object.
The second time however that I call the servlet with different keywords like this, I notice by the statistics generation of the former non-webbased app that the
app is processing the keywords entirely wrong asif the form search still resides somewhere.
Can you ad a println to your servlet to output the values of input? (println goes to your server console.) This will tell you if the request parameter is "sticking" or if it is something later on in your custom logic.
Yes, the keywords are coming through every time the servlet is called, I checked the incoming values from within the app.
Joined: Jun 28, 2009
Well, the problem is solved! Turns out that a certain object persisted in between calls.
This is the best part of programming: getting insight and solving the problem!
Either way, thanks for giving input, it helped, just being there and letting me formulate my problem got it clearer, even though I didn't get the insight right away.
David Newton wrote:http://c2.com/cgi-bin/wiki?RubberDucking
I use an animated plant.
Or sometimes a co-worker who is disturbingly similar.
Humans help. I've tried describing things to inanimate objects. Doesn't help. What does help is either talking to someone (who doesn't have to be technical) or composing a post here - I often don't press save - just writing it helps.
[edited to fix typo]
subject: From a non-web application to a webbased application using servlets