File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jsp to Servlet parameters

 
Terence Doyle
Ranch Hand
Posts: 328
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
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?
Thanks,

------------------
Terry Doyle
Sun Certified Programmer for Java 2 Platform
 
Jared Krull
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bal Sharma
Ranch Hand
Posts: 273
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 328
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,
Thanks.
I meant a bean INSTEAD of a Servlet.
Anyway I'm trying out fordy's suggestion to make the Servlet work.
Bye,

------------------
Terry Doyle
Sun Certified Programmer for Java 2 Platform
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Terry,
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.
Kyle
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic