1

我编写了一个 Java servlet 程序并通过本地 Tomcat 7 运行它,但它显示以下错误:

HTTP Status 404 - /skypark/registration
type Status report
message /skypark/registration
description The requested resource is not available.
Apache Tomcat/7.0.33

我不知道我的 Html 页面是什么原因

 <html>
 <head>
<title>
User registration
</title>
 </head>
<body>
<form action="registration" method="post">
 <center>
<h2><b>Skypark User Registration</b></h2>
 <table border="0">
<tr><td>
First Name
</td><td>
<input type="text" name="fname"/></br>
</td></tr><tr><td>
Last Name
</td><td>
  <input type="text" name="lname"/></br>
  </td></tr><tr><td>
 UserName
 </td><td>
  <input type="text" name="uname"></br>
  </td></tr><tr><td>
  Enter Password
    </td><td>
  <input type="password" name="pass"></br>
   </td></tr><tr><td>
  Re-Type Password
  </td><td>
  <input type="password" name="pass1"></br>
   </td></tr><tr><td>
   Enter Email ID
  </td><td>
   <input type="email" name="email1"></br>
    </td></tr><tr><td>
  Phone Number
 </td><td>
  <input type="number" name="phone">
 </td></tr><tr><td>
 Gender<br>
 </td></tr><tr><td>
   <input type="radio" name="gender" value="Male">Male</input></br>
  </td></tr><tr><td>
  <input type="radio" name="gender" value="Female">Female</input></br>
  </td></tr><tr><td>
   Enter Your Date of Birth<br>
 </td><td>
  <Table Border=0>
   <tr>
  <td>
  Date
  </td>

  <td>Month</td>

   <td>Year</td>
   </tr><tr>
 <td> <select name="date">
   <option value="1">1</option>
  <option value="2">2</option>
   <option value="3">3</option>
     <option value="4">4</option>
   <option value="5">5</option>
    .
    .
    .
     have some code
    .
    .
    .
  </table>
 <input type="submit" value="Submit"></br>
  </center>
  </form>
</body>
  </html>

我的小服务程序是:

  package skypark;
  import skypark.*;
   import java.io.*;
  import javax.servlet.*;
  import javax.servlet.http.*;
   import java.sql.*;
   public class Registration extends HttpServlet
     {
public static Connection prepareConnection()throws       ClassNotFoundException,SQLException
{
    String dcn="oracle.jdbc.driver.OracleDriver";
    String url="jdbc:oracle:thin:@JamesPJ-PC:1521:skypark";
    String usname="system";
    String pass="tiger";
    Class.forName(dcn);
    return DriverManager.getConnection(url,usname,pass);
}
public void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
{
    resp.setContentType("text/html");
    PrintWriter out=resp.getWriter();
  try
  {
    String phone1,uname,fname,lname,dob,address,city,state,country,pin,email,password,gender,lang,qual,relegion,privacy,hobbies,fav;

    uname=req.getParameter("uname");
    fname=req.getParameter("fname");
    lname=req.getParameter("lname");
    dob=req.getParameter("date");
    address=req.getParameter("address");
    city=req.getParameter("city");
    state=req.getParameter("state");
    country=req.getParameter("country");
    pin=req.getParameter("pin");
    email=req.getParameter("email1");
    password=req.getParameter("password");
    gender=req.getParameter("gender");
    phone1=req.getParameter("phone");
    lang="";
    qual="";
    relegion="";
    privacy="";
    hobbies="";
    fav="";
    int phone=Integer.parseInt(phone1);

    Connection con=prepareConnection();
    String Query="Insert into regdetails values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    PreparedStatement ps=con.prepareStatement(Query);
    ps.setString(1,uname);
    ps.setString(2,fname);
    ps.setString(3,lname);
    ps.setString(4,dob);
    ps.setString(5,address);
    ps.setString(6,city);
    ps.setString(7,state);
    ps.setString(8,country);
    ps.setString(9,pin);
    ps.setString(10,lang);
    ps.setString(11,qual);
    ps.setString(12,relegion);
    ps.setString(13,privacy);
    ps.setString(14,hobbies);
    ps.setString(15,fav);
    ps.setString(16,gender);

    int c=ps.executeUpdate();

    String query="insert into passmanager values(?,?,?,?)";
    PreparedStatement ps1=con.prepareStatement(query);

    ps1.setString(1,uname);
    ps1.setString(2,password);
    ps1.setString(3,email);
    ps1.setInt(4,phone);

    int i=ps1.executeUpdate();

    if(c==1||c==Statement.SUCCESS_NO_INFO && i==1||i==Statement.SUCCESS_NO_INFO)
    {
        out.println("<html><head><title>Login</title></head><body>");
        out.println("<center><h2>Skypark.com</h2>");
        out.println("<table border=0><tr>");
        out.println("<td>UserName/E-Mail</td>");
        out.println("<form action=login method=post");
        out.println("<td><input type=text name=uname></td>");
        out.println("</tr><tr><td>Password</td>");
        out.println("<td><input type=password name=pass></td></tr></table>");
        out.println("<input type=submit value=Login>");
        out.println("</form></body></html>");
    }
    else
    {
        out.println("<html><head><title>Error!</title></head><body>");
        out.println("<center><b>Given details are incorrect</b>");
        out.println(" Please try again</center></body></html>");
        RequestDispatcher rd=req.getRequestDispatcher("registration.html");
        rd.include(req,resp);
        return;
    }
  }
  catch(Exception e)
  {
      out.println("<html><head><title>Error!</title><body>");
      out.println("<b><i>Unable to process try after some time</i></b>");
      out.println("</body></html>");
      RequestDispatcher rd=req.getRequestDispatcher("registration.html");
      rd.include(req,resp);
      return;
  }
  out.flush();
  out.close();
   }
 }

而 web.xml 文件是

  <?xml version="1.0" encoding="UTF-8"?>
   <web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     version="3.0"
   metadata-complete="true">
  <servlet>
<servlet-name>reg</servlet-name>
<servlet-class>skypark.Registration</servlet-class>
  </servlet>
  <servlet-mapping>
<servlet-name>reg</servlet-name>
<url-pattern>/registration</url-pattern>
  </servlet-mapping>
   </web-app>

我保存在 C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\skypark\WEB_INF\web.xml 和 C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\skypark\WEB_INF\classes 中的 servlet 类中\skypark 和 registration.html 在 C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\skypark\

如果这方面的任何错误导致上述错误意味着请帮助我。在此先感谢....

4

2 回答 2

2

我认为问题出在这两行:

<form action="registration" method="post">在您的 html 页面中。

<url-pattern>/Registration</url-pattern>在你的 web.xml

您的操作设置为registration并且您的 url 模式期望Registration(注意大写R和小r)。

只需尝试更改它们(两者都相同),它应该可以工作

于 2012-12-15T05:48:02.163 回答
0

尝试将请求映射到您希望请求调用的方法。例如,如果您使用 Spring 框架,则需要在您的情况下将 web.xml 中的 servlet url-pattern 设置为 htm,然后返回 html 文件的名称。

@RequestMapping(value ="/registration",method = RequestMethod.GET)
public String render(Model model, HttpServletRequest request, HttpServletResponse response) {
{

--------------- compute ----
return "registration";
}

如果您向 /registration 发出请求,服务器将计算映射到它的方法,然后搜索“registration”(返回值)以显示。

希望能帮助到你 !

于 2012-12-21T14:17:29.550 回答