one.jsp

<%@ page language="java" import="java.util.*" import="java.text.*" pageEncoding="GB2312"%>
<%@taglib tagdir="/WEB-INF/tags" prefix="compute" %>
<html>
  <body>
	<h4>以下是三角形的边长,面积最多保留三位小数</h4>
	<form>
		输入边长a:<input type="text" name="a">
		<br>
		输入边长b:<input type="text" name="b">
		<br>
		输入边长c:<input type="text" name="c">
		<br>
		<input type="submit" value="提交" name="submit">
	</form>
	
	<% 
		String a=request.getParameter("a");
		String b=request.getParameter("b");
		String c=request.getParameter("c");
		if(a==null||b==null||c==null){
		a="0";
		b="0";
		c="0";
		}
		if(a.length()>0&&b.length()>0&&c.length()>0)
 	%>		
 		<compute:GetArea sideA="<%= a%>" sideB="<%= b%>" sideC="<%= c%>" />
	
	<%
		NumberFormat f=NumberFormat.getInstance();//得到默认的数字格式化显示
		f.setMaximumFractionDigits(3);//最多保留3位小数
		double result=area.doubleValue();
		String str=f.format(result);//格式化之后的数据
		out.println(str);
	%>
  </body>
</html>

two.jsp

<%@ page language="java" import="java.util.*" import="java.text.*" pageEncoding="GB2312"%>
<%@taglib tagdir="/WEB-INF/tags" prefix="compute" %>
<html>
  <body>
	<h4>以下是三角形的边长,面积最多保留六位小数</h4>
	<form>
		输入边长a:<input type="text" name="a">
		<br>
		输入边长b:<input type="text" name="b">
		<br>
		输入边长c:<input type="text" name="c">
		<br>
		<input type="submit" value="提交" name="submit">
	</form>
	
	<% 
		String a=request.getParameter("a");
		String b=request.getParameter("b");
		String c=request.getParameter("c");
		if(a==null||b==null||c==null){
		a="0";
		b="0";
		c="0";
		}
		if(a.length()>0&&b.length()>0&&c.length()>0)
 	%>		
 		<compute:GetArea sideA="<%= a%>" sideB="<%= b%>" sideC="<%= c%>" />
	
	<%
		NumberFormat f=NumberFormat.getInstance();
		f.setMaximumFractionDigits(6);//设置最多保留6位小数
		double result=area.doubleValue();
		String str=f.format(result);
		out.println(str);
	%>
  </body>
</html>
 

GetArea.tag

<%@ attribute name="sideA" required="true"%>
	<%@ attribute name="sideB" required="true"%>
	<%@ attribute name="sideC" required="true"%>
	<%@ variable name-given="area" variable-class="java.lang.Double" scope="AT_END"%>
	
	<%
		double a=Double.parseDouble(sideA);
		double b=Double.parseDouble(sideB);
		double c=Double.parseDouble(sideC);
		if(a+b>c&&a+c>b&&b+c>a){
			double p=(a+b+c)/2.0;
			double result=Math.sqrt(p*(p-a)*(p-b)*(p-c));
			jspContext.setAttribute("area",new Double(result));
		}
		else{
		jspContext.setAttribute("area",new Double(-1));
	}
%>

结果显示

一、关于one.jsp的结果

 1.原始界面

JSP作业 习题3-7

 2.输入数据: 

JSP作业 习题3-7

3.最终结果显示  

JSP作业 习题3-7

二、关于two.jsp的结果显示

 1.原始界面

JSP作业 习题3-7

 2.输入数据:

JSP作业 习题3-7

3.最终结果显示 

JSP作业 习题3-7

 

相关文章:

  • 2021-08-29
  • 2021-12-05
  • 2021-06-03
  • 2021-12-24
  • 2021-12-12
  • 2021-08-27
  • 2021-09-07
猜你喜欢
  • 2021-11-04
  • 2022-03-11
  • 2022-01-14
  • 2021-12-09
  • 2022-12-23
  • 2021-10-24
  • 2022-01-11
相关资源
相似解决方案