gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:54:20 24 4
gpt4 key购买 nike

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

HTTP Status 404 - Not Found.

我搜索了另一个类似的问题,但找不到解决方案。我认为我的 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>

最佳答案

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

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

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

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

您的web.xml中没有定义这样的servlet

这就是您收到 404 错误的原因

关于java - HTTP 状态 404 - 未找到 - Servlet 和 JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47702096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com