JSP for loop output all elements of an array

request.jsp call par.jsp:


request.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>request.jsp</title>
</head>
<body>
	<form action="par.jsp" method="post">
	Name: <input type="text" name="name"><br/>
	Age: <input type="text" name="age"><br/>
	<br/>
	<input type="radio" name="sex" value="Male" checked>Male
	<input type="radio" name="sex">Femal<br>
	<br/>
	<input type="checkbox" name="hobby" value="Reading book">Reading
	<input type="checkbox" name="hobby" value="Playing Game">Playing Game
	<input type="checkbox" name="hobby" value="Listening Music">Listening Music
	<input type="checkbox" name="hobby" value="Basketball">Basketball<br/>
	<input type="submit" value="Submit">
	</form>
</body>
</html>

par.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>par.jsp</title>
</head>
<body>
	<%
	//request.setCharacterEncoding(("utf-8");
	String name=request.getParameter("name");
	String age=request.getParameter("age");
	String sex=request.getParameter("sex");
	%>
	Name: <%=name %>
	<br/>
	Age: <%=age %>
	<br/>
	Sex: <%=sex %>
	<%
		String[] hobby=request.getParameterValues("hobby");
		if(hobby!=null){
			for(int i=0; i< hobby.length;++i){ 
	%>
	<table>
	<tr>
			<td>hobby<%=i %>: <%=hobby[i] %></td>
	</tr>
	</table>
	
	<%} }%>
</body>
</html>

par.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>par.jsp</title>
</head>
<body>
	<%
	//request.setCharacterEncoding(("utf-8");
	String name=request.getParameter("name");
	String age=request.getParameter("age");
	String sex=request.getParameter("sex");
	%>
	Name: <%=name %>
	<br/>
	Age: <%=age %>
	<br/>
	Sex: <%=sex %>
	<%
		String[] hobby=request.getParameterValues("hobby");
		if(hobby!=null){
			for(int i=0; i< hobby.length;++i){
				out.println(hobby[i]+"<br>");
			}
		}
	%>	
</body>
</html>


你可能感兴趣的:(JSP for loop output all elements of an array)