HTTP 400和500错误消息

时间:2011-10-12 04:05:26

标签: java servlets tomcat7

当我运行我的应用程序时,我遇到了HTTP错误:

  

HTTP状态404 - / ProjectName /

这是我项目的网址:

  

本地主机:8080 / Initiation1 /

当我在地址栏中输入此字符串时:

  

本地主机: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="XXXXXX@XXXXXXXX.XX";
        }
    }
    //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>

这是我的项目结构:

project structure

2 个答案:

答案 0 :(得分:0)

也许Eclipse没有将src视为源文件夹。尝试右键单击它并选择Build Path - &gt; Use as source folder

答案 1 :(得分:0)

检查类ca.web.CarteIdentite是否已正确编译并进入项目的WAR(在WEB-INF / classes下)。如果没有,请检查/更正项目构建路径设置。