Netbeans 和 glassfish HTTP 错误 404

问题描述 投票:0回答:2

我需要你的帮助。我已经为 Web 应用程序制作了一个源代码,但是当我点击“添加”提交按钮时出现了这个错误,而我在其他提交按钮上也出现了同样的错误。我的源代码是:

filoptwxoinfo.jsp :

 <%-- 
    Document   : index
    Created on : 17 Δεκ 2012, 6:39:05 μμ
    Author     : Editor
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Πληροφορίες Φιλοπτώχου</title>
    </head>
    <body>
        <table border="1">
        <td><a href="filoptwxoinfo.jsp">Μέλη Φιλοπτώχου</a></td>
        <td><a href="./FullFiloptwxo.jsp">Σύνολο Φιλοπτώχου</a></td>
    </table>
    </br>

    <form action=".FiloptwxoServlet" method="POST">
        <table>
            <tr>
                <td>Α.Φ.Μ. :</td>
                <td><input type="text" name="afm" value="${filoptwxo.afm}"/></td>
            </tr>

            <tr>
                <td>Όνομα :</td>
                <td><input type="text" name="name" value="${filoptwxo.name}"/></td>
            </tr>

            <tr> 
                <td>Επώνυμο:</td>
                <td><input type="text" name="lastname" value="${filoptwxo.lastname}"/></td>
            </tr>

            <tr>
                <td>Πατρώνυμο:</td>
                <td><input type="text" name="fathername" value="${filoptwxo.fathername}"/></td>
            </tr>

            <tr>
                <td>Διεύθυνση Οικείας:</td>
                <td><input type="text" name="address" value="${filoptwxo.address}"/></td>
            </tr>

            <tr>
                <td>Τηλέφωνο:</td>
                <td><input type="text" name="number" value="${filoptwxo.number}"/></td>
            </tr>

            <tr>  
                <td>Μέλη Οικογενείας:</td>
                <td><input type="text" name="numberop" value="${filoptwxo.numberop}"/></td>
            </tr>

            <tr>
                <td colspan="2"><input type="Submit" name="operation" value="Add" />
                                <input type="Submit" name="operation" value="Edit" />
                                <input type="Submit" name="operation" value="Delete" />
                                <input type="Submit" name="operation" value="Search" />

                        </tr>
                </table>
        </form>

    </body>
</html>

Fullfiloptwxo.jsp :

 <%-- 
        Document   : FullFiloptwxo
        Created on : Dec 17, 2012, 6:47:52 PM
        Author     : Editor
    --%>

    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"  %>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>Λίστα Μελών Φιλοπτώχου</title>
        </head>
        <body>
             <table border="1">
            <td><a href="filoptwxoinfo.jsp">Μέλη Φιλοπτώχου</a></td>
            <td><a href="./FullFiloptwxo.jsp">Σύνολο Φιλοπτώχου</a></td>
        </table>
        <br />
            <h3> Λίστα Μελών </h3>

            <table border="1">
                <th> Α.Φ.Μ. </th>
                <th> Όνομα </th> 
                <th> Επώνυμο </th>
                <th> Πατρώνυμο </th>
                <th> Διεύθυνση Οικείας </th>
                <th> Τηλέφωνο </th>
                <th> Μέλη Οικογενείας </th>
                <c:forEach items="${requestScope.list}" var="Filoptwxo">  
                    <tr>
                        <td><a href=".GetFiloptwxo?afm=${filoptwxo.afm}">${filoptwxo.afm}</a></td>
                        <td>${filoptwxo.name}</td>
                        <td>${filoptwxo.lastname}</td>
                        <td>${filoptwxo.fathername}</td>
                        <td>${filoptwxo.address}</td> 
                        <td>${filoptwxo.phone}</td>
                        <td>${filoptwxo.numberop}</td>
                    </tr>
                </c:forEach>

            </table>    
        </body>
    </html>

Filoptwxo.java :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

/**
 * @author Editor
 */
public class Filoptwxo {
    private String afm;
    private String name;
    private String lastname;
    private String address;
    private String fathername;
    private String numberop;
    private String phone;


    public String getAfm() {
        return afm;
    }

    public void setAfm(String afm) {
        this.afm = afm;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getFathername() {
        return fathername;
    }

    public void setFathername(String fathername) {
        this.fathername = fathername;
    }

     public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    } 

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

     public String getNumberop() {
        return numberop;
    }

    public void setNumberop(String numberop) {
        this.numberop = numberop;
    }

    public Filoptwxo(String afm, String name, String lastname, String fathername, String address, String phone, String numberop ){
        this.afm = afm;
        this.name = name;
        this.lastname = lastname;
        this.fathername = fathername;
        this.address = address;
        this.numberop = numberop;
        this.phone = phone;
    }
    public Filoptwxo(){}
}

FiloptwxoDAO.java :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.ejb;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.sql.DataSource;
import model.Filoptwxo;

/**
 * @author Editor
 */
@Stateless
public class FiloptwxoDAO { 
    @Resource(name="jdbc/filoptwxoDB")
    private DataSource ds;

    public void addFiloptwxo (Filoptwxo filoptwxo) {
        String sql = "INSERT INTO FILOPTWXO VALUES('"+ filoptwxo.getAfm() +"', '"+ filoptwxo.getName() +"', '"+ filoptwxo.getLastname() +"', '"+ filoptwxo.getFathername() +"', '"+ filoptwxo.getAddress() +"', '"+ filoptwxo.getPhone() +"', '"+ filoptwxo.getNumberop() +"')";
        executeModifyQuery(sql);
    }

    public void editFiloptwxo (Filoptwxo filoptwxo) {
        String sql = "UPDATE FILOPTWXO SET NAME='" + filoptwxo.getName() +"', ADDRESS" + filoptwxo.getAddress() +"', LASTNAME" + filoptwxo.getLastname() +"', FATHERNAME" + filoptwxo.getFathername() +"', PHONE" + filoptwxo.getPhone() +"', NUMBEROP" + filoptwxo.getNumberop() +"' WHERE AFM='"+ filoptwxo.getAfm() +"'";
        executeModifyQuery(sql);
    }

    public void deleteFiloptwxo(Filoptwxo filoptwxo) {
        String sql = "DELETE FROM STUDENT WHERE AFM= '"+ filoptwxo.getAfm() + "'";
        executeModifyQuery(sql);
    }

      public Filoptwxo getFiloptwxo(String afm) {
        Filoptwxo filoptwxo = new Filoptwxo();
        String sql = "SELECT * FROM FILOPTWXO WHERE AFM='"+ afm+"'";
        System.out.println(sql);
        ResultSet rs = executeFetchQuery(sql);
        try {
            if (rs.next()){
               filoptwxo.setAfm(rs.getString("AFM"));
               filoptwxo.setName(rs.getString("NAME"));
               filoptwxo.setLastname(rs.getString("LASTNAME"));
               filoptwxo.setFathername(rs.getString("FATHERNAME"));
               filoptwxo.setAddress(rs.getString("ADDRESS"));
               filoptwxo.setPhone(rs.getString("PHONE"));
               filoptwxo.setNumberop(rs.getString("NUMBEROP"));
            }
        } catch (Exception ex) {
            System.err.println("GS" + ex.getMessage());
        }
        return filoptwxo;
    }

    public ArrayList<Filoptwxo> getFullFiloptwxo() {
        ArrayList<Filoptwxo> list = new ArrayList<Filoptwxo>();
        String sql = "SELECT * FROM FILOPTWXO";
        ResultSet rs = executeFetchQuery(sql);
        try {
            while(rs.next()) {
               Filoptwxo filoptwxo = new Filoptwxo();
               filoptwxo.setAfm(rs.getString("AFM"));
               filoptwxo.setName(rs.getString("NAME"));
               filoptwxo.setLastname(rs.getString("LASTNAME"));
               filoptwxo.setFathername(rs.getString("FATHERNAME"));
               filoptwxo.setAddress(rs.getString("ADDRESS"));
               filoptwxo.setPhone(rs.getString("PHONE"));
               filoptwxo.setNumberop(rs.getString("NUMBEROP"));
               list.add(filoptwxo);
            }
        } catch (SQLException ex) {
            System.err.println(ex.getMessage());
        }
        return list;
    }

    public void executeModifyQuery(String sql)  {
        try {
            Connection conn = ds.getConnection();
            conn.createStatement().execute(sql);
            conn.close();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }  

     public ResultSet executeFetchQuery(String sql)  {
         ResultSet rs = null;
         try {
            Connection conn = ds.getConnection();
            rs = conn.createStatement().executeQuery(sql);
            conn.close();
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return rs;
    }

}

FiloptwxoServlet.java :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import javax.ejb.EJB;
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 model.Filoptwxo;
import model.ejb.FiloptwxoDAO;

/**
 * @author Editor
 */
@WebServlet(name = "FiloptwxoServlet", urlPatterns = {"/FiloptwxoServlet"})
public class FiloptwxoServlet extends HttpServlet {
    @EJB private FiloptwxoDAO filoptwxoDAO;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    String afm = request.getParameter ("afm");
    String name = request.getParameter ("name");
    String lastname = request.getParameter ("lastname");
    String fathername = request.getParameter ("fathername");
    String address = request.getParameter ("address");
    String phone = request.getParameter ("phone");
    String numberop = request.getParameter ("numberop");
    String operation = request.getParameter ("operation");

    Filoptwxo filoptwxo = new Filoptwxo(afm, name, lastname, fathername, address, phone, numberop);

    if (operation.equalsIgnoreCase("Add")) {
        filoptwxoDAO.addFiloptwxo(filoptwxo);
        request.setAttribute("filoptwxo", filoptwxo);
    } else if (operation.equalsIgnoreCase("Edit")) {
        filoptwxoDAO.editFiloptwxo(filoptwxo);
        Filoptwxo searchedFiloptwxo = filoptwxoDAO.getFiloptwxo(afm);
        request.setAttribute("filoptwxo", searchedFiloptwxo);       

    } else if (operation.equalsIgnoreCase("Delete")) {
        filoptwxoDAO.deleteFiloptwxo(filoptwxo);
    } else if (operation.equalsIgnoreCase("Search")) {
        Filoptwxo searchedFiloptwxo = filoptwxoDAO.getFiloptwxo(afm);
        request.setAttribute("filoptwxo", searchedFiloptwxo);
    }
    request.getRequestDispatcher("filoptwxoinfo.jsp").forward(request, response);    
 }


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * 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 {
        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);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

FullFiloptwxo.java :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.util.ArrayList;
import javax.ejb.EJB;
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 model.Filoptwxo;
import model.ejb.FiloptwxoDAO;

/**
 * @author Editor
 */
@WebServlet(name = "FullFiloptwxo", urlPatterns = {"/FullFiloptwxo"})
public class FullFiloptwxo extends HttpServlet {
    @EJB private FiloptwxoDAO filoptwxoDAO;
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     * 
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

       ArrayList<Filoptwxo> list = filoptwxoDAO.getFullFiloptwxo();
       request.setAttribute("list", list);
       request.getRequestDispatcher("FullFiloptwxo.jsp").forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * 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 {
        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);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

GetFiloptwxo.java :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import javax.ejb.EJB;
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 model.Filoptwxo;
import model.ejb.FiloptwxoDAO;

/**
 * @author Editor
 */
@WebServlet(name = "GetFiloptwxo", urlPatterns = {"/GetFiloptwxo"})
public class GetFiloptwxo extends HttpServlet {
    @EJB private FiloptwxoDAO filoptwxoDAO;
    /**
     * Processes requests for both HTTP 
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       String afm = request.getParameter("afm");
       Filoptwxo filoptwxo = filoptwxoDAO.getFiloptwxo(afm);
       request.setAttribute("filoptwxo", filoptwxo);
       request.getRequestDispatcher("filoptwxoinfo.jsp").forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * 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 {
        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);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

web-xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>filoptwxoinfo.jsp</welcome-file>
    </welcome-file-list>
</web-app>

这是我使用的源代码,但我遇到了 HTTP ERROR 404 问题,我不知道我还必须做什么......我现在正在尝试找到解决方案 2 天,所以这就是我写的原因整个代码。

java netbeans glassfish http-status-code-404
2个回答
0
投票

form action=".FiloptwxoServlet"
前面有句号


0
投票

您应该按照定义的方式调用 servlet :

@WebServlet(name = "FiloptwxoServlet", urlPatterns = {"/FiloptwxoServlet"})

 <form action=".FiloptwxoServlet" method="POST">

 <form action="FiloptwxoServlet" method="POST">

更改所有指向 .jsp 文件的链接(在所有 .java 文件中)

“filoptwxoinfo.jsp”
"/WEB-INF/filoptwxoinfo.jsp"

HTML 的 action 属性可以指向一个 servlet URL 并且 method="post" 将触发 servlet 的 doPost() 方法,您可以在其中完全自由地控制 HTTP 请求和响应。将所有逻辑放入 doPost() 方法中。

  • 不要从 doGet() 方法调用 doPost() 方法。
  • 不要让他们都调用一些其他的常用方法,比如 processRequest() 或其他东西。(正如您在代码中所做的那样)这是完全错误的。

FiloptwxoServlet.java

[...]
@WebServlet("/FiloptwxoServlet")

[...]

public class FiloptwxoServlet extends HttpServlet {
[...]

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

[...]  
request.getRequestDispatcher("/WEB-INF/filoptwxoinfo.jsp").forward(request, response);
}

有关更多信息,请查看此处stackoverflow:如何调用 servlet-through-a-jsp-page
servlets/信息

© www.soinside.com 2019 - 2024. All rights reserved.