Issue
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}"/>
<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!
Solution
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>
.
Answered By - haba713
Answer Checked By - David Marino (JavaFixing Volunteer)