Calling servlet results in HTTP Status 404 “The re

2019-01-26 14:13发布

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:

HTTP Status 404

The requested resource (/omgtuk/Register) is not available.

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>

I'm using Eclipse.

1条回答
混吃等死
2楼-- · 2019-01-26 14:43

The requested resource (/omgtuk/Register) is not available.

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").

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">

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

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

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.

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.

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.

See also:

查看更多
登录 后发表回答