问题
I creat a web application using eclipse and tomcat7 I had the following code in the html file and the java servlet class in the html file:
<form action="UserAccessServlet" method = "get">
in the servlet class I had
@WebServlet ("/UserAccessServlet")
then I just made some small changes (new println statements) but it shows no effect I changed the server name with the following peice of code
html file: <form action="SQA_Servlet" method = "get">
java class: @WebServlet ("/SQA_Servlet")
but it seems that no reload take place and I got the following error:
HTTP Status 404 - /SQA_Learning/SQA_Servlet
--------------------------------------------------------------------------------
type Status report
message /SQA_Learning/SQA_Servlet
description The requested resource (/SQA_Learning/SQA_Servlet) is not available.
I tried clean the module, refresh, close the reopen the project with the same result
I replaced @WebServlet ("/SQA_Servlet")
with @WebServlet(urlPatterns={"/SQA_Servlet"})
and still have no effect.. any suggestion.
回答1:
The WebServlet
name
attribute cannot start with a /
. Rather do,
@WebServlet("UserAccessServlet")
or leave it blank (if you want the WebServlet
to use the name of your Servlet class name. Example:
@WebServlet
public class UserAccessServlet extends HttpServlet {
//Do stuff
}
I would recommend declaring your WebServlet
annotations fully like in this example.
回答2:
I'm not sure when and in what for conditions you are receiving this error. But if you are deploying to tomcat, the following might occur:
Assuming your webapp is called "my.webapp" resulting in my.webapp.war
assuming you have a Servlet "servlet1" which performs action1 => @WebServlet(urlPatterns = "/action1")
(note the slash in front of action1)
Assuming you are calling this action with a html form:
<form action="/action1" method="GET">
this might not work because of the slash in front of action1
When it's there tomcat will redirect to localhost:8080/action1?.. while it should redirect to localhost:8080/my.project/action1?..
Solution alter the html so the form looks like:
<form action="action1" method="GET">
, don't change the @WebServlet(urlPatterns = "/action1")
Hope this helps someone!
来源:https://stackoverflow.com/questions/5786857/http-status-404-the-required-resource-is-not-found