我是servlet编程的新手。我已经检查了有关Http 404错误的各种其他链接,但没有任何帮助。所以我在这里发布我的代码。
我在WebContent/
文件夹form1.html
,form2.html
,form3.html
中有三个html表单,所有这些表单都具有相同的url模式,因为在同一个Http会话中访问三种不同的表单
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
<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
<?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
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>");
}
}
}
}
我正在使用tomcat服务器8.0。我检查了这个Link,我的tomcat服务器具有完全相同的设置。并且this link跟随任何可能的错误但是,我不知道为什么我得到Http 404错误的确切原因。帮帮我解决为什么我收到此错误。
感谢。
答案 0 :(得分:0)
检查web.xml
是否存在于WEB-INF文件夹中。
并使用servlet映射到web.xml
并在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>
更多参考下面的链接: -
http://www.beingjavaguys.com/2013/08/jsp-servlet-hello-world-example.html
答案 1 :(得分:0)
该问题是由web.xml配置与WebServlet批注之间的冲突引起的。 web.xml是一个名为login的servlet,它目标是/ reg url,但是在RegistrationServlet中,还有一个@WebServlet注释的声明引用了同一个url / reg。
一种可能的解决方案是从web.xml中删除servlet声明,这意味着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>
只允许通过注释声明Servlet。希望这会有所帮助。