1.要求:
通过HTML表单输入一个4个选项的复选框字段,提交给jsp程序,要求在浏览器上输出这4个复选框哪几个被勾选了。
1)HTML程序如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>爱好</title>
</head>
<body>
<form action="strchallenge1.jsp" method="post">
<h1>请选择你的爱好</h1>
<br>
<ol >
<li><p >打篮球<input type=checkbox value="true" name="one"></p></li>
<li><p >听音乐、唱歌<input type=checkbox value="true" name="two"></p></li>
<li><p >看电影<input type=checkbox value="true" name="three"></p></li>
<li><p >打羽毛球<input type=checkbox value="true" name="four"></p></li>
</ol>
<p ><input type="submit" value="提交"></p>
</form>
</body>
</html>
2)jsp程序如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>结果</title>
</head>
<body>
<h1 align="center">结果</h1>
<%
String [] one =request.getParameterValues("one");
String [] two=request.getParameterValues("two");
String [] three=request.getParameterValues("three");
String [] four=request.getParameterValues("four");
%>
<h4 align="center"><%
if(one!=null&&two!=null&&three!=null&&four!=null){
out.print("你选了1、2、3、4");
}
else if(one!=null&&two!=null&&three!=null){
out.print("你选了1、2、3");
}
else if(two!=null&&three!=null&&four!=null){
out.print("你选了2、3、4");
}
else if(one!=null&&three!=null&&four!=null){
out.print("你选了1、3、4");
}else if(one!=null&&two!=null&&four!=null){
out.print("你选了1、2、4");
}else if(one!=null&&two!=null){
out.print("你选了1、2");
}else if(one!=null&&three!=null){
out.print("你选了1、3");
}else if(one!=null&&four!=null){
out.print("你选了1、4");
}else if(two!=null&&three!=null){
out.print("你选了2、3");
}else if(two!=null&&four!=null){
out.print("你选了2、4");
}else if(one!=null){
out.print("你选了1");
}else if(two!=null){
out.print("你选了2");
}
else if(three!=null){
out.print("你选了3");
}
else if(four!=null){
out.print("你选了4");
}else{
out.print("你什么都没选");
}
%></h4>
<a href="strchallenge1.html">返回上一级</a>
</body>
</html>
运行结果:
2.要求:
通过HTML表单输入一个字符串,提交给jsp程序,要求输入的字符串被原模原样输入到浏览器。
例如:输入字符串:abcde 在浏览器上输出为:abcde。
例如:输入字符串:数学<font color=’#ff0000’>100</font>分
在浏览器上输出为:数学<font color=’#ff0000’>100</font>分
而不是输出:数学100分。
1)HTML程序如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>字符串输出</title>
</head>
<body>
<form action="strchallenge2.jsp" method="post">
<h1 align="center">字符串输出</h1>
<p align="center">字符串:<input type="text" name="str1" placeholder="请输入一个字符串"></p>
<p align="center"><input type="submit" value="提交"></p>
</form>
</body>
</html>
2)jsp程序如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>字符串输出</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String str1=request.getParameter("str1");
%>
<h4 align="center"><p ><xmp><% out.print("你输入的字符串是:"+str1);%></xmp></P></h4>>
<a href=strchallenge2.html>返回上一级</a>
</body>
</html>
运行结果: