英文:
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}"/>
<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
<%@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!
答案1
得分: 0
以下是翻译好的部分:
至少有两个问题在实现中:
-
Bean必须具有公共的默认构造函数,且无参数:
public MathBean() { ...
您的代码中是:
public MathBean(long numbera, long numberb) { ...
-
在
index.jsp
上,HTML输入标签numberA
、numberB
和submit
缺少闭合的>
。
英文:
There are at least 2 problems in the implementation:
-
Beans must have public default constructor with no arguments:
public MathBean() { ...
You have
public MathBean(long numbera, long numberb) { ...
-
HTML input tags
numberA
,numberB
andsubmit
onindex.jsp
are missing closing>
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论