题目
编写两个 JSP 页面 inputString.jsp 和 computer.jsp,用户可以使用 inputString.jsp 提供的表单输入一个字符串,并提交给 computer.jsp 页面,该页面通过内置对象获取 inputString.jsp 页面提交的字符串,并显示该字符串的长度。
代码
inputString.jsp
<%@ page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<html>
<body>
<form action="computer.jsp" method="post">
请输入字符串:
<input type="text" name="inputStr">
<input type="submit" value="提交">
</form>
</body>
</html>
computer.jsp
<%@ page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<html>
<body>
<% String inputStr = request.getParameter("inputStr");
out.print("输入的字符串是" + inputStr);
out.print(";长度为" + inputStr.length());
%>
</body>
</html>
执行结果
在 inputString.jsp 输入字符串后,提交到 computer.jsp,在 computer.jsp 中,通过 request.getParameter 来获取输入的值,最后通过 length 方法得到长度。