我创建了一个简单的JSP/Servlet项目来计算每月付款。然而,当我在localhost,
上发出HTTP请求时,我收到以下错误:
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.
字符串
这是调用的servlet的代码:
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);
}
}
型
这是JSP的代码,它调用:
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>
型
这是一个代码的metier调用
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;
}
}
型
模型的代码调用
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;
}
}
型
我尝试了HTTP请求与get,但它不工作。
1条答案
按热度按时间jdzmm42g1#
你有一个编译错误
无法解析请求
因为
req
是一个方法的本地参数。当一个方法doGet()
结束时,它将在代码中的任何地方都不可用,包括一个被编译到另一个servlet的JSP。在JSP中使用scriptlets。它们在stackoverflow.com上被高度讨论,因为它们经常是用Java编写的,代码可以移动到servlet中,以便用EL表达式进行进一步引用。
因此,您需要从JSP的头部删除一个scriptlet代码。
字符串
与
型