當我運行我的應用程序,我有一個HTTP錯誤:HTTP 400和500錯誤信息
HTTP Status 404 - /ProjectName/
這是我的項目的URL:
localhost:8080/Initiation1/
當我鍵入此字符串到地址欄:
localhost:8080/Initiation1/CarteIdentite
,我在此HTTP 500錯誤消息:
javax.servlet.ServletException: Error instantiating servlet class ca.web.CarteIdentite
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
root cause
java.lang.ClassNotFoundException: ca.web.CarteIdentite
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
這是我的servlet代碼:
package ca.web;
import java.io.IOException;
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;
import javax.servlet.ServletConfig;
/**
* Servlet implementation class CarteIdentite
*/
@WebServlet("/CarteIdentite")
public class CarteIdentite extends HttpServlet
{
// paramètres d’instance
private String defaultNom = null;
private String defaultAge = null;
private String defaultCourriel = null;
//init
public void init()
{
// on récupère les paramètres d’initialisation de la servlet
ServletConfig config = getServletConfig();
defaultNom = config.getInitParameter("defaultNom");
if(defaultNom==null)
{
defaultNom="XXXXXXXXXXXXXXXX";
}
defaultAge = config.getInitParameter("defaultAge");
if(defaultAge==null)
{
defaultAge="AAA";
}
defaultCourriel = config.getInitParameter("defaultCourriel");
if(defaultCourriel==null)
{
defaultCourriel="[email protected]";
}
}
//GET
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
IOException, ServletException
{
// on récupère les paramètres du formulaire
String nom = request.getParameter("txtNom");
if (nom == null)
{
nom = defaultNom;
}
String age = request.getParameter("txtAge");
if (age == null)
{
age = defaultAge;
}
String courriel = request.getParameter("txtCourriel");
if (courriel == null)
{
courriel = defaultCourriel;
}
// on affiche le formulaire
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println
(
"<html>"+
"<head>"+
"<title>Carte d’identité</title>"+
"</head>"+
"<body>"+
"<center>"+
"<h2>Carte d’identité</h2>"+
"<form action=’’ method=’post’>"+
"<table>"+
"<tr>"+
"<td>Nom:</td>"+
"<td><input name=’txtNom’ value=’"+nom+"’ type=’text’ size=’30’></td>"+
"</tr>"+
"<tr>"+
"<td>Age:</td>"+
"<td><input name=’txtAge’ value=’"+ age +"’ type=’text’ size=’3’></td>"+
"</tr>"+
"<tr>"+
"<td>Courriel:</td>"+
"<td><input name=’txtCourriel’ value=’"+courriel+"’ type=’text’ size=’30’></td>"+
"</tr>"+
"</table>"+
"<table>"+
"<tr>"+
"<td><input type=’submit’ value=’Envoyer’></td>"+
"<td><input type=’reset’ value=’Effacer’></td>"+
"</tr>"+
"</table>"+
"</form>"+
"</center>"+
"</body>"+
"</html>"
);
}
/**
* @param request la requête HTTP du client
* @param response la réponse HTTP qu’on va construire
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
//on passe la main au GET
doGet(request, response);
}
}
這是我的web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app SYSTEM "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>CarteIdentite</servlet-name>
<servlet-class>ca.web.CarteIdentite</servlet-class>
<init-param>
<param-name>defaultNom</param-name>
<param-value>inconnu</param-value>
</init-param>
<init-param>
<param-name>defaultAge</param-name>
<param-value>XXX</param-value>
</init-param>
<init-param>
<param-name>defaultCourriel</param-name>
<param-value>inconnu</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>CarteIdentite</servlet-name>
<url-pattern>/CarteIdentite</url-pattern>
</servlet-mapping>
</web-app>
這裏是我的項目結構:
大家好, 我在項目結構中沒有WEB-INF/classes文件夾。 此外,當我右鍵點擊項目名稱,我沒有「構建路徑」 謝謝。 – cProg
你確實沒有在* Project Explorer *中看到它。但是,您可以在* Navigator *視圖中查看它,或者直接在WAR文件的部署位置中查看它。但是,這應該會自動發生。您是否重建項目,重新發布服務器並重新啓動服務器? – BalusC