JSP作業:1.計算字符串長度; 2.猜英文26個小寫字母的web遊戲

計算字符串長度

一、內容


       編寫兩個JSP頁面inputString.jsp和computer.jsp,用戶可以使用inputString.jsp提供表單的輸入一個字符串,並提交給computer.jsp頁面,該頁面通過內置對象獲取inputString.jsp頁面提交的字符串,計算並顯示該字符串的長度。

二、實現

inputString.jsp

<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>在此處插入標題</title>
</head>
<body>
<font size=5>
<form action="computer.jsp" method="post" name="form">
請輸入一個字符串
<input type="text" name="str">
<input type="submit" value="提交併計算長度">
</form>
</body>
</html

 

 

computer.jsp

<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>在此處插入標題</title>
</head>
<body>
<font size=5>
<%String str=request.getParameter("str");
	byte b[]=str.getBytes("ISO-8859-1");
	str=new String(b);
%>
字符串長度:<%=str.length() %>
</body>
</html>

 

 

三、效果展示

 

 

猜英文26個小寫字母的web遊戲

一、內容

編寫一個猜英文26個小寫字母的Web遊戲。當用戶訪問example4-18.jsp頁面時,服務器隨機分配一個a-z間的字母給用戶,用戶點擊該頁面的a連接跳轉至guess.jsp頁面進行猜測,guess.jsp請求Guess.tag判斷用戶猜測是否正確。

 

二、實現

 

example4-18.jsp

<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>在此處插入標題</title>
</head>
<body>
隨機分配一個小寫的英文字母,請猜猜看!
<%
int letter=(int)(Math.random()*26)+97;
session.setAttribute("count",new Integer(0));
session.setAttribute("save",new Integer(letter));
%>
<br><a href="guess.jsp">去猜猜這個數</a>
</body>
</html>

 

guess.jsp

 

<%@ page contentType="text/html;charset=utf-8"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="guessLetter"%>
<html>
<body>
<%String str=request.getParameter("guessLetter");
if(str==null){
	str="*";
}
if(str.length()==0){
	str="*";
}
%>
<guessLetter:GuessTag letter="<%=str %>"/>
當前猜測結果:<%=message %>
<%if(message.startsWith("你猜對了!")){
%>	<br><a href="example4-18.jsp">重新獲得隨機字母</a>
<%}
else{
%> <br> 輸入你的猜測:
<form action="" method="post" name="form">
<input type="text" name="guessLetter">
<input type="submit" value="送出" name="submit">
</form>
<%}
%>
</body>
</html>

Guess.tag

 


<%@ tag pageEncoding='UTF-8'%>
<%@ attribute name="letter" required="true" %>
<%@ variable name-given="message" scope="AT_END"%>
<% String mess="";
	Integer integer=(Integer)session.getAttribute("save");  //隨機生成的數
	int realletter=integer.intValue();
	int guessLetter=0;
	boolean boo=true;
	try{
		char ch[] = letter.toCharArray();
		byte b=(byte)ch[0];
		guessLetter=b;
	}
	catch(Exception exp){
		boo=false;
	}
	if(boo){
		if(guessLetter==realletter){
			int n=((Integer)session.getAttribute("count")).intValue();
			n=n+1;
			session.setAttribute("count",new Integer(n));
			mess="你猜對了!這是第"+n+"次猜測";
		}
		else if(guessLetter>realletter){
			int n=((Integer)session.getAttribute("count")).intValue();
			n=n+1;
			session.setAttribute("count",new Integer(n));
			mess="你猜大了!這是第"+n+"次猜測";
		}
		else if(guessLetter<realletter){
			int n=((Integer)session.getAttribute("count")).intValue();
			n=n+1;
			session.setAttribute("count",new Integer(n));
			mess="你猜小了!這是第"+n+"次猜測";
		}
		else if(letter.equals("你還沒開始猜")){
			mess="你還沒開始猜";
		}
		jspContext.setAttribute("message",mess);
	}
	else{
		jspContext.setAttribute("message","請輸入你的猜測");
	}
%>

 

三、效果展示

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章