HTTP状态404 - 未找到 - Servlet和JSP

时间:2017-12-07 19:06:54

标签: java jsp servlets

我正在开发一个网络应用。我想测试servlet时遇到问题。 当我运行我的servlet时弹出错误 -

  

HTTP状态404 - 未找到。

我搜索另一个类似的问题,但我找不到解决方案。我认为我的servlet和jsp文件之间没有任何关联。在我的代码下面:

GetServletController

    /*
 * 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.
 */
package Servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Adam
 */
public class GetServletController extends HttpServlet {

//    private HandsDBUtil handsDBUtil;
//
//    private ServletConfig config = getServletConfig();
//    private ServletContext context = config.getServletContext();
//
//    private String user = null;
//    private String pass = null;
//    private String c_name = null;
//    private String url = null;
//    @Override
//    public void init() throws ServletException {
//        super.init();
//        Enumeration enumeration = context.getInitParameterNames();
//        while (enumeration.hasMoreElements()) {
//            String name = (String) enumeration.nextElement();
//            switch (name) {
//                case "url":
//                    url = context.getInitParameter(name);
//                    break;
//                case "pass":
//                    pass = context.getInitParameter(name);
//                    break;
//                case "user":
//                    user = context.getInitParameter(name);
//                    break;
//                case "c_name":
//                    c_name = context.getInitParameter(name);
//                    break;
//            }
//        }
//
//        handsDBUtil = new HandsDBUtil(user, pass, c_name, url);
//
//    }
    /**
     * 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 {

        response.setContentType("text/plain; charset=ISO-8859-2");

        PrintWriter out = response.getWriter();

        out.println("WOW");

        HandsDBUtil handsDBUtil;

        ServletConfig config = getServletConfig();
        ServletContext context = config.getServletContext();

        String user = null;
        String pass = null;
        String c_name = null;
        String url = null;

        Enumeration enumeration = context.getInitParameterNames();
        while (enumeration.hasMoreElements()) {
            String name = (String) enumeration.nextElement();
            switch (name) {
                case "url":
                    url = context.getInitParameter(name);
                    break;
                case "pass":
                    pass = context.getInitParameter(name);
                    break;
                case "user":
                    user = context.getInitParameter(name);
                    break;
                case "c_name":
                    c_name = context.getInitParameter(name);
                    break;
            }
        }

        handsDBUtil = new HandsDBUtil(user, pass, c_name, url);

        out.println("wow");
        out.println(user);
        try {
            //listHands(request, response);
            List<Hand> hands = handsDBUtil.getHands();
            System.out.println(hands);
            request.setAttribute("HAND_LIST", hands);

            RequestDispatcher dispatcher = request.getRequestDispatcher("getHands");
            dispatcher.forward(request, response);
        } catch (Exception ex) {
            throw new ServletException(ex);
        }
    }

//    private void listHands(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, Exception {
//
//        List<Hand> hands = handsDBUtil.getHands();
//
//        request.setAttribute("HAND_LIST", hands);
//
//        RequestDispatcher dispatcher = request.getRequestDispatcher("getHands");
//        dispatcher.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>

}

JSP

<%@page import="java.util.*, Servlets.GetServletController, Servlets.Hand" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Poker Hands List</title>
    </head>

    <form action="GetServletController"/>

    <%


        List<Hand> theHands = (List<Hand>) request.getAttribute("HAND_LIST");
        %>

    <body>

        <%= theHands %>

        <div id="wrapper">
            <div id="header">
                <h2>
                    Poker Hands List
                </h2>
            </div>
        </div>

        <div id="container">
            <div id="content">

            </div>
        </div>
    </body>
</html>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
    <context-param>
        <param-name>url</param-name>
        <param-value>jdbc:derby://localhost:1527/lab</param-value>
    </context-param>
    <context-param>
        <param-name>c_name</param-name>
        <param-value>org.apache.derby.jdbc.ClientDriver</param-value>
    </context-param>
    <context-param>
        <param-name>user</param-name>
        <param-value>lab</param-value>
    </context-param>
    <context-param>
        <param-name>pass</param-name>
        <param-value>lab</param-value>
    </context-param>
    <servlet>
        <servlet-name>ServletTest</servlet-name>
        <servlet-class>Servlets.ServletTest</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>GetServletController</servlet-name>
        <servlet-class>Servlets.GetServletController</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ServletTest</servlet-name>
        <url-pattern>/ServletTest</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>GetServletController</servlet-name>
        <url-pattern>/GetServletController</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>getHands.jsp</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>mainMenu.jsp</welcome-file>
    </welcome-file-list>
</web-app>

1 个答案:

答案 0 :(得分:1)

Request dispatcher用于调度请求到任何资源,例如服务器上的HTML, Image, JSP, Servlet。如果你想将你的请求转发给jsp,你需要定义如下

 RequestDispatcher dispatcher = request.getRequestDispatcher("getHands.jsp");

在您的情况下,您将请求转发给servlet。

RequestDispatcher dispatcher = request.getRequestDispatcher("getHands");

web.xml

中没有定义此类servlet

这就是为什么你得到 404错误