wood burning stoves 2.0*
The moose likes Servlets and the fly likes Jsp to Servlet parameters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Jsp to Servlet parameters" Watch "Jsp to Servlet parameters" New topic

Jsp to Servlet parameters

Terence Doyle
Ranch Hand

Joined: May 30, 2001
Posts: 328
I'm writing some practice servlets and I thought it would be nice to try write a basic DB access servlet that takes in 4 parameters from a form ( dbase name, table name ,search style and search word).
In the form I have the servlet configured with Strings that are the same names as the form inputs. The servlet has appropriate get/set methods for the 4 strings.
How do I send the servlet the parameters?
Is this something better done by a bean?

Terry Doyle
Sun Certified Programmer for Java 2 Platform

Raising Flares debut album 'Ignition' out now


Terry Doyle <br />SCPJ 1.4 , SCWCD , SCMAD(Beta)
Jared Krull
Ranch Hand

Joined: Sep 28, 2001
Posts: 32
Write a form in HTML and under the action part put the address of your servlet.
In the code of your servlet have use request.getParameter("name") to get the string associated with the "name" part of your form.
Simple & bean free.....
C programmer gone astray

C programmer gone astray
Bal Sharma
Ranch Hand

Joined: Sep 19, 2001
Posts: 273
I am not sure you can use java bean on servlet. It is gonna be new thing me to learn. I have not seen any program embeded bean with servlet. You could be right! Why not to try JSP+SERVLET? You can use bean as well as call servlet from JSP page. If you know servlet you have enough prereqisite to move on JSP. Just $0.00199 advice.
Thanks BK.

Is this something better done by a bean?

Terence Doyle
Ranch Hand

Joined: May 30, 2001
Posts: 328
I meant a bean INSTEAD of a Servlet.
Anyway I'm trying out fordy's suggestion to make the Servlet work.

Terry Doyle
Sun Certified Programmer for Java 2 Platform
Kyle Brown
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
Most good servlet books (mine, Jason Hunter's, etc.) will show an example or two of how to write a Servlet that takes parameters in from a form (using Fordy's method) and then creates a Java Bean from the information in the parameters. You can then call other business logic (perhaps with this bean as an argument) and then get some "output beans" that can be displayed by a JSP.
Usually, you get the following flow.
1. Get the form data using req.getParameter(...);
2. Populate an "input bean" from the parameter values
4. Validate the "input bean" (make sure all the input is complete)
5. Execute your business logic (database calls)
6. Create an "output bean" to hold the results of the bus. logic
7. Put the "output bean" in the request (req.setAttribute(...))
8. Get a request dispatcher from the servlet context that points to a JSP to handle the output.
9. use RequestDispatcher.forward() to pass control to the JSP
10. use the <jsp:useBean> tag to display the "output" bean in the JSP.
That's pretty much the standard flow for Servlets, Java Beans, and JSP's. This is referred to as an "MVC" or Model-II architecture. See my book (or Jason Hunter's) for more details.
P.S. DON'T put variables in the servlet, ever! Servlets are resources shared by multiple threads (one thread per user request). You'll be in for a nightmare if you try it. You should be able to handle the entire flow above all inside the doPost() method.
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.

Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Jsp to Servlet parameters
Similar Threads
POST file and parameters to servlet from Java Application?
getParameter returns truncated string
Servlet and XML result HTTP 404
Passing values from JSP page to Java program
passing parameters form applet to servlet