How do I fix a HTTP Status 500 Index Error?

dumbloser

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

<%@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>Calculations using JSP pages</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">Next Prime</a>
        <p><a href="Primes?number=previous">Previous Prime</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>Error page</title>
    </head>
    <body>
        <h1>Error!</h1>
        <p><a href="index.jsp">back 2 menu</a></p>
    </body>
</html>

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("Illegal number");
        prime = p;
    }
    private static boolean isPrime(long p) {
        if (p == 2 || p == 3 || p == 5 || p == 7) return true;
        if(p<11||p%2 == 0 ) return false;
        for(long t = 3, m = (long)Math.sqrt(p) + 1; t <= m; t+=2) if(p % t == 0) return false;
        return true;
    } 
   public boolean next() {
       if (prime < max) {
           if (prime == 2) prime = 3;
           else for(prime += 2; !isPrime(prime); prime += 2);
           return true;
       }
       return false;
   }
   public boolean previous() {
       if (prime > 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 = "Primes", urlPatterns = {"/Primes"})
public class PrimeServlet extends HttpServlet {


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

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> 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 <code>POST</code> 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 "Short description";
    }// </editor-fold>

}

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!

haba713

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 >.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to fix HTTP error fetching URL. Status=500 in java while crawling?

From Dev

How do I fix this index out of bound error

From Dev

How do I fix a HTTP Error 400: Bad Request?

From Dev

How can i resolve the "HTTP Status 500 - Internal Server Error" in my web service app on glassfish?

From Dev

How do I fix a "Problem with MergeList" or "status file could not be parsed" error when trying to do an update?

From Dev

How do i fix 'Error:Opening the cache...' or 'The package list or status file could not be parsed'?

From Dev

HTTP Status 424 or 500 for error on external dependency

From Dev

Spring Resfull error HTTP Status 500

From Dev

HTTP Status 500 error in Openbravo ERP 3

From Dev

HTTP Status 500 Error instantiating servlet class

From Dev

HTTP 500 error failed with 200 status code

From Dev

GlassFish Http Status 500 - Internal Server Error

From Dev

Error instantiating servlet class - HTTP Status 500

From Dev

Spring Resfull error HTTP Status 500

From Dev

How do I fix this string index out of bounds error in my word game?

From Java

What is a NoReverseMatch error, and how do I fix it?

From Dev

How do I fix this Eclipse error?

From Dev

How do I fix this rstrip attribute error?

From Dev

How do I fix a dimension error in TensorFlow?

From Dev

How do I fix this ajaxToolkit error?

From Dev

How do I fix a transaction error?

From Dev

How do I fix this internet connection error?

From Dev

How do i fix the expected identifier error?

From Dev

How do I fix this Eclipse error?

From Dev

How do I fix this data type error?

From Dev

How do I fix this error installing Laravel?

From Dev

How do I fix unexpected token error?

From Dev

How do I fix it 'error: undefined' in javascript?

From Dev

How do I use a validator in Spring? I encountered a 500 error

Related Related

  1. 1

    how to fix HTTP error fetching URL. Status=500 in java while crawling?

  2. 2

    How do I fix this index out of bound error

  3. 3

    How do I fix a HTTP Error 400: Bad Request?

  4. 4

    How can i resolve the "HTTP Status 500 - Internal Server Error" in my web service app on glassfish?

  5. 5

    How do I fix a "Problem with MergeList" or "status file could not be parsed" error when trying to do an update?

  6. 6

    How do i fix 'Error:Opening the cache...' or 'The package list or status file could not be parsed'?

  7. 7

    HTTP Status 424 or 500 for error on external dependency

  8. 8

    Spring Resfull error HTTP Status 500

  9. 9

    HTTP Status 500 error in Openbravo ERP 3

  10. 10

    HTTP Status 500 Error instantiating servlet class

  11. 11

    HTTP 500 error failed with 200 status code

  12. 12

    GlassFish Http Status 500 - Internal Server Error

  13. 13

    Error instantiating servlet class - HTTP Status 500

  14. 14

    Spring Resfull error HTTP Status 500

  15. 15

    How do I fix this string index out of bounds error in my word game?

  16. 16

    What is a NoReverseMatch error, and how do I fix it?

  17. 17

    How do I fix this Eclipse error?

  18. 18

    How do I fix this rstrip attribute error?

  19. 19

    How do I fix a dimension error in TensorFlow?

  20. 20

    How do I fix this ajaxToolkit error?

  21. 21

    How do I fix a transaction error?

  22. 22

    How do I fix this internet connection error?

  23. 23

    How do i fix the expected identifier error?

  24. 24

    How do I fix this Eclipse error?

  25. 25

    How do I fix this data type error?

  26. 26

    How do I fix this error installing Laravel?

  27. 27

    How do I fix unexpected token error?

  28. 28

    How do I fix it 'error: undefined' in javascript?

  29. 29

    How do I use a validator in Spring? I encountered a 500 error

HotTag

Archive