Jsp中通过JavaBean实现问卷调查
TestJsp15.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Jsp中通过JavaBean实现问卷调查</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="TestJsp15_reg.jsp" method="post">
<div>
<h1>调查问卷</h1>
<hr/>
<ul>
<li>你经常用哪些编程语言开发程序:</li>
<li>
<input type="checkbox" name="languages" value="Java">Java
<input type="checkbox" name="languages" value="C#">C#
<input type="checkbox" name="languages" value="C">C
<input type="checkbox" name="languages" value="C++">C++
</li>
</ul>
<ul>
<li>你目前所掌握的技术:</li>
<li>
<input type="checkbox" name="technics" value="HTML">HTML
<input type="checkbox" name="technics" value="CSS">CSS
<input type="checkbox" name="technics" value="Servlet">Servlet
<input type="checkbox" name="technics" value="JavaBean">JavaBean
</li>
</ul>
<ul>
<li>在学习中哪部分感觉有困难:</li>
<li>
<input type="checkbox" name="parts" value="JSP">JSP
<input type="checkbox" name="parts" value="Struts2">Struts2
</li>
</ul>
<input type="submit" value="提交">
</div>
</form>
</body>
</html>
TestJsp15_reg.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>Jsp中通过JavaBean实现问卷调查</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<jsp:useBean id="paper" class="com.test.bean.Paper"></jsp:useBean>
<jsp:useBean id="convert" class="com.test.bean.Convert"></jsp:useBean>
<jsp:setProperty property="*" name="paper"/>
<div>
<h1>调查结果</h1>
<hr/>
<ul>
<li>
你经常使用的编程语言:<%=convert.arr2Str(paper.getLanguages()) %>
</li>
<li>
你目前所掌握的技术:<%=convert.arr2Str(paper.getTechnics()) %>
</li>
<li>
在学习中感觉有困难的部分:<%=convert.arr2Str(paper.getParts()) %>
</li>
</ul>
</div>
</body>
</html>
Paper.java
package com.test.bean;
public class Paper {
private String[] languages;
private String[] technics;
private String[] parts;
public Paper(){
}
public String[] getLanguages(){
return languages;
}
public void setLanguages(String[] languages){
this.languages = languages;
}
public String[] getTechnics(){
return technics;
}
public void setTechnics(String[] technics){
this.technics = technics;
}
public String[] getParts(){
return parts;
}
public void setParts(String[] parts){
this.parts = parts;
}
}
Convert.java
package com.test.bean;
public class Convert {
/**
* 将数组转换成字符串
* @param arr 数组
* @return 字符串
*/
public String arr2Str(String[] arr){
//实例化StringBuffer
StringBuffer sb = new StringBuffer();
//判断arr是否为有效数组
if(arr != null && arr.length > 0){
//遍历数组
for(String s : arr){
//将字符串追加到StringBuffer中
sb.append(s);
//将","追加到StringBuffer中
sb.append(",");
}
//判断字符串长度是否有效
if(sb.length() > 0){
//截取字符
sb = sb.deleteCharAt(sb.length()-1);
}
}
//返回字符串
return sb.toString();
}
}