0
我在JEE中製作了一個小型計算器,並且我獲得了不支持的HTTP 405方法。 我沒有重寫post方法,仍然是一樣的。http 405該方法不受此URL支持
我的代碼:
<%@ 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>
\t <head>
\t \t <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
\t \t <title>Ma calculatrice</title>
\t </head>
\t <body>
\t \t
\t \t <h1>Bienvenue dans ma calculatrice !! </h1>
\t \t <h3>Faîtes ici tous les calculs dont vous rêvez </h3>
\t \t
\t \t <form method = post action=Calculeuse>
\t \t <input type=number name="nbre1" id="nbre1">
\t \t \t <select name="select">
\t \t \t <option value=" + " selected> + </option>
\t \t \t <option value=" - "> - </option>
\t \t \t <option value=" X "> X </option>
\t \t \t <option value="/">/</option>
\t \t \t </select>
\t \t <input type=number name="nbre2" id="nbre2">
\t \t <button type=submit>Calculer</button>
\t \t \t
\t \t </form>
</body>
</html>
,我使用bean:
package com.calculator.bean;
public class Calculeuse {
\t public double nbre1,nbre2, resultat;
\t
\t public String operat;
\t
\t public double getNbre1() {
\t \t return nbre1;
\t }
\t public void setNbre1(double nbre1) {
\t \t this.nbre1 = nbre1;
\t }
\t public double getNbre2() {
\t \t return nbre2;
\t }
\t public void setNbre2(double nbre2) {
\t \t this.nbre2 = nbre2;
\t }
\t public double getResultat() {
\t \t return resultat;
\t }
\t public void setResultat(double resultat) {
\t \t this.resultat = resultat;
\t }
\t public String getOperat() {
\t \t return operat;
\t }
\t public void setOperat(String operat) {
\t \t this.operat = operat;
\t }
\t public double calcul(double nbre1,double nbre2, String operat){
\t \t
\t \t
\t \t if(operat.equals(" + "))
\t \t \t resultat = nbre1 + nbre2;
\t \t
\t \t if(operat.equals(" X "))
\t \t \t resultat = nbre1 * nbre2;
\t \t
\t \t if(operat.equals("/"))
\t \t \t resultat = nbre1/nbre2;
\t \t
\t \t if(operat.equals(" - "))
\t \t \t resultat = nbre1 - nbre2;
\t
\t \t return resultat;
\t \t
\t }
}
並且servlet:
我必須得到類型編號的參數,我想這是我使用doPost的方式mystake,但我無法找到它。
package com.calculator.servlets;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.calculator.bean.Calculeuse;
/**
* Servlet implementation class Calcul
*/
@WebServlet("/Calcul")
public class Calcul extends HttpServlet {
\t private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Calcul() {
super();
// TODO Auto-generated constructor stub
}
\t /**
\t * @param nbre2
\t * @param operat
\t * @param nbre1
\t * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
\t */
\t protected void doGet(HttpServletRequest request, HttpServletResponse response, Object nbre2, Object operat, Object nbre1) throws ServletException, IOException {
\t \t // TODO Auto-generated method stub
\t \t Calculeuse calc= new Calculeuse();
\t \t
\t \t request.setAttribute("nbre2", nbre2);
\t \t request.setAttribute("nbre1", nbre1);
\t \t request.setAttribute("operat", operat);
\t \t
\t calc.setNbre1(Double.parseDouble(request.getParameter("nbre1")));
\t \t calc.setNbre2(Double.parseDouble(request.getParameter("nbre2")));
\t \t calc.setOperat(request.getParameter("operat"));
\t \t
\t \t calc.calcul(calc.getNbre1(), calc.getNbre2(), calc.getOperat());
\t
\t
\t \t System.out.println(calc.getResultat());
\t \t
\t \t
\t \t request.setAttribute("calcul", calc);
\t \t
\t \t this.getServletContext().getRequestDispatcher("/WEB-INF/CalculatriceVue.jsp").forward(request, response);
\t
\t \t
\t }
\t /**
\t * @param nbre2
\t * @param nbre1
\t * @param operat
\t * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
\t */
\t protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
\t \t this.doGet(request, response);
\t }
}