NetBeans - HTTP状态404 - 未找到 - 说明请求的资源不可用

时间:2016-04-18 03:46:08

标签: java jsp netbeans web.xml

大家!!!

我很麻烦......我有一个JEE应用程序,当我点击确定按钮时,显示此消息:HTTP状态404 - 未找到 - 说明请求的资源不可用。

JSP:

<%@include file="_header.jsp" %>

<img src="include/sections/projetos.jpg" align="bottom" /><h1 id="title">ProfessorInserir</h1>

<p id="text">
    &nbsp;&nbsp;Inserção de Professores
</p>

<form action="professor">
    Matrícula: <input type="text" name="mat"/>
    <br/>Nome: <input type="text" name="nome"/>
    <br/>Disciplina: <input type="text" name="disc"/>
    <input type="hidden" name="acao" value="inserir"/>
    <br/> <input type="submit" value="ok"/>
</form>    


<%@include file="_footer.jsp" %>

Java类:

package modelo;

public class Professor {

    private String mat;
    private String nome;
    private String disc;

    public String getMat() {
        return mat;
    }

    public void setMat(String mat) {
        this.mat = mat;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

      public String getDisc() {
        return disc;
    }

    public void setDisc(String disc) {
        this.disc = disc;
    }
  }

的Servlet

package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modelo.Professor;

public class ServletProfessor extends HttpServlet {

    private ArrayList professores = new ArrayList();

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String comando = request.getParameter("acao");


        if (comando.equals("inserir")){
            String mat = request.getParameter("mat");
            String nome = request.getParameter("nome");
            String disc = request.getParameter("disc");

            Professor p = new Professor();
             p.setMat(mat);
             p.setNome(nome);
             p.setDisc(disc);

             professores.add(p);

             response.sendRedirect("professorInserir.jsp");

        }                    

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * 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 {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

Web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="WebApp_ID" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   <display-name>academicnet</display-name>
   <servlet>
      <description />
      <display-name>ServletLogin</display-name>
      <servlet-name>ServletLogin</servlet-name>
      <servlet-class>servlets.ServletLogin</servlet-class>
   </servlet>
   <servlet>
      <description />
      <display-name>ServletLogout</display-name>
      <servlet-name>ServletLogout</servlet-name>
      <servlet-class>servlets.ServletLogout</servlet-class>
   </servlet>
   <servlet>
      <servlet-name>ServletAluno</servlet-name>
      <servlet-class>servlets.ServletAluno</servlet-class>
   </servlet>
   <servlet>
      <servlet-name>ServletProfessor</servlet-name>
      <servlet-class>servlets.ServletProfessor</servlet-class>
   </servlet>
   <servlet-mapping>
      <servlet-name>ServletLogin</servlet-name>
      <url-pattern>/login</url-pattern>
   </servlet-mapping>
   <servlet-mapping>
      <servlet-name>ServletLogout</servlet-name>
      <url-pattern>/logout</url-pattern>
   </servlet-mapping>
   <servlet-mapping>
      <servlet-name>ServletAluno</servlet-name>
      <url-pattern>/aluno</url-pattern>
   </servlet-mapping>
   <servlet-mapping>
      <servlet-name>ServletProfessor</servlet-name>
      <url-pattern>/ServletProfessor</url-pattern>
   </servlet-mapping>
   <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      <welcome-file>index.htm</welcome-file>
      <welcome-file>index.jsp</welcome-file>
      <welcome-file>default.html</welcome-file>
      <welcome-file>default.htm</welcome-file>
      <welcome-file>default.jsp</welcome-file>
   </welcome-file-list>
</web-app>

请帮助我!!!

1 个答案:

答案 0 :(得分:0)

您的表单action指向professor,但没有注册的终结点,您应该使用web.xml中定义的那个,即ServletProfessor

ex:<form action="ServletProfessor">

或者,添加新端点:

<servlet-mapping>
    <servlet-name>ServletProfessor</servlet-name>
    <url-pattern>/ServletProfessor</url-pattern>
    <url-pattern>/professor</url-pattern>  <!-- <---- here -->
</servlet-mapping>