实验一:
编写两个JSP页面inputString.jsp和computer.jsp,用户可以使用inputString.jsp 提供表单的输入一个字符串,并提交给computer.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=ISO-8859-1">
<title>习题4</title>
</head>
<body>
<FORM action="computer.jsp" method=post name=form>
<BR>请输入字符串:<INPUT type="text" name="string" value="">
<INPUT TYPE="submit" value="提交" name="submit">
<INPUT TYPE="reset" value="重置" >
</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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% String yourString=request.getParameter("string"); %>
<P> 您输入的字符串是:<%=yourString %></P>
<p>字符串的长度是:<%=yourString.length() %></p>
<a href = "inputString.jsp">return</a>
</body>
</html>
运行结果
实验二:JSP简单练习-猜字母游戏。这个游戏非常特殊。为保证玩家既能适当放松有能避免沉迷游戏,所以这款游戏的gameover设置很特别
ex6.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
session.setAttribute("meaasge", "猜猜字母蛙");
char a[]=new char[26];
int m= 0;
for(char c='a'; c<='z'; c++)
{
a[m]=c;
m++;
}//把26个小写字母写进数组a[]中
int randomIndex = (int)(Math.random()*a.length);
char ch = a[randomIndex];
session.setAttribute("SaveLetter", new Character(ch));
session.setAttribute("count", new Integer(0));
%><!-- 获取随机数 -->
<form action="guess.jsp" method="post" name=form>
<p>输入你的猜测:</p>
<input type="text" name="guesschar">
<input type="submit" value="提交" name="submit">
</form>
</body>
</html>
guess.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String tempString=request.getParameter("guesschar");
String TempCharacter=session.getAttribute("SaveLetter").toString();
if(tempString!=null)
{
if(TempCharacter.equalsIgnoreCase(tempString))
out.println("恭喜您,您猜对了!");
else
out.println("您猜错了,加油哦!");
}
%>
<BR>
<P>输入您所猜的字母:
<FORM action="guessResultExample1.jsp" method="post" name=form>
<INPUT type="text" name="guesschar" >
<INPUT TYPE="submit" value="提交" name="submit">
</FORM>
<p>Game over</p>
</body>
</html>