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.
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( "clientSiz e", request);
String pageName = (clientSize == null) ? "IdeBoot" : "Ide Main";
out.html("Project Ghost [ " + pageName + " ]");
out.setBodyAttribute("bgco lor", "#300303");
out.setBodyAttribute("styl e", "margin:0px;border:none;") ;
if (clientSize == null) {
out.openTag("table");
out.addTagAttribute("borde r", "0");
out.addTagAttribute("heigh t", "100%");
out.addTagAttribute("width ", "100%");
out.openTag("tr");
out.addTagAttribute("heigh t", "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("heigh t", "98%");
out.openTag("td");
out.addTagAttribute("width ", "100%");
out.addTagAttribute("colSp an", "3");
out.openTag("p");
out.addTagAttribute("align ", "center");
addApplet(false, out, 600, 26, "middle", -1);
out.closeTag("table");
out.openTag("tr");
out.addTagAttribute("heigh t", "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
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(
String pageName = (clientSize == null) ? "IdeBoot" : "Ide Main";
out.html("Project Ghost [ " + pageName + " ]");
out.setBodyAttribute("bgco
out.setBodyAttribute("styl
if (clientSize == null) {
out.openTag("table");
out.addTagAttribute("borde
out.addTagAttribute("heigh
out.addTagAttribute("width
out.openTag("tr");
out.addTagAttribute("heigh
out.openTag("td");
out.addTagAttribute("width
addApplet(false, out, 5, 5, "left", 0);
out.closeTag("tr");
out.emptyTag("td");
out.emptyTag("td");
out.closeTag("table");
out.openTag("tr");
out.addTagAttribute("heigh
out.openTag("td");
out.addTagAttribute("width
out.addTagAttribute("colSp
out.openTag("p");
out.addTagAttribute("align
addApplet(false, out, 600, 26, "middle", -1);
out.closeTag("table");
out.openTag("tr");
out.addTagAttribute("heigh
out.emptyTag("td");
out.emptyTag("td");
out.openTag("td");
out.addTagAttribute("width
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
ASKER
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?
<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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the example.
You're welcome and I hope it will help you in your project.
ASKER
Doronb,
Can you add the jsp so I can see how you call the things you're displaying on the jsp?
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 ]
A good example of a JSP iterating through data could be found here: [ http://java.sun.com/products/jsp/tutorial/TagLibraries16.html#62513 ]
I'm not 100% sure what your problem is :-(