3、编写两个JSP页面,用户可以使用inputString.jsp提供的表单输入一个字符串,并提交给另一个jsp页面,该页面通过内置对学校获取inputString.jsp页面提价的字符串,并显示该字符串的长度
<!-- inputString.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>inputString</title>
</head>
<body>
<form action="computer.jsp" method="get" name=form>
<table border = 1>
<tr><td>请输入字符串:<br><input type="text" name="str"></td></tr>
<tr><td><input type="submit" value="提交" name="submit"></td></tr>
</table>
</form>
</body>
</html>
<!-- computer.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>computer</title>
</head>
<body>
<table border = 1>
<tr><td>
输入的字符串为:
<% String stri=request.getParameter("str") ;
%>
<%= stri %> </td></tr>
<tr><td>
输入的字符长度为:<%= stri.length() %>
</td></tr>
</table>
</body>
</html>
6、参照猜100以内数字例子编写一个猜英文26个小写字母的web游戏
主要将课本的例子随机生成一个正整形的100以内的数字改为随机生成一个小写字母,如下。然后将后面的获取整形数值改为获取一个字符型
<%
char a[] = new char[26];
int m=0;
for(char c='a';c<='z';c++){
a[m] = c;
m++;
}
int num=(int)(Math.random()*a.length);
char ch =a[num];
下面是完整代码
<!-- inputInt.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>Insert title here</title>
</head>
<body>
随机分给了你一个a~z之间的一个小写字母,请猜!
<%
char a[] = new char[26];
int m=0;
for(char c='a';c<='z';c++){
a[m] = c;
m++;
}
int num=(int)(Math.random()*a.length);
char ch =a[num];
session.setAttribute("count",new Integer(0));
session.setAttribute("save",new Character(ch));
%>
<br><a HREF = "guess.jsp">点击这里去猜这个字母</a>
</body>
</html>
<!-- guess.jsp -->
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib tagdir = "/WEB-INF/tags" prefix = "guessNumber" %>
<!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>Insert title here</title>
</head>
<body>
<% String str = request.getParameter("guessLetter");
if(str == null)
str = "*";
if(str.length()==0)
str = "*";
%>
<guessNumber:GuessTag number="<%= str %>"></guessNumber:GuessTag>
当前的猜测结果:<%= message %>
<% if(message.startsWith("你猜对了")){
%> <br><a HREF = "inputInt.jsp">重新获取一个字母</a>
<% }
else{
%> <br>输入你的猜测(a~z):
<form action=""method="post" name=form>
<INPUT type = "text" name = "guessLetter">
<INPUT TYPE = "submit" value= "送出" name="submit">
</form>
<% }
%>
</body>
</html>
<!-- GuessTag.tag -->
<%@ tag language="java" pageEncoding="utf-8"%>
<%@ attribute name="number" required = "true"%>
<%@ variable name-given = "message" scope="AT_END" %>
<% String mess= "";
Character ch = (Character)session.getAttribute("save");
char realnumber = ch.charValue();
//int guessNumber= 0;
char guessNumber =(number.trim()).charAt(0);
//boolean boo = true;
//try{ guessNumber = Integer.parseInt(number);
//}
//catch(Exception exp){
//boo=false;
//}
out.print("你猜测的是:"+guessNumber+"<br/>");
if(guessNumber<='z'&&guessNumber>='a'){
if(guessNumber == realnumber){
int n=((Integer)session.getAttribute("count")).intValue();
n=n+1;
session.setAttribute("count", new Integer(n));
mess ="你猜对了,这是第"+n+"次猜测";
}
else if(guessNumber>realnumber){
int n=((Integer)session.getAttribute("count")).intValue();
n=n+1;
session.setAttribute("count", new Integer(n));
mess ="你猜大了,这是第"+n+"次猜测";
}
else if(guessNumber<realnumber){
int n=((Integer)session.getAttribute("count")).intValue();
n=n+1;
session.setAttribute("count", new Integer(n));
mess ="你猜小了,这是第"+n+"次猜测";
}
//else if(number.equals("你还没开始猜测")){
//mess="你还没开始猜测";
//}
jspContext.setAttribute("message", mess);
}
else{
jspContext.setAttribute("message", "请输入你的猜测");
}
%>
下面是截图