HTTP状态404 - 请求的资源不可用。 => servlet存在于web.xml中

时间:2014-09-02 08:08:08

标签: jsp servlets

我收到错误“HTTP Status 404 - / Servlet / NotaControllerServlet

类型:状态报告 message:/ Servlet / NotaControllerServlet description:请求的资源不可用“

当我尝试运行我的webApp时。这是我的代码:

1。 NoteDAO

package DAO;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import java.util.*;

import Entities.*;

public class NoteDAO {
    private static List<Nota> result;
    static Configuration configuration=new Configuration().configure();
    static SessionFactory sessionFactory=configuration.buildSessionFactory();
    static Session session=sessionFactory.openSession();
    static Transaction tx=session.beginTransaction();
public void addNota(int nota, int id_elev, int id_materie) {
        try {

            tx=session.beginTransaction();
            Elev elev = new Elev();
            elev.setId_elev(id_elev);
            Materie mat = new Materie();
            mat.setId_materie(id_materie);

            Nota newNota = new Nota();
            newNota.setNota(nota);
            newNota.setElev(elev);
            newNota.setMaterie(mat);
            session.save(newNota);
            tx.commit();

        } catch (HibernateException ex) {
            ex.printStackTrace();
        }

    }
}

2。的Servlet

package Servlet;

import java.io.IOException;
import java.util.List;

import DAO.*;
import Entities.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class NotaControllerServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected Elev getElev(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        Elev elv = null;
        String elev = request.getParameter("nume_elev");
        List<Elev> listElevi = EleviDAO.getEleviByName(elev);
        for (Elev el : listElevi) {
            elv = el;
        }
        return elv;
    }

    protected Materie getMaterie(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        Materie mat = null;
        String materie = request.getParameter("materie");
        List<Materie> listMaterie = MaterieDAO.getMaterieByName(materie);
        for (Materie mate : listMaterie) {
            mat = mate;
        }
        return mat;
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        int id_elev = getElev(request, response).getId_elev();
        int id_materie = getMaterie(request, response).getId_materie();
        int nota = Integer.parseInt(request.getParameter("nota"));

            HttpSession session=request.getSession(true);
        try {
            NoteDAO notaDAO = new NoteDAO();
            notaDAO.addNota(nota, id_elev, id_materie);
            response.sendRedirect("AddNota.jsp");
        }

        catch (Exception e) {
            e.printStackTrace();
        }

    }
}

第3。 JSP

         

  “http://www.w3.org/TR/html4/loose.dtd” &GT;

     

     

&lt;%@ page import =“java.util。,实体。,DAO。,   。的Servlet ;“%&GT;     

     

Adauga nota

     

$(function(){var spinner =   $( “#微调”)微调()。 });

  <h1>Adauga nota</h1>

  <%          List<Elev> list = EleviDAO.getElevi();          List<Materie> mat =
     

MaterieDAO.getMaterie(); %GT;

  <table cellpadding="3">             <tr>
          <td>Nume_elev</td>
          <td><select name="nume_elev">

                  <%
                      for (Elev el : list) {
                  %>
                  <Option value="<%=el.getNume_elev()%>"><%=el.getNume_elev()%></Option>
                  <%
                      }
                  %>
          </select></td>          </tr>

      <tr>
          <td>Nume_materie</td>
          <td><select name="materie">
                  <option value="Select materie">Select materie</option>
                  <%
                      for (Materie ma : mat) {
                  %>

                  <Option value="<%=ma.getDenumire_materie()%>"><%=ma.getDenumire_materie()%></Option>
                  <%
                      }
                  %>
          </select></td>          </tr>

      <tr>
          <td>Nota:</td>
          <td><input id="spinner" name="nota"></td>           </tr>

  </table>

  <p />       <input type="submit" value="Adauga nota" /> <input
     

type =“reset”value =“Reseteaza formular”/&gt;

     

4。 WEB.XML

> <?xml version="1.0" encoding="UTF-8"?> <web-app
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>   xmlns="http://java.sun.com/xml/ns/javaee"
>   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    version="2.5">
> 
>   <servlet>       
>       <display-name>Note</display-name>
>       <servlet-name>Note</servlet-name>
>       <servlet-class>Servlet.NotaControllerServlet</servlet-class>
>   </servlet>      
>   <servlet-mapping>   
>       <servlet-name>Note</servlet-name>
>       <url-pattern>/AddNota</url-pattern>     
>   </servlet-mapping>
  

     

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

你得到HTTP Status 404 - /Servlet/NotaControllerServlet,因为你的web.xml你的servlet映射是

<servlet-mapping>   
      <servlet-name>Note</servlet-name>
      <url-pattern>/AddNota/</url-pattern>     
</servlet-mapping>

所以你应该使用AddNote而不是servlet的文件夹目录。

然后在您的jsp表单中,您可以将其用作action=AddNote/NotaControllerServlet