无法实例化Servlet

时间:2019-06-12 04:53:42

标签: tomcat java-ee netbeans

尝试启动服务器时,我收到HTTP Status 500或404

这是一个通过netbeans / java-EE运行的ubuntu服务器,我不确定出什么问题。

我将粘贴一些代码以显示我的servlet和我的web.xml

  

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

    <servlet>
        <servlet-name>contato</servlet-name>
        <servlet-class>contato</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>contato</servlet-name>
        <url-pattern>/contato</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

  

Contato.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import db.Mensagem;
import db.MensagemDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Desenvolvimento
 */
public class contato extends HttpServlet {


   /**
    * 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 {
      response.setContentType("text/html;charset=UTF-8");
      try (PrintWriter out = response.getWriter()) {
         /* TODO output your page here. You may use following sample code. */
         out.println("<!DOCTYPE html>");
         out.println("<html>");
         out.println("<head>");
         out.println("<title>Entrar em Contato</title>");
         out.println("</head>");
         out.println("<body>");
         out.println("<h1>Área de Contato </h1>");
         out.println("</body>");

         out.println("<form action=\"contato\" method=\"GET\">");
         out.println("<pre><label>Descrição: </label> <input name=\"descricao\"></input> </pre>");
         out.println("<pre><button type=\"submit\">OK</button> </pre>");
         out.println("</form>");

         try{
            MensagemDAO dao = new MensagemDAO();
            ArrayList<Mensagem> mensagens = dao.select("select * from mensagem");

            out.println("<h3>Menesagens do sistema</h3>");

            for(Mensagem mensagem:mensagens){
               out.println("<pre>"+mensagem.getDescricao()+"</pre>");
            }

         }catch(Exception e){
            out.println("<span>ERRO: "+ e.toString() +" </span>");
         }

         out.println("</body>");
         out.println("</html>");
      }
   }

   /**
    * 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 {
      String descricao = request.getParameter("descricao");

      Mensagem nova = new Mensagem();
      nova.setDescricao(descricao);

      try{
         MensagemDAO dao = new MensagemDAO();
         dao.insert(nova);
      }catch(Exception e){
         System.out.println(e.toString());
      }

   }

}

如果在web.xml中将servlet更改为classes.contato.java,则会收到错误500,实例化servlet类时出错

否则我找不到错误404。

http://localhost:8080/WebApplication2/contato上的两个错误

我的Tomcat日志https://pastebin.com/TFL5Qkry,pastebin.com/QHrGexXL

我将非常感谢您的帮助:)

0 个答案:

没有答案