Link to home
Start Free TrialLog in
Avatar of javabeginr
javabeginr

asked on

Dynamic forms

I'm trying to create a dynamic form using JSP, servlet, etc.  When I say dynamic, for example I have 15 different forms with different questions that will display depending on which type a questions is selected on the page before the form page.  What I'm trying to do is have one jsp for these 10 forms instead of creating 10 pages with different form content.  I have all the questions in my database and can display one set of questions on a jsp but trying to figure out how to display the text area, text field, list/menu box next to the questions that need one of these form entries.  I started out trying to use a java string buffer but haven't been successful.  Can somebody give me some answers on how to construct this project?  I've searched on the web for an example but no luck.  If you know of a tutorial or a book that gives step by step instructions please let me know.  Thank You.

Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Can you post some of your code?

I'm not 100% sure what your problem is :-(
Avatar of javabeginr
javabeginr

ASKER

First should I use a string buffer to accomplish what I'm trying to do?  The problem is I'm not sure how to do what I'm trying to do (displaying questions and a text box dynamically).  What I need is somebody who has actually done this before and can show me, so I can understand what's happening.  Again can somebody show me or direct me to a website or a book that explains = displaying form content dynamically.  

I always start with a static form which I edit in HTML first.
Once I have the HTML form built as a static file, I then create a template from the HTML.
Once I have built several templates (from all the forms I need to create) I identify template parts that appear in more than just one form.
I then create Servlet code (or JSP tags) that output the template parts I've previously identified.

When I work with Servlets, I usually have some sort of generic Tag-based class that I can use as follows:

      //Process the HTTP Get request
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            HtmlWriter out = new HtmlWriter(response);
            //The size of the client's visible area within a Web Browser
            Dimension clientSize = ServletUtils.getDimension("clientSize", request);
            String pageName = (clientSize == null) ? "IdeBoot" : "Ide Main";
            out.html("Project Ghost [ " + pageName + " ]");
            out.setBodyAttribute("bgcolor", "#300303");
            out.setBodyAttribute("style", "margin:0px;border:none;");
            if (clientSize == null) {
                  out.openTag("table");
                  out.addTagAttribute("border", "0");
                  out.addTagAttribute("height", "100%");
                  out.addTagAttribute("width", "100%");
                  out.openTag("tr");
                  out.addTagAttribute("height", "1%");
                  out.openTag("td");
                  out.addTagAttribute("width", "1%");
                  addApplet(false, out, 5, 5, "left", 0);
                  out.closeTag("tr");
                  out.emptyTag("td");
                  out.emptyTag("td");
                  out.closeTag("table");
                  out.openTag("tr");
                  out.addTagAttribute("height", "98%");
                  out.openTag("td");
                  out.addTagAttribute("width", "100%");
                  out.addTagAttribute("colSpan", "3");
                  out.openTag("p");
                  out.addTagAttribute("align", "center");
                  addApplet(false, out, 600, 26, "middle", -1);
                  out.closeTag("table");
                  out.openTag("tr");
                  out.addTagAttribute("height", "1%");
                  out.emptyTag("td");
                  out.emptyTag("td");
                  out.openTag("td");
                  out.addTagAttribute("width", "1%");
                  addApplet(false, out, 5, 5, "right", 1);
            } else {
                  addApplet(true, out, clientSize.width, clientSize.height, "middle", -1);
            }
            out.output();
      }

As you can see, my HtmlWriter actually *knows* how to create, open and close tags, building them into a tag-tree and making sure they all close properly.  Then, once I'm done adding tags to my HtmlWriter, I simply call its output() method to write everything through the HttpServletResponse instance.

Good luck and I hope this helps,
Doron
Well thanks for this but I would rather not use swing.  I'm still focused on using StringBuffer or PrintWriter.  It looks like I'm not going into enough detail so I'll explain some more.  If I have a jsp called DropDown.jsp that contains a drop down menu/list like the example below


<html>
<head>
</head>
<body>
<form name="form" method="post" action="">
  <select name="select">
    <option>Form 1</option>
    <option>Form 2</option>
    <option>Form 3</option>
  </select>
</form>
</body>
</html>



If I select form 1, form 2, or form 3 a method in my servlet will display the content of the form you pick on say a jsp called AllForms.  The method in my servlet will be something like this

buf.append("<html>");
buf.append("<head>");
buf.append("<title>Form 1</title>");
buf.append("</head>");
buf.append("<body>");
buf.append("<form name="form1" method="post" action="">");
buf.append("<table">");
buf.append("<tr>");
buf.append("<td>Name:</td>");
buf.append("<td><input type="text" name="textfield"></td>");
buf.append("</tr>");
buf.append("</body>");
buf.append("</html>");

I'll use a return to the AllForms.jsp


Say the Form 2's method is different and something like this


buf.append("<html>");
buf.append("<head>");
buf.append("<title>Form 2</title>");
buf.append("</head>");
buf.append("<body>");
buf.append("<form name="form2" method="post" action="">");
buf.append("<table">");
buf.append("<tr>");
buf.append("<td>Color:</td>");
buf.append("<td><select name="select">
    <option>Blue</option>
   <option>Red</option>
      </select></td>");
buf.append("</tr>");
buf.append("</body>");
buf.append("</html>");


Of course my forms are a lot more intense with sometimes more than 10 questions depending on which form is selected  and the form fields will have text fields, text area, radio button, checkbox but each form will have different questions but have the same form fields.  My question what is the most productive way of doing this?  Can somebody give me an example of how I can loop through this and display the contents on a jsp using a method using string buffer?




Who said anything about using Swing?!!?!?  The code I posted does not use Swing.  My HtmlWriter is a special helper class I wrote that wraps a PrintWriter over the HttpServletResponse.

I don't use JSP as I find it easier to completely program the HTML output myself, but I think that what you are looking for is actually defining your own Tag Library so that you can use them in your JSP forms.  However, if I'm not mistaken, the sample you've attached is not from a JSP but rather a Servlet since a JSP doesn't requite adding the HTML/TITLE/BODY tags to be embedded in a StringBuffer.

Here is a JSP taglib tutorial presented by Sun: [ http://java.sun.com/products/jsp/tutorial/TagLibrariesTOC.html ]

In conclusion, if you're looking for the JSP solution, read up on creating Tag Libraries where your customized tags can render dynamic content based on parameters you send through JSP taglib-syntax.  If on the other hand, you're looking for a Servlet solution, then working with a StringBuffer and PrintWriter over the HttpServletResponse instance would be the way to go.  If you want some code samples for that, I can post more code to demonstrate the process.
ASKER CERTIFIED SOLUTION
Avatar of doronb
doronb

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the example.  
You're welcome and I hope it will help you in your project.
Doronb,

Can you add the jsp so I can see how you call the things you're displaying on the jsp?
The servlet does the work in my previous sample, so there is no JSP actually.

A good example of a JSP iterating through data could be found here: [ http://java.sun.com/products/jsp/tutorial/TagLibraries16.html#62513 ]