1. 체크박스 데이터 폼 체크를 foreach로 체크하기
1.1 index.jsp
<form method="post" action="checkbox.jsp">
<P>Please check adjectives you would
use to describe this web site's
customer service:</p>
<P>Atrocious
<input type="checkbox" name="feedback" value="atrocious"/></p>
<P>Loathsome
<input type="checkbox" name="feedback" value="loathsome"/></p>
<P>Flagitious
<input type="checkbox" name="feedback" value="flagitious"/></p>
<P>Satisfactory
<input type="checkbox" name="feedback" value="satisfactory"/></p>
<P><input type="submit" value="Submit" /></p>
</form>
1.2 checkbox.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<c:choose>
<c:when test="${not empty paramValues.feedback}">
You described our customer service as
<ul>
<c:forEach items="${paramValues.feedback}" var="adj">
<li><c:out value="${adj}"/></li>
</c:forEach>
</ul>
</c:when>
<c:otherwise>
You didn't choose any feedback checkboxes.
</c:otherwise>
</c:choose>
2. Choose/When/Ohterwise를 이용한 체크박스 폼 데이터 처리
2.1 index.jsp
<form method="post" action="checkbox.jsp">
<P>Please check adjectives you would
use to describe this web site's
customer service:</p>
<P>Atrocious
<input type="checkbox" name="feedback" value="atrocious"/></p>
<P>Loathsome
<input type="checkbox" name="feedback" value="loathsome"/></p>
<P>Flagitious
<input type="checkbox" name="feedback" value="flagitious"/></p>
<P>Satisfactory
<input type="checkbox" name="feedback" value="satisfactory"/></p>
<P><input type="submit" value="Submit" /></p>
</form>
2.2 checkbox.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<c:choose>
<c:when test="${not empty paramValues.feedback}">
You described our customer service as
<ul>
<c:forEach items="${paramValues.feedback}" var="adj">
<c:choose>
<c:when test="${adj == 'satisfactory'}">
<font size="+2">
</c:when>
<c:otherwise>
<font size="-2">
</c:otherwise>
</c:choose>
<li><c:out value="${adj}"/></li>
</font>
</c:forEach>
</c:when>
<c:otherwise>
You didn't choose any feedback checkboxes.
</c:otherwise>
</c:choose>
3. CheckBox를 다루기 위한 Bean 이용
3.1 index.jsp
<BODY bgcolor="white">
<FORM TYPE=POST ACTION=checkresult.jsp>
<BR>
<font size=5 color="red">
Check all Favorite fruits: <br>
<input TYPE=checkbox name=fruit VALUE=apples> Apples <BR>
<input TYPE=checkbox name=fruit VALUE=grapes> Grapes <BR>
<input TYPE=checkbox name=fruit VALUE=oranges> Oranges <BR>
<input TYPE=checkbox name=fruit VALUE=melons> Melons <BR>
<br> <INPUT TYPE=submit name=submit Value="Submit">
</font>
</FORM>
</BODY>
</HTML>
3.2 checkresult.jsp
<body bgcolor="white">
<font size=5 color="red">
<%! String[] fruits; %>
<jsp:useBean id="foo" scope="page" class="beans.MyBean" />
<jsp:setProperty name="foo" property="fruit" param="fruit" />
<hr>
The checked fruits (got using request) are: <br>
<%
fruits = request.getParameterValues("fruit");
%>
<ul>
<%
if (fruits != null) {
for (int i = 0; i < fruits.length; i++) {
%>
<li>
<%
out.println (beans.HTMLFilter.filter(fruits[i]));
}
} else out.println ("none selected");
%>
</ul>
<br>
<hr>
The checked fruits (got using beans) are <br>
<%
fruits = foo.getFruit();
%>
<ul>
<%
if (!fruits[0].equals("1")) {
for (int i = 0; i < fruits.length; i++) {
%>
<li>
<%
out.println (beans.HTMLFilter.filter(fruits[i]));
}
} else out.println ("none selected");
%>
</ul>
</font>
</body>
</html>
3.3 MyBean.java
package beans;
import java.beans.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.text.DateFormat;
import java.util.*;
public class MyBean {
String b[] = new String[] { "1", "2", "3", "4" };
public String[] getFruit() {
return b;
}
public void setFruit(String [] b) {
this.b = b;
}
}
3.4 HTMLFilter class
package beans;
/**
* HTML filter utility.
*
* @author Craig R. McClanahan
* @author Tim Tye
* @version $Revision: 466607 $ $Date: 2006-10-21 17:09:50 -0600 (Sat, 21 Oct 2006) $
*/
public final class HTMLFilter {
/**
* Filter the specified message string for characters that are sensitive
* in HTML. This avoids potential attacks caused by including JavaScript
* codes in the request URL that is often reported in error messages.
*
* @param message The message string to be filtered
*/
public static String filter(String message) {
if (message == null)
return (null);
char content[] = new char[message.length()];
message.getChars(0, message.length(), content, 0);
StringBuffer result = new StringBuffer(content.length + 50);
for (int i = 0; i < content.length; i++) {
switch (content[i]) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
case '"':
result.append(""");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
}
1.1 index.jsp
<form method="post" action="checkbox.jsp">
<P>Please check adjectives you would
use to describe this web site's
customer service:</p>
<P>Atrocious
<input type="checkbox" name="feedback" value="atrocious"/></p>
<P>Loathsome
<input type="checkbox" name="feedback" value="loathsome"/></p>
<P>Flagitious
<input type="checkbox" name="feedback" value="flagitious"/></p>
<P>Satisfactory
<input type="checkbox" name="feedback" value="satisfactory"/></p>
<P><input type="submit" value="Submit" /></p>
</form>
1.2 checkbox.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<c:choose>
<c:when test="${not empty paramValues.feedback}">
You described our customer service as
<ul>
<c:forEach items="${paramValues.feedback}" var="adj">
<li><c:out value="${adj}"/></li>
</c:forEach>
</ul>
</c:when>
<c:otherwise>
You didn't choose any feedback checkboxes.
</c:otherwise>
</c:choose>
2. Choose/When/Ohterwise를 이용한 체크박스 폼 데이터 처리
2.1 index.jsp
<form method="post" action="checkbox.jsp">
<P>Please check adjectives you would
use to describe this web site's
customer service:</p>
<P>Atrocious
<input type="checkbox" name="feedback" value="atrocious"/></p>
<P>Loathsome
<input type="checkbox" name="feedback" value="loathsome"/></p>
<P>Flagitious
<input type="checkbox" name="feedback" value="flagitious"/></p>
<P>Satisfactory
<input type="checkbox" name="feedback" value="satisfactory"/></p>
<P><input type="submit" value="Submit" /></p>
</form>
2.2 checkbox.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<c:choose>
<c:when test="${not empty paramValues.feedback}">
You described our customer service as
<ul>
<c:forEach items="${paramValues.feedback}" var="adj">
<c:choose>
<c:when test="${adj == 'satisfactory'}">
<font size="+2">
</c:when>
<c:otherwise>
<font size="-2">
</c:otherwise>
</c:choose>
<li><c:out value="${adj}"/></li>
</font>
</c:forEach>
</c:when>
<c:otherwise>
You didn't choose any feedback checkboxes.
</c:otherwise>
</c:choose>
3. CheckBox를 다루기 위한 Bean 이용
3.1 index.jsp
<BODY bgcolor="white">
<FORM TYPE=POST ACTION=checkresult.jsp>
<BR>
<font size=5 color="red">
Check all Favorite fruits: <br>
<input TYPE=checkbox name=fruit VALUE=apples> Apples <BR>
<input TYPE=checkbox name=fruit VALUE=grapes> Grapes <BR>
<input TYPE=checkbox name=fruit VALUE=oranges> Oranges <BR>
<input TYPE=checkbox name=fruit VALUE=melons> Melons <BR>
<br> <INPUT TYPE=submit name=submit Value="Submit">
</font>
</FORM>
</BODY>
</HTML>
3.2 checkresult.jsp
<body bgcolor="white">
<font size=5 color="red">
<%! String[] fruits; %>
<jsp:useBean id="foo" scope="page" class="beans.MyBean" />
<jsp:setProperty name="foo" property="fruit" param="fruit" />
<hr>
The checked fruits (got using request) are: <br>
<%
fruits = request.getParameterValues("fruit");
%>
<ul>
<%
if (fruits != null) {
for (int i = 0; i < fruits.length; i++) {
%>
<li>
<%
out.println (beans.HTMLFilter.filter(fruits[i]));
}
} else out.println ("none selected");
%>
</ul>
<br>
<hr>
The checked fruits (got using beans) are <br>
<%
fruits = foo.getFruit();
%>
<ul>
<%
if (!fruits[0].equals("1")) {
for (int i = 0; i < fruits.length; i++) {
%>
<li>
<%
out.println (beans.HTMLFilter.filter(fruits[i]));
}
} else out.println ("none selected");
%>
</ul>
</font>
</body>
</html>
3.3 MyBean.java
package beans;
import java.beans.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.text.DateFormat;
import java.util.*;
public class MyBean {
String b[] = new String[] { "1", "2", "3", "4" };
public String[] getFruit() {
return b;
}
public void setFruit(String [] b) {
this.b = b;
}
}
3.4 HTMLFilter class
package beans;
/**
* HTML filter utility.
*
* @author Craig R. McClanahan
* @author Tim Tye
* @version $Revision: 466607 $ $Date: 2006-10-21 17:09:50 -0600 (Sat, 21 Oct 2006) $
*/
public final class HTMLFilter {
/**
* Filter the specified message string for characters that are sensitive
* in HTML. This avoids potential attacks caused by including JavaScript
* codes in the request URL that is often reported in error messages.
*
* @param message The message string to be filtered
*/
public static String filter(String message) {
if (message == null)
return (null);
char content[] = new char[message.length()];
message.getChars(0, message.length(), content, 0);
StringBuffer result = new StringBuffer(content.length + 50);
for (int i = 0; i < content.length; i++) {
switch (content[i]) {
case '<':
result.append("<");
break;
case '>':
result.append(">");
break;
case '&':
result.append("&");
break;
case '"':
result.append(""");
break;
default:
result.append(content[i]);
}
}
return (result.toString());
}
}
'WEB_Programming > JSTL' 카테고리의 다른 글
15. JSTL 폼 Select 예제 (0) | 2008.06.24 |
---|---|
14. JSTL Input 폼 처리 (0) | 2008.06.20 |
12. JSTL 예외 처리 (0) | 2008.06.20 |
11. JSTL Cookie 처리 (0) | 2008.06.20 |
10. JSTL의 Scope (0) | 2008.06.20 |