clicking on this will visually collapse the text block directly below it

Servlet Tutorial Part 3 - Accepting Parameters, September 2002

Not Just for Forms

The one thing that seems to confuse a lot of beginning programmers is where and when to use parameters in your servlets. Forms are obvious but there are a multitude of other uses for page parameters and they can be grabbed from the standard request, cookies or sessions. As I mentioned in page one of this on going novel this very servlet uses parameters to know what page to load when a certain link is clicked. This particular page is called when the pageName parameter javaServlet3 is received, so one servlet handles all the content for this site. This parameter also allows the other objects associated with this servlet (IE: the top logo / nav bar object, the left side navbar object & the lower navbar object) to know which page has been requested so the proper links can be highlighted, drop downs used etc, etc. The use of sending and receiving parameters has to become as simple as tying your shoes if you expect to get any where in servlet land.

The Rules of Parameters

Several things MUST be know about parameters from the get go. Once again this has to become common knowledge in your programming. ALL parameters you get from the request object will be Strings. They may be numbers entered in a form, the price of something but they ALL come in as Strings. You can convert them back into usable numbers but until you do they are just plain old Strings! Also when dealing with sessions you have to remember that all objects that come out of a session are generic objects and MUST be cast back into the type object they originally were. (I'll have a session example up here soon that will further explain this). For now we will just stick with plain old standard request parameters. Last but not least (and something I see people screw up everyday) is the fact that you must spell the name of the parameter correctly and yes it is case sensitive! This may sound stupid but I see this mistake every day. So lets build a quick form and use it with our servlet. Click here to see the form layout (you can also cheat and cut / paste the source code, if you really must). OK, we've got our form now let's grab the info when it comes into the servlet. click here to see the new improved code.

Now you're probably asking what the heck all this if logic is for....isn't the form validated by Javascript? Yup it sure is but you have to remember about 10% of the web surfers out there either don't have a javascript enabled browser or have shut it off in there browser. Unless you like error logs a mile long, very upset customers & possibly corrupt databases I suggest you use some form of logic such as this. Each parameter is first checked for null & then if they aren't null the .trim() method is invoked on the incoming parameter to remove white space, then the parameter is checked for an empty string (For those bozos who just hit the space bar to get around your javascript.) DO NOT try to call .trim() on a parameter unless you have checked it and made sure it contains some sort of data or you can throw a null pointer exception right there! Then & only then you accept the parameter and assign it into the proper variable. Also note I have used the Integer.parseInt & Double.parseDouble methods to return my numeric values back into usable numbers! Now we still haven't done anything about input errors so let's fix that. click here to check out the added error handling. Now Click here to use a form with no JavaScript validation at all to show you the error handling in action.
  So there you have the basics of parameter handling. There are others ways to grab the incoming information, two very notable methods are getParameterNames() & getParameterValues(java.lang.String name). These two methods are very helpful when you have multiple values coming in for one named attribute (IE: multi select input boxes). The getParameterNames() returns an enumeration of the incoming parameter names while the getParameterValues returns a String array of the values. With a well constructed loop you can grab all your info in a matter of a couple of lines of code!

So What's Next?

  Well then, we have input and output.....well kind of. Our output is a little on the, how shall we say it? UGLY side! So let's move on & try to make this puppy look like something while we explore Object Oriented Java Deeper (Oh come on, you didn't think we were really done did you?)

Page Four - Breaking Down your servlets into reusable objects

Customer Comments

clicking on this will visually collapse the text block directly below it
Lee Ritenour and his staff and fans are very pleased with the work of the DCD Designs team.
Lee Ritenour Staff, leeritenour.com | More Testimonials >>

Latest Articles / Tutorials

clicking on this will visually collapse the text block directly below it |
Our RSS Article Feed Available as RSS Feed!

Check here often for articles & tutorials that will help make your business grow!

| More Articles & Tutorials >>

Partner Comp. & Friends

clicking on this will visually collapse the text block directly below it