大家好,又见面了,我是你们的朋友全栈君。
inputString.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>习题3.6</title>
</head>
<body>
<font size="5">
<form action="computer.jsp" method="post" name="form">
<p>输入信息:
<input type="text" name="message">
<br> <input type="submit" value="提交信息" name="submit">
</p>
</form>
</font>
</body>
</html>
computer.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<body>
获取文本框提交的信息:
<%
String str=null;
str=request.getParameter("message");
%>
<%= str%>
<br>获取信息长度:
<%= str.length()%>
</body>
</html>
运行结果如下:
xiti4_6.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>猜英文26个小写字母的web游戏</title>
</head>
<body>
<font size="2"></font>
猜英文26个小写字母:
<%
int num = (int)(Math.random()*25+97);//a-z 的ascll码值是[97,122]
String word = String.valueOf((char) num);
session.setAttribute("count",new Integer(0));
session.setAttribute("save",new String(word));
%>
<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>
<html>
<body><font size="2">
<%
String str=request.getParameter("guessNumber");
if(str==null)
str="*";
if(str.length()==0)
str="*";
%>
<guessNumber:GuessTag number="<%= str %>" />
当前猜测结果:<%= message %>
<% if(message.startsWith("你猜对了")){%>
<br><a href="xiti4_6.jsp">重新获取随机数</a>
<% }else{%>
<br>输入你的猜测:
<form action="" method="post" name=form>
<input type="text" name="guessNumber">
<input type="submit" value="提交" name="submit">
</form>
<%} %>
</font>
</body>
</html>
GuessTag.tag代码如下:
<%@ tag pageEncoding="utf-8"%>
<%@ attribute name="number" required="true"%>
<%@ variable name-given="message" scope="AT_END"%>
<% String mess="";
String realnumber=(String)session.getAttribute("save");//随机数save
String guessNumber = number;
if(guessNumber.equals("*")){
mess="开始你的猜测!";
}else if(guessNumber.equals(realnumber)){
int n=((Integer)session.getAttribute("count")).intValue();
n=n+1;
session.setAttribute("count",new Integer(n));
mess="你猜对了,这是第"+n+"次猜测";
}else{
int n=((Integer)session.getAttribute("count")).intValue();
n=n+1;
session.setAttribute("count",new Integer(n));
mess="你猜错了,这是第"+n+"次猜测";
}
jspContext.setAttribute("message",mess);
%>
运行结果如下:
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/149876.html原文链接:https://javaforall.cn