我编写了一个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>
我的servlet是:
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 \ skypark中的servlet类 和C:\ Program Files \ Apache Software Foundation \ Tomcat 7.0 \ webapps \ skypark \
中的registration.html如果有任何错误导致上述错误意味着请帮助我。谢谢提前....
答案 0 :(得分:2)
我认为问题在于这两行:
<form action="registration" method="post">
在您的html页面中。
您的web.xml中的 <url-pattern>/Registration</url-pattern>
您的操作设置为registration
,您的网址模式需要Registration
(请注意,首都R
和小r
)。
尝试更改它们(两者都相同),它应该可以正常工作
答案 1 :(得分: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”(返回值)。
希望它有所帮助!