如何修复HTTP状态500的索引错误?

huangapple go评论74阅读模式
英文:

How do I fix a HTTP Status 500 Index Error?

问题

我正在制作一个应用程序,其中包含两个值,并返回许多不同的函数答案(加法、减法、除法、乘法等)。它使用 JSP 进行制作,我有一个 Index 页面和一个错误的 JSP 页面。当我尝试运行启用了 error.jsp 的应用程序时,无论我尝试什么,它总是返回该页面,因为我的代码中有些问题,但我不明白是什么问题。该应用程序还有一个素数部分,在该部分中,您可以输入一个数字,按下 OK,如果该数字是素数,则不会发生任何情况,但如果不是素数,则会收到错误消息。Next Prime 和 Previous Prime 只会从您输入的数字返回下一个和前一个素数。

以下是您提供的代码翻译后的结果:

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page errorPage="error.jsp"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Home Page</title>
    </head>
    <body>
        <h1>使用 JSP 页面进行计算</h1>
        <jsp:useBean id="math" scope="session" class="beans.MathBean" />
        <jsp:useBean id="primenumber" scope="session" class="beans.PrimeBean" />
        <jsp:setProperty name="math" property="*" />
        <jsp:setProperty name="primenumber" property="prime" />
        <form name="form1" method="post">
            <table>
                <tr>
                    <td>Number 1: </td>
                    <td><input type="text" name="numberA" value="${math.numbera}" style="width: 130px"></td>
                </tr>
                <tr>
                    <td>Number 2: </td>
                    <td><input type="text" name="numberB" value="${math.numberb}" style="width: 130px"></td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align: right"><input type="submit" value="OK"></td>
                </tr>
            </table>
        </form>
        <table>
            <tr>
                <td>Add</td><td>${math.add()}</td>
            </tr>
            <tr>
                <td>Subtract</td><td>${math.subtract()}</td>
            </tr>
            <tr>
                <td>Multiply</td><td>${math.multiply()}</td>
            </tr>
            <tr>
                <td>Divide</td><td>${math.divide()}</td>
            </tr>
        </table>
        <form name="form2" method="post">
            <input type="text" name="prime" value="${primenumber.prime}"/> &nbsp;&nbsp;
            <input type="submit" value="OK"/>
        </form>
        <p><a href="Primes?number=next">下一个素数</a>
        <p><a href="Primes?number=previous">上一个素数</a>
    </body>
</html>

error.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page isErrorPage="true"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>错误页面</title>
    </head>
    <body>
        <h1>错误!</h1>
        <p><a href="index.jsp">返回菜单</a></p>
    </body>
</html>

这是一份关于您提供的 JSP 页面和 JavaBean 代码的翻译。如果您有关于此代码的问题,请随时问我。

英文:

I'm making an application that has two values and it returns a lot of different function answers (addition, subtraction, div, multiplication, etc). It's made using JSP and I have an Index and an error JSPs. When I try to run the app with the error.jsp enabled, it always returns that page no matter what I try, because there's something wrong in my code but I don't understand what. The application also has a prime number section, in which you can enter a number, pressing ok and if the number is a prime nothing happens, but if it isnt then you will get an error message. Next prime and previous prime just returns next and previous prime numbers from the number you entered.

index.jsp

&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@page errorPage=&quot;error.jsp&quot;%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
        &lt;title&gt;Home Page&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;h1&gt;Calculations using JSP pages&lt;/h1&gt;
        &lt;jsp:useBean id=&quot;math&quot; scope=&quot;session&quot; class=&quot;beans.MathBean&quot; /&gt;
        &lt;jsp:useBean id=&quot;primenumber&quot; scope=&quot;session&quot; class=&quot;beans.PrimeBean&quot; /&gt;
        &lt;jsp:setProperty name=&quot;math&quot; property=&quot;*&quot; /&gt;
        &lt;jsp:setProperty name=&quot;primenumber&quot; property=&quot;prime&quot; /&gt;
        &lt;form name=&quot;form1&quot; method=&quot;post&quot;&gt;
            &lt;table&gt;
                &lt;tr&gt;
                    &lt;td&gt;Number 1: &lt;/td&gt;
                    &lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;numberA&quot; value=&quot;${math.numbera}&quot; style=&quot;width: 130px&quot;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                    &lt;td&gt;Number 2: &lt;/td&gt;
                    &lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;numberB&quot; value=&quot;${math.numberb}&quot; style=&quot;width: 130px&quot;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                    &lt;td colspan=&quot;2&quot; style=&quot;text-align: right&quot;&gt;&lt;input type=&quot;submit&quot; value=&quot;OK&quot;&lt;/td&gt;
                &lt;/tr&gt;
            &lt;/table&gt;
        &lt;/form&gt;
        &lt;table&gt;
            &lt;tr&gt;
                &lt;td&gt;Add&lt;/td&gt;&lt;td&gt;${math.add()}&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;Subtract&lt;/td&gt;&lt;td&gt;${math.subtract()}&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;Multiply&lt;/td&gt;&lt;td&gt;${math.multiply()}&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr&gt;
                &lt;td&gt;Divide&lt;/td&gt;&lt;td&gt;${math.divide()}&lt;/td&gt;
            &lt;/tr&gt;
        &lt;/table&gt;
        &lt;form name=&quot;form2&quot; method=&quot;post&quot;&gt;
            &lt;input type=&quot;text&quot; name=&quot;prime&quot; value=&quot;${primenumber.prime}&quot;/&gt; &amp;nbsp;&amp;nbsp;
            &lt;input type=&quot;submit&quot; value=&quot;OK&quot;/&gt;
        &lt;/form&gt;
        &lt;p&gt;&lt;a href=&quot;Primes?number=next&quot;&gt;Next Prime&lt;/a&gt;
        &lt;p&gt;&lt;a href=&quot;Primes?number=previous&quot;&gt;Previous Prime&lt;/a&gt;
    &lt;/body&gt;
&lt;/html&gt;

Error.jsp

&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@page isErrorPage=&quot;true&quot;%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
    &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
        &lt;title&gt;Error page&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;h1&gt;Error!&lt;/h1&gt;
        &lt;p&gt;&lt;a href=&quot;index.jsp&quot;&gt;back 2 menu&lt;/a&gt;&lt;/p&gt;
    &lt;/body&gt;
&lt;/html&gt;

web.xml file is just default with added welcome-file-list, welcome-file with the index.jsp in it.

MathBean:

package beans;
import java.io.Serializable;

public class MathBean implements Serializable{
    
    private long numbera, numberb;

    public MathBean(long numbera, long numberb) {
        this.numbera = numbera;
        this.numberb = numberb;
    }

    public long getNumbera() {
        return numbera;
    }

    public void setNumbera(long numbera) {
        this.numbera = numbera;
    }

    public long getNumberb() {
        return numberb;
    }
    public void setNumberb(long numberb) {
        this.numberb = numberb;
    }
    public long add() {
        return numbera + numberb;
    }
    public long subtract() {
        return numbera - numberb;
    }
    public long divide() {
        if (numberb == 0) {
            return 0;
        }
        return numbera / numberb; 
    }
    public long multiply() {
        return numbera * numberb;
    }
    
}

PrimeBean:


package beans;

public class PrimeBean {
    private static final long max = 9223372036854775783L;
    private long prime = 2;
    
    public PrimeBean() {
        
    }

    public long getPrime() {
        return prime;
    }

    public void setPrime(long p) throws Exception {
        if(!isPrime(p)) throw new Exception(&quot;Illegal number&quot;);
        prime = p;
    }
    private static boolean isPrime(long p) {
        if (p == 2 || p == 3 || p == 5 || p == 7) return true;
        if(p&lt;11||p%2 == 0 ) return false;
        for(long t = 3, m = (long)Math.sqrt(p) + 1; t &lt;= m; t+=2) if(p % t == 0) return false;
        return true;
    } 
   public boolean next() {
       if (prime &lt; max) {
           if (prime == 2) prime = 3;
           else for(prime += 2; !isPrime(prime); prime += 2);
           return true;
       }
       return false;
   }
   public boolean previous() {
       if (prime &gt; 2) {
           if(prime == 3) prime = 2;
           else for(prime -= 2; !isPrime(prime); prime -= 2);
           return true;
       }
       return false;
   }
    
}

PrimeServlet:


package servlets;

import beans.PrimeBean;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = &quot;Primes&quot;, urlPatterns = {&quot;/Primes&quot;})
public class PrimeServlet extends HttpServlet {


    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException{
         PrimeBean bean = (PrimeBean) request.getSession().getAttribute(&quot;primenumber&quot;);
         String number = request.getParameter(&quot;number&quot;);
         if (number.equals(&quot;next&quot;)) bean.next(); else bean.previous();
         response.sendRedirect(&quot;index.jsp&quot;);
        }

    // &lt;editor-fold defaultstate=&quot;collapsed&quot; desc=&quot;HttpServlet methods. Click on the + sign on the left to edit the code.&quot;&gt;
    /**
     * Handles the HTTP &lt;code&gt;GET&lt;/code&gt; method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP &lt;code&gt;POST&lt;/code&gt; method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return &quot;Short description&quot;;
    }// &lt;/editor-fold&gt;

}

Sorry for posting all the code, but I'm truly lost and I don't understand why it doesn't work. All help appreciated.

Thank you!

答案1

得分: 0

以下是翻译好的部分:

至少有两个问题在实现中:

  1. Bean必须具有公共的默认构造函数,且参数:

    public MathBean() { ...
    

    您的代码中是:

    public MathBean(long numbera, long numberb) { ...
    
  2. index.jsp上,HTML输入标签numberAnumberBsubmit缺少闭合的&gt;

英文:

There are at least 2 problems in the implementation:

  1. Beans must have public default constructor with no arguments:

    public MathBean() { ...
    

    You have

    public MathBean(long numbera, long numberb) { ...
    
  2. HTML input tags numberA, numberB and submit on index.jsp are missing closing &gt;.

huangapple
  • 本文由 发表于 2020年9月22日 03:58:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63999181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定