HTTP 状态 404 - Servlet [ServletName] 不可用 [英] HTTP Status 404 - Servlet [ServletName] is not available

查看:30
本文介绍了HTTP 状态 404 - Servlet [ServletName] 不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我的 index.jsp 和 web.xml 不断进入 HTTP 404 和 500

The problem : My index.jsp with web.xml keeps going into HTTP 404 and 500

我使用的是 Tomcat6.

I'm using Tomcat6 .

这是来自 index.jsp :

This is from index.jsp :

  <legend>Registration</legend>
  <form action="./register"> <%-- Address from web.xml  --%>
    First name: <input type="text" name="firstName"><br>
    Last name: <input type="text" name="lastName"><br>
    <input type="submit" value="Register">
  </form>

当我注册时:

然后我点击 name 和 last-name ,我进入 404 ,消息:

and I hit the name and last-name , I go into 404 , the message :

输入状态报告

message Servlet RegistrationServlet 不可用

message Servlet RegistrationServlet is not available

描述 请求的资源(Servlet RegistrationServlet 不可用)不可用.

description The requested resource (Servlet RegistrationServlet is not available) is not available.

Apache Tomcat/6.0.35

Apache Tomcat/6.0.35

您认为该错误的原因是什么?

What do you think it the cause for that error ?

RegistrationServlet 类位于 src/coreservlets/

我检查了 web.xml 但它似乎没问题,但它在这里(如果有帮助的话):

I checked web.xml but it seems to be okay , but here it is (if it would be helpful):

<?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/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  <servlet>
    <servlet-name>ShowBalance</servlet-name>
    <servlet-class>coreservlets.ShowBalance</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ShowBalance</servlet-name>
    <url-pattern>/show-balance</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>RandomNumberServlet</servlet-name>
    <servlet-class>coreservlets.RandomNumberServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>RandomNumberServlet</servlet-name>
    <url-pattern>/random-number</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>RegistrationServlet</servlet-name>
    <servlet-class>coreservlets.RegistrationServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>RegistrationServlet</servlet-name>
    <url-pattern>/register</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>PrimeServlet</servlet-name>
    <servlet-class>coreservlets.PrimeServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>PrimeServlet</servlet-name>
    <url-pattern>/prime</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

过去两天我一直在努力解决这个小罪魁祸首,但没有任何帮助,我们将不胜感激.

I've been trying to fix this little culprit for the last two days but nothing , any help would be much appreciated .

根据要求,这里是 RegistrationServlet

package coreservlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class RegistrationServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        synchronized (session) {
            NameBean nameBean = (NameBean) session.getAttribute("name");
            if (nameBean == null) {
                nameBean = new NameBean();
                session.setAttribute("name", nameBean);
            }
            nameBean.setFirstName(request.getParameter("firstName"));
            nameBean.setLastName(request.getParameter("lastName"));
            String address = "/WEB-INF/mvc-sharing/ShowName.jsp";
            RequestDispatcher dispatcher = request
                    .getRequestDispatcher(address);
            dispatcher.forward(request, response);
        }
    }
}

这里还有项目树:

推荐答案

您的 URL 完全没问题.如果 URL 错误,您将收到如下 404 消息:

Your URL is completely fine. If the URL was wrong, you would have gotten a 404 message like follows:

请求的资源register不可用

但你却得到了

Servlet RegistrationServlet 不可用

Servlet RegistrationServlet is not available

这意味着找到了 servlet,但它无法执行,因为它的构造和初始化失败并出现异常.在 Tomcat 的掩护下,基本上以下步骤之一失败了:

This means that the servlet was found, but that it couldn't be executed because its construction and initialization failed with an exception. Under Tomcat's covers, basically one of the following steps has failed:

Servlet servlet = new RegistrationServlet();
servlet.init(servletConfig);
servlet.init();

您需要阅读此异常的服务器日志,然后相应地修复代码.

You need to read the server logs for this exception and then fix the code accordingly.

虽然您的 URL 完全没问题,但它很容易因上下文的变化而出错.您更愿意指定一个相对于域的 URL:

Whilst your URL is completely fine, it's error prone to changes in the context. You'd rather like to specify a domain-relative URL instead:

<form action="${pageContext.request.contextPath}/register">

另请参阅此相关答案:在调用转发到 JSP 的 Servlet 时,浏览器无法访问/查找 CSS、图像和链接等相关资源

这篇关于HTTP 状态 404 - Servlet [ServletName] 不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆