• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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



 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.






 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Daniel Walter
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the keywords are coming through every time the servlet is called, I checked the incoming values from within the app.
 
Daniel Walter
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://c2.com/cgi-bin/wiki?RubberDucking

I use an animated plant.

Or sometimes a co-worker who is disturbingly similar.
 
Daniel Walter
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I think I solved it because of the one eyed greenhorn above this board fulfilling a similar role.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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]
 
reply
    Bookmark Topic Watch Topic
  • New Topic