What I want to realize is the following:
In a JSP x I have a 3 dropdownlists and a button called edit when user click this button a table dynamically should be displayed. Now this table is modified correponding the values in those 3 dropdownlists. So it can be 3 cols or 4 cols or even 6 cols, it depends on the selected values. So what I tried is to use the servlet to getParameter
doing the if
clause construct the html and then forward the response. Have you any the suggestion of the structure that I can use in JSP? Thank you.
views:
64answers:
1You shouldn't construct HTML in the servlet. Do it in the JSP. You can use JSTL to control the page flow in JSP page. Just drop jstl-1.2.jar in /WEB-INF/lib
and declare the taglibs as per the aforelinked TLD documentation.
You can use the <c:if>
or <c:choose>
tag to introduce a condition in the page flow. You can use the <c:forEach>
tag to iterate over an array or a collection in JSP. Your functional requirement is pretty vague, but here's a kickoff example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!doctype html>
<html lang="en">
<head>
<title>SO question 2690671</title>
</head>
<body>
<form>
Cols:
<select name="cols">
<option>3</option><option>4</option><option>6</option>
</select><br>
Rows:
<select name="rows">
<option>3</option><option>4</option><option>6</option>
</select><br>
<input type="submit">
</form>
<c:if test="${param.cols > 0 && param.rows > 0}">
<p>Here is a table with ${param.cols} cols and ${param.rows} rows.
<table>
<c:forEach begin="1" end="${param.rows}">
<tr>
<c:forEach begin="1" end="${param.cols}">
<td>cell</td>
</c:forEach>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
Pretty straightforward, isn't it? You actually don't need a servlet here, unless you want to preprocess the rows/cols and implement some validation/business logic. Just change the form action to the servlet URL and let the servlet forward the request to the JSP using requestdispatcher which was already explained to you in several answers before. Don't process the HTML in Servlet. There's no need to do that.
After all, you really need get yourself through some basic JSP/Servlet tutorials/books first instead of stabbing around in the dark. You can find excellent online JSP/Servlet tutorials here and you can find good books here and here. Leave your project aside and concentrate you on actually learning the stuff.