jsp Session应用

本文介绍了两个JSP实验:一是通过inputString.jsp和computer.jsp实现用户输入字符串并计算长度的功能;二是展示了使用ex6.jsp和guess.jsp构建的猜字母游戏,探讨了JSP在游戏交互中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实验一:

编写两个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>

运行效果

<br>JSP Login.jsp <br><br><br><br><%@ page contentType="text/html;charset=GB2312" %><br><br><html><br><head><br><title>CH5 - Login.jsp</title><br></head><br><body><br><br><h2>javax.servlet.http.HttpSession - session 对象</h2> <br><form action=Login.jsp method="POST" ><br>Login Name: <input type="text" name="Name"><br><br>Login Password: <input type="text" name="Password" ><br><br><input type="submit" value="Send"><br><br><form><br><br><% if (request.getParameter("Name") != null &&<br> request.getParameter("Password") != null) { <br>String Name = request.getParameter("Name");<br>String Password = request.getParameter("Password");<br><br>if (Name.equals("mike") && Password.equals("1234")) { <br>session.setAttribute("Login", "OK");<br>response.sendRedirect("Member.jsp");<br>}<br>else { <br>out.println("登录错误,请输入正确名称"); <br>} <br>}<br>%><br><br></body><br></html> <br><br><br>JSP Member.jsp <br><br><br><br><%@ page contentType="text/html;charset=GB2312" %><br><br><html><br><head><br><title>CH5 - Member.jsp</title><br></head><br><body><br><br><h2>javax.servlet.http.HttpSession - session 对象</h2> <br><% <br>String Login = (String)session.getAttribute("Login");<br><br>if (Login != null && Login.equals("OK")) { <br>out.println("欢迎进入");<br>session.invalidate(); <br>} <br>else { <br>out.println("请先登录,谢谢") ;<br>out.println("<br>经过五秒之后,网页会自动返回Login.jsp");<br><br>response.setHeader("Refresh","5;URL=Login.jsp"); <br>}<br>%><br><br></body><br></html> <br>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值