jspservletshttp-status-code-500

Compilation error in JSP: req cannot be resolved


I created a simple JSP/Servlet project for calculating the monthly payment. However, when I make an HTTP request on my localhost, I receive the following error:

HTTP Status 500 – Internal Server Error
Type Exception Report

message Unable to compile class for JSP:

description The server encountered an internal error that prevented it from fulfilling the request.

Exception:

org.apache.jasper.JasperException: Impossible de compiler la classe pour la JSP: 

Une erreur s'est produite à la ligne : [3] dans le fichier jsp : [/ServletJsp.jsp]
req cannot be resolved
1:  <%@ page import="web.CreditModel"%>
2:  <%
3:   CreditModel model = (CreditModel) req.getAttribute("creditModel");
4: 
5:  %>
6: <!DOCTYPE html>


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:214)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:531)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:398)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:368)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:352)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:603)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:399)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:379)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:327)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:623)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
    web.ControllerServlet.doGet(ControllerServlet.java:25)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:529)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:623)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
note The complete trace of the root cause of this error is available in the log files of this server.

This is the code of the servlet that calls:

ControllerServlet.java:

package web;

import metier.CreditMetier;
import metier.CreditMetierImpl;

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 java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/hi")
public class ControllerServlet extends HttpServlet {
    private CreditMetier metier;
    @Override
    public void init() throws ServletException {
        metier = new CreditMetierImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setAttribute("creditModel", new CreditModel());
        req.getRequestDispatcher("ServletJsp.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        double montant = Double.parseDouble(req.getParameter("montant"));
        double taux = Double.parseDouble(req.getParameter("taux"));
        int duree = Integer.parseInt(req.getParameter("duree"));

        CreditModel model = new CreditModel();
        model.setMontant(montant);
        model.setTaux(taux);
        model.setDuree(duree);

        double result = metier.calculerMensualiterCredit(montant,taux,duree);
        model.setResultat(result);
        req.setAttribute("creditModel", model);

        req.getRequestDispatcher("ServletJsp.jsp").forward(req,resp);

    }
}

and this is the code of the JSP that calls:

ServletJsp.jsp:

 <%@ page import="web.CreditModel"%>
 <%
  CreditModel model = (CreditModel) req.getAttribute("creditModel");

 %>
<!DOCTYPE html>
<html>
<head>
<title> Crédit bancaire </title>
<meta charset = "utf-8">
<link rel= "stylesheet" type="text/css" href="css/style.css"/>
</head>
<body>
  <div>


    <form action="hi" method="post">
    <table>
      <tr>
        <td> Montant:</td>
        <td><input type="text" name="montant"/></td>
        <td>DH</td>
      </tr>

      <tr>
        <td> Taux:</td>
        <td><input type="text" name="taux"/></td>
        <td>%</td>
      </tr>

      <tr>
        <td> Duree:</td>
        <td><input type="text" name="duree"/></td>
        <td>mois</td>
      </tr>
    </table>
    <button type="submit">Calculer</button>
    </form>
  </div>
  <div>
   <p> Mensualite = <%=model.getResultat() %> </p>
  </div>
</body>
</html>

this is the code of the metier that calls

CreditMetierImpl.java:

package metier;

public class CreditMetierImpl implements CreditMetier{
    @Override
    public double calculerMensualiterCredit(double capital, double taux, int duree) {
        double t = taux/100;
        double t1 = capital* t/12;
        double t2 = 1 - Math.pow(1 +t/12, -duree);
        return t1/t2;
    }
}

and the code of model that calls

CreditModel.java:

package web;

public class CreditModel {
    private double montant;
     private double taux;
     private int duree;
     private double mensualite;
     private double resultat;

    public double getMontant() {
        return montant;
    }

    public void setMontant(double montant) {
        this.montant = montant;
    }

    public double getTaux() {
        return taux;
    }

    public void setTaux(double taux) {
        this.taux = taux;
    }

    public int getDuree() {
        return duree;
    }

    public void setDuree(int duree) {
        this.duree = duree;
    }

    public double getMensualite() {
        return mensualite;
    }

    public void setMensualite(double mensualite) {
        this.mensualite = mensualite;
    }

    public double getResultat() {
        return resultat;
    }

    public void setResultat(double resultat) {
        this.resultat = resultat;
    }
}

I tried the HTTP request with get but it's not working.


Solution

  • You've got a compilation error

    req cannot be resolved

    because req is a method's local parameter. When a method doGet() ends then it won't be available anywhere in the code, including a JSP, which is compiled to another servlet.

    You use scriptlets in the JSP. They are frequently written on Java and a code can be moved to a servlet for further references with EL expressions.

    So, you need to remove a scriptlet code from the head of JSP. And this code can be replaced

    <p> Mensualite = <%=model.getResultat() %> </p>  
    

    With

    <p> Mensualite = ${model.resultat}  </p>