• 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

General Servlet Organization Question

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I'm not a Java guru in the least. I am not familiar with it, so I haven't attained an object oriented look to programming yet. I've recently tried to tackle Java as a replacement to PHP. In any case I may be missing out on some cool features to servlets, here's my attempt and maybe someone can give me a shove in the right direction.
Basically what I want is the following:
An application that's web interface is a page broken into five zones that form the core structure (top and bottom rows, left center and right columns). These zones would be filled with small web parts that would each have there own logic to handle database query's and such. The main idea relied on these web parts handling all of the logic and only relied on the servlet for the db connections. Also I wanted the web parts to be in seperate files so that creating a new web part or modifying an old one would be isolated.
Here's the way I was thinking I could do it:
I was looking to have a servlet that maintained db connections to a users db and handled only user authentication, once the user was verified they would be passed to another servlet that maintained db connections to the application db. I was thinking that the web parts could be stored each in their own folder that would contain a code file and a jsp file. On initializing, the servlet would look for these folders and would import the code files. Then you would be left with a huge servlet that had all of the logic in it to handle requests and pass the responses to the web parts jsp pages for display. The main problem that I saw was that if I wanted these jsp pages to handle displaying just the web part, then I would have to import their output into another jsp that handled the core zoning structure. (ie web part 1's output is placed in left zone column and web part 2's output is placed in the center column).
If anyone actually read through this whole post, I would be really happy if they could give me a better way of doing this. A shove in the right direction would be a huge help. Thanks
-Chris
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. The first thing you need to get used to when moving from PHP to Java is that a Java web application is mainly made up of regular Java objects rather than pages or servlets. The only purpose of a servlet is to handle the HTTP protocol. In a good design a servlet should be quite small and single-purpose, handing off all the processing to regular java classes.
So. Let's simplify your question to start with, imagine we only want a web application with just two "page zones" - a header, and a a main part. Let's start with a simple servlet which sits waiting for requests, and passes them to our two "zones":

Notice that we know very little about about these zones at the moment. They have a constructor taking a string, and they have a method "generate" which takes a HttpRequest to get parameters and stuff from, and a Writer to write the output to.
To make this work in a very simple way, let's code a basic PageZone class:

Now, you should be able to compile these two classes, place them in WEB-INF/classes in a servlet container, and run the servlet to generate an ultra-simple two-zone page:

To make the page zones smarter, there are loads of things you could do such as using the supplied name to find a file or directory containing chunks of HTML to show instead of the hard coded text. Or you could fetch information from a database. You have the whole power of the Java language at your disposal, and you might never need to change that servlet code again.
If you want to get really fancy, you could even change the servlet code a little to read the names of the classes to create for each of your zones from some sort of configuration file, rather than hard-coding them in the servlet.
For more suggestions and tutorials about writing, deploying and and testing Java web applications, I suggest you check out my article series "Small and Simple Web applications, the Friki way", in the JavaRanch journal
I hope this has helped.
 
Chris Snapp
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the kick in the tail....I'm working through your "Friki Way" articles now. I've never run any java from the command prompt....I'm on article two for the testing piece and when I type:
"java junit.swingui.TestRunner tests.AllTests"
I get this as an error:
Exception in thread "main" java.lang.NoClassDefFoundError: junit/swingui/TestRunner
Sorry to pester with a simple beginner problem, but it'd be nice to continue with the articles and not be stuck with an error.
-Chris
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to hava JUnit (www.junit.org) installed, and the file junit.jar needs to be on your CLASSPATH, the environment variable that tells Java where to find libraries. Here's a primer on CLASSPATH.
 
Chris Snapp
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm....call me an idiot, but that sounds like I need to type:
java -classpath "C:\eclipse\Servlet JAR\junit3.8.1\junit.jar" "c:\eclipse\workspace\Idealens\test" junit.swingui.TestRunner tests.AllTests
kinda seems like that can't be right.
-Chris
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Almost. If you really want to do it that way, you will need a semi-colon ";" between the two classpath entries rather than a space.
However, I can never be bothered to type all that stuff, so if I'm going to use the command line version, I usually just put it in a batch file instead (called, for example junit.bat):

then you can run your tests by typing
junit tests.AllTests
Note that you only really need the quotes if you have spaces or other wierd characters in a path entry.
Of course, you will find as you progress through the articles, that I don't do this myself these days. I tend to start my unit tests either from Ant, or from Eclipse, which (when you have it set up) makes it all a lot smoother. But it's worthwhile getting the hang of this stuff now, so you can work out how to run some strange and undocumented piece of open source software when you need it in a hurry
 
Chris Snapp
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well thanks to all who were patient enough to hold my hand through this stuff. I appreciate the info, and I'm sure I'll be busy trying to learn everything. Thanks again
-Chris
 
I child proofed my house but they still get in. Distract them with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic