温度转换程序
创建一个HTML文件,要求用户输入待转换的摄氏温度,通过按钮提交请求一个JSP页面,返回对应的华氏温度。
其中:华氏温度 = 1.8*摄氏温度 + 32。
HTML显示效果为:
HTML提示代码:
JSP提示代码:
操作说明:
在“VoteCounter”项目的“WebContent”文件夹上点击右键,选择“New-HTML File”新建HTML文件
在弹出的对话框中,设置文件名为“tempconvert0.html”,点击“Finish”按钮
修改生成的“tempconvert0.html”文件来显示文本框及按钮。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="tempconvert1.jsp" method="get">
摄氏温度:
<input type="text" name="ctemp" />
<input type="submit" value="转换为华氏温度" />
</form>>
</body>
</html>
在“VoteCounter”项目的“WebContent”文件夹上点击右键,选择“New-JSP File”
在弹出的对话框中,设置文件名为“tempconvert1.jsp”,点击“Finish”按钮
修改JSP页面内容,以响应HTML页面的请求
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String strCtemp=request.getParameter("ctemp");
float ftemp=1.8f*Integer.parseInt(strCtemp)+32.0f;
%>
摄氏温度: <%=strCtemp %>
<br/>
<br/>
华氏温度: <%=ftemp %>
</body>
</html>
重启tomcat
在IE地址栏中敲入URL“http://localhost:8080/VoteCounter/tempconvert0.html”,得到页面