zoukankan      html  css  js  c++  java
  • JSP第五次作业:使用servlet计算相应图形的周长和面积

    题目:
    页面1:显示如下信息
    下拉列表:
    1.计算三角形
    2.计算矩形
    3.计算梯形
    按钮:计算(submit按钮,提交到页面2)
    页面2:根据选项1,2,3
    分别收集三角形3条边、矩形2条边、梯形4条边和高的信息
    提交按钮(提交给serlet)
    servlet:
    根据选项和提交的数据,计算相应图形的周长和面积
    显示:xxx形,它的边长和高,分别是,周长是,面积是

    因为各浏览器编码不同,与servlet编码冲突。为防止出现乱码,所以在这里表达全用了英语。

    两个jsp文件由于习惯性的放进了一个作业文件夹里,所以在form表单向servlet传参时用到了 ../  然后~~~,servlet真好用,比上次的javabean更方便

    页面1:frist.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
          <form name="f1" method="post" action="second.jsp">
               下拉列表:
               <select name="se1">
                  <option value ="sanjiao">计算三角形</option>
                  <option value ="juxing">计算矩形</option>
                  <option value ="tixing">计算梯形</option>
               </select>
               <input type="submit" name="s1" value="计算">
          </form>
    </body>
    </html>
    

      

    页面2:second.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <%@ page import="java.util.*"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<%
    		request.setCharacterEncoding("utf-8");
    		String type;
    		type = request.getParameter("se1");
    		if (type.equals("sanjiao")) {
    	%>
    	<form name="f1" method="post" action="../solve">
    		请输入三角形的三条边:<br> 
    		<input type="text" name="b1"><br> 
    		<input type="text" name="b2"><br> 
    		<input type="text" name="b3">
    		<input type="submit" name="s1" value="提交" />
    	</form>
    	<%
    		} else if (type.equals("juxing")) {
    	%>
    	<form name="f1" method="post" action="../solve">
    		请输入矩形的长和宽: <br> 
    		<input type="text" name="b1"><br>
    		<input	type="text" name="b2">
    		<input type="submit" name="s1" value="提交" />
    	</form>
    	<%
    		} else if (type.equals("tixing")) {
    	%>
    	<form name="f1" method="post" action="../solve">
    		请输入梯形的四条边和高:<br> 
    		上底 :<input type="text" name="b1"><br>
    		下底 :<input type="text" name="b2"><br>
    		斜边 :<input type="text" name="b3"><br>
    		斜边 :<input type="text" name="b4"><br> 
    		高     :<input type="text" name="h"> 
    		<input type="submit" name="s1" value="提交" />
    	<%
    		}
    	%>
    	
    </body>
    </html>
    

      

    servlet:solve.java

    package anyi;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.lang.Math;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class CalculateMulti2
     */
    @WebServlet("/solve")
    public class solve extends HttpServlet {
    	private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public solve() {
            super();
            // TODO Auto-generated constructor stub
        }
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		response.setCharacterEncoding("utf-8");
    		double b1 = 0, b2 = 0, b3 = 0, b4 = 0, h = 0;
    		b1 = Integer.parseInt(request.getParameter("b1"));
    		b2 = Integer.parseInt(request.getParameter("b2"));
    		try{
    			b3 = Integer.parseInt(request.getParameter("b3"));
    					} catch (Exception e) {
    					}
    		try{
    			b4 = Integer.parseInt(request.getParameter("b4"));
    			h = Integer.parseInt(request.getParameter("h"));
    					} catch (Exception e) {
    					}
    		PrintWriter pw = response.getWriter();
    		pw.print("<html><body><br>");
    		pw.print("My name is solve, which is a servlet object.<br><br>");
    		pw.print("I am responsible for calculating the perimeter and area of the graphics you provided:<br><br>");
    		if (b3 == 0) {
    			pw.print("rectangle		 The side lengths are "+b1+" and "+b2);
    			pw.print("<br><br> perimeter:" + (b1 + b2) * 2 + "	   area:" + (b1 * b2));
    		} else if (b4 == 0) {
    			pw.print("triangle		 The side lengths are "+b1+" and "+b2+" and "+b3);
    			double p=(b1+b2+b3)/2;
    			double s=Math.sqrt(p*(p-b1)*(p-b2)*(p-b3));
    			pw.print("<br><br> perimeter:" + (b1 + b2 + b3) + "	   area:" + s);
    		}else {
    			pw.print("trapezoid		 The side lengths are "+b1+" and "+b2+" and "+b3+" and "+ b4+". height is "+h);
    			pw.print("<br><br> perimeter:" + (b1 + b2 + b3 + b4) + "	   area:" + (b1 + b2)*h);
    		}
    		pw.print("</body></html>");		
    	}
    
    }
    

      就问题而言,由于页面2传参数量不确定,但又差的个数不多,所以这里分别用了两个try-catch接收可能要接收的值,避免500。其他好像没什么可说的。快期末了要好好学习呀~!

  • 相关阅读:
    java 递归函数
    iOS安全攻防(三):使用Reveal分析他人app
    mapreduce任务失败、重试、猜測式运行机制小结
    javascript UniqueID属性
    弧度和角度的转换
    批处理批量创建90个用户
    【设计模式】状态模式
    【.NET进程通信】初探.NET中进程间通信的简单的实现
    天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为,所以动心忍性,增益其所不能
    冷门却使用的 javascript 技巧
  • 原文地址:https://www.cnblogs.com/thx2199/p/14815034.html
Copyright © 2011-2022 走看看