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\t\t The side lengths are "+b1+" and "+b2);
			pw.print("<br><br> perimeter:" + (b1 + b2) * 2 + "\t   area:" + (b1 * b2));
		} else if (b4 == 0) {
			pw.print("triangle\t\t 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) + "\t   area:" + s);
		}else {
			pw.print("trapezoid\t\t The side lengths are "+b1+" and "+b2+" and "+b3+" and "+ b4+". height is "+h);
			pw.print("<br><br> perimeter:" + (b1 + b2 + b3 + b4) + "\t   area:" + (b1 + b2)*h);
		}
		pw.print("</body></html>");		
	}

}

  就問題而言,由於頁面2傳參數量不確定,但又差的個數不多,所以這里分別用了兩個try-catch接收可能要接收的值,避免500。其他好像沒什么可說的。快期末了要好好學習呀~!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM