wood burning stoves 2.0*
The moose likes Servlets and the fly likes From a non-web application to a webbased application using servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "From a non-web application to a webbased application using servlets " Watch "From a non-web application to a webbased application using servlets " New topic
Author

From a non-web application to a webbased application using servlets

Daniel Walter
Greenhorn

Joined: Jun 28, 2009
Posts: 7
Hi there,

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.


Daniel



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Without knowing more it's hard to help.

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.
Daniel Walter
Greenhorn

Joined: Jun 28, 2009
Posts: 7
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:

out.println("<form action='Frontend' method='get'>");

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.

http://localhost:8080/Keywords/Frontend?search_text=keyword1&search_text=keyword2

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.






Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30076
    
149

Daniel,
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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Daniel Walter
Greenhorn

Joined: Jun 28, 2009
Posts: 7
Yes, the keywords are coming through every time the servlet is called, I checked the incoming values from within the app.
Daniel Walter
Greenhorn

Joined: Jun 28, 2009
Posts: 7
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.

Dan
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

http://c2.com/cgi-bin/wiki?RubberDucking

I use an animated plant.

Or sometimes a co-worker who is disturbingly similar.
Daniel Walter
Greenhorn

Joined: Jun 28, 2009
Posts: 7


I think I solved it because of the one eyed greenhorn above this board fulfilling a similar role.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30076
    
149

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]
 
wood burning stoves
 
subject: From a non-web application to a webbased application using servlets
 
Similar Threads
IBM HttpServer and websphere Appserver
servlet throws IllegalAccessError when trying to access another class
Creating a custom scope... (kinda long)
Architecture for a web application
MVC (urgent)