[英]“HTTP Status 404 The requested resource is not available”
I'm newbie to servlet programming. 我是servlet编程的新手。 I have checked various other links about the Http 404 error but nothing helped me. 我检查了有关Http 404错误的各种其他链接,但没有任何帮助。 So I'm posting my code here. 所以我在这里发布我的代码。
I have three html forms in WebContent/
folder form1.html
, form2.html
, form3.html
, all these forms have same url pattern because accessing three different forms in the same Http session. 我在WebContent/
文件夹form1.html
, form2.html
, form3.html
有三种html表单,所有这些表单都具有相同的url模式,因为在同一Http会话中访问了三种不同的表单。
form1.html form1.html
<html> <head> <title>Adhar Registration Form</title> </head> <body style="background-color:orange"> <h1>FORM 1</h1> <form action="./reg" method="get"> <table> <tr><td>NAME:</td><td><input type="text" name="id"/></td></tr> <tr><td>F_NAME:</td><td><input type="text" name="name"/></td></tr> <tr><td>M_NAME:</td><td><input type="text" name="email"/></td></tr> <tr><td><input type="submit" name= "NEXT"> </td></tr> </table> <input type="hidden" name="fno" value="1"> </form> </body> </html>
form2.html form2.html
<html> <head> <title>Adhar Registration Form</title> </head> <body style="background-color:orange"> <h1>FORM 2</h1> <form action="./reg" method="get"> <table> <tr><td>CONTACT:</td><td><input type="text" name="id"/></td></tr> <tr><td>EMAIL:</td><td><input type="text" name="name"/></td></tr> <tr><td>ADDRESS:</td><td><textarea rows ="10" cols="5" name="address"></textarea></td></tr> <tr><td><input type="submit" name= "NEXT"> </td></tr> </table> <input type="hidden" name="fno" value="2"> </form> </body> </html>
<html> <head> <title>Adhar Registration Form</title> </head> <body style="background-color:orange"> <h1>FORM 3</h1> <form action="./reg" method="get"> <table> <tr><td>QUALIFICATION:</td><td><input type="text" name="id"/></td></tr> <tr><td>PAN NO:</td><td><input type="text" name="name"/></td></tr> <tr><td><input type="submit" name= "Register"> </td></tr> </table> <input type="hidden" name="fno" value="3"> </form> </body> </html>
web.xml web.xml中
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Adhar</display-name> <welcome-file-list> <welcome-file>form1.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>login</servlet-name> <servlet-class>container.RegistrationServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/reg</url-pattern> </servlet-mapping> </web-app>
RegistrationServlet.java RegistrationServlet.java
package container;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class RegistrationServlet
*/
@WebServlet("/reg")
public class RegistrationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public static final String URL = "jdbc:mysql://localhost/db";
public static final String USER = "root";
public static final String PASSWORD = "12345";
public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
/**
* @see HttpServlet#HttpServlet()
*/
public RegistrationServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
HttpSession hs = request.getSession();
String fno = request.getParameter("fno");
if(fno.equals("1")){
String name = request.getParameter("name");
String f_name = request.getParameter("f_name");
String m_name = request.getParameter("m_name");
hs.setAttribute("name", name);
hs.setAttribute("f_name", f_name);
hs.setAttribute("m_name", m_name);
response.sendRedirect("./Form2.html");
}
if(fno.equals("2")){
String contact = request.getParameter("contact");
String email = request.getParameter("email");
String address = request.getParameter("address");
hs.setAttribute("contact", contact);
hs.setAttribute("email", email);
hs.setAttribute("address", address);
response.sendRedirect("./Form3.html");
}
if(fno.equals("3")){
String qual = request.getParameter("qual");
String pan = request.getParameter("pan");
String name = (String)hs.getAttribute("name");
String f_name = (String)hs.getAttribute("f_name");
String m_name = (String)hs.getAttribute("m_name");
String contact = (String)hs.getAttribute("contact");
String email = (String)hs.getAttribute("email");
String address = (String)hs.getAttribute("address");
try {
Class.forName(DRIVER_CLASS);
System.out.println("Loaded the driver");
Connection con = DriverManager.getConnection(URL,USER,PASSWORD);
PreparedStatement ps = con.prepareStatement("INSERT into adharReg values(?,?,?,?,?,?,?,?)");
ps.setString(1, name);
ps.setString(2, f_name);
ps.setString(3, m_name);
ps.setString(4, contact);
ps.setString(5, email);
ps.setString(6, address);
ps.setString(7, qual);
ps.setString(8, pan);
int i = ps.executeUpdate();
if(i!=0){
out.println("<h1>REGISTRATION SUCCESS</h1>");
}
else{
out.println("<h1>REGISTRATION FAILED</h1>");
}
} catch (Exception e) {
// TODO: handle exception
out.println("<h1>REGISTRATION FAILED" + e.getMessage() + " </h1>");
}
}
}
}
I'm using tomcat server 8.0. 我正在使用Tomcat服务器8.0。 I checked this Link and my tomcat server has exact same settings. 我检查了此链接,并且我的tomcat服务器具有完全相同的设置。 And followed this link for any possible mistakes but, I don't know the exact reason why i'm getting Http 404 error. 并遵循此链接的任何可能的错误,但是,我不知道我收到Http 404错误的确切原因。 Help me out why i'm getting this error. 帮助我,为什么我会收到此错误。
Thanks. 谢谢。
check web.xml
is exist into WEB-INF folder. 检查web.xml
是否存在于WEB-INF文件夹中。
and use servlet mapping into web.xml 并使用servlet映射到web.xml
And register the servlet instead in web.xml like this: 然后像这样在web.xml中注册servlet:
<servlet>
<servlet-name>yourServlet</servlet-name>
<servlet-class>com.example.YourServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>yourServlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
for more refer bellow link :- 有关更多信息,请参见波纹管链接:-
http://www.beingjavaguys.com/2013/08/jsp-servlet-hello-world-example.html http://www.beingjavaguys.com/2013/08/jsp-servlet-hello-world-example.html
https://www.javatpoint.com/servlet-with-annotation https://www.javatpoint.com/servlet-with-annotation
The issue is caused by a conflict between web.xml configuration and the WebServlet annotation. 该问题是由web.xml配置和WebServlet批注之间的冲突引起的。 The web.xml is delcaring a servlet called login that target to the /reg url, but also in the RegistrationServlet there is a declaration throught @WebServlet annotation that reference to the same url /reg. web.xml正在传送一个名为login的servlet,该servlet的目标是/ reg url,但在RegistrationServlet中,也有一个@WebServlet批注声明,该声明引用了相同的URL / reg。
One possible solution is to remove the servlet declaration from the web.xml, that means that the web.xml content should be like this. 一种可能的解决方案是从web.xml中删除servlet声明,这意味着web.xml内容应如下所示。
web.xml web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>Adhar</display-name>
<welcome-file-list>
<welcome-file>form1.html</welcome-file>
</welcome-file-list>
</web-app>
Just let the Servlet declared by annotation only. 只需让Servlet仅通过注释声明即可。 Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.