實訓(xùn)內(nèi)容和要求
編寫兩個 JSP 頁面 inputString.jsp 和 computer.jsp,用戶可以使用 inputString.jsp 提供的表單輸入一個字符串,并提交給 computer.jsp 頁面,該頁面通過內(nèi)置對象獲取 inputString.jsp 頁面提交的字符串,并顯示該字符串的長度。
1、inputString.jsp 的代碼如下:
<%@ page contentType="text/html;charset=utf-8" %>
<html>
<body bgcolor=green>
<form action="computer.jsp" method=post name=form>
<input type="text" name="str">
<input type="submit" value="提交" name="submit">
</form>
</body>
</html>
2、computer.jsp 的代碼如下:
<%@ page contentType="text/html;charset=utf-8" %>
<html>
<body>
<%
String textContent=request.getParameter("str");
byte b[]=textContent.getBytes("utf-8");
textContent=new String(b);
%>
字符串:<%=textContent%>的長度:<%=textContent.length()%>
</body>
</html>
程序的運行結(jié)果如圖 1 所示。
a)
b)
圖1 通過內(nèi)置對象獲取并顯示字符串