本文介绍了调用Servlet会导致HTTP状态404“请求的资源不可用".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在类p1中有一个servlet register.我有一个JSP jsp1.jsp.我运行JSP文件并看到它,但是当我尝试将其应用于servlet时,Tomcat显示错误:

I have a servlet register in class p1. I have a JSP jsp1.jsp. I run JSP file and see it, but when I try to apply to the servlet, Tomcat shows an error:

Servlet:

@WebServlet("/register")

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>omgtuk</display-name>


 <servlet>
    <description></description>
    <display-name>register</display-name>
    <servlet-name>register</servlet-name>
    <servlet-class>p1.register</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>register</servlet-name>
    <url-pattern>/register</url-pattern>
  </servlet-mapping>

  <welcome-file-list>
    <welcome-file>jsp1.jsp</welcome-file>
  </welcome-file-list>
</web-app>

我正在使用Eclipse.

I'm using Eclipse.

推荐答案

这仅表示servlet不在监听/Register的URL模式.换句话说,您没有@WebServlet("/Register").

This simply means that the servlet isn't listening on an URL pattern of /Register. In other words, you don't have a @WebServlet("/Register").

在您的特定情况下,您在URL中输入了大小写错误.网址区分大小写.您正在调用/Register,但是您的servlet正在监听/register.相应地修正表单操作.

In your particular case, you made a case mistake in the URL. URLs are case sensitive. You're calling /Register, but your servlet is listening on /register. Fix your form action accordingly.

因此,它应该看起来像这样:

So, it should not look like this:

<form action="Register">

但是它应该看起来像这样:

But it should look like this:

<form action="register">

或者这,如果您无聊时碰巧在JSP周围移动,这会更健壮:

Or this, which is more robust in case you happen to move around JSPs when you're bored:

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


无关与具体问题无关,请注意,您是通过类上的@WebServlet注释和web.xml中的<servlet>条目注册了servlet的.这个不对.您应该使用其中一个.从Servlet 3.0(Java EE 6)开始,@WebServlet是注册servlet的新方法,而<servlet>是注册servlet的旧方法.


Unrelated to the concrete problem, please note that you registered the servlet via both a @WebServlet annotation on the class and a <servlet> entry in web.xml. This is not right. You should use the one or the other. The @WebServlet is the new way of registering servlets since Servlet 3.0 (Java EE 6) and the <servlet> is the old way of registering servlets.

只需除去web.xml中的整个<servlet><servlet-mapping>.您无需同时指定两者.确保您正在阅读最新的书籍/教程. Servlet 3.0自2009年12月以来已经存在.

Just get rid of the whole <servlet> and <servlet-mapping> in web.xml. You don't need to specify both. Make sure that you're reading up to date books/tutorials. Servlet 3.0 exist since December 2009 already.

另一个细节是p1不是 一个类,而是一个包.在加入Java EE之前,我强烈建议您花更多的时间在学习基本Java上.

Another detail is that p1 is not a class, it's a package. I'd warmly recommend to invest a bit more time in learning basic Java before diving into Java EE.

这篇关于调用Servlet会导致HTTP状态404“请求的资源不可用".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 20:22