返回信息流感谢抽空看这个求助!
出现问题:
HTTP Status 404 - /sb/Registration
type Status report
message /sb/Registration
description The requested resource (/sb/Registration) is not available.
Apache Tomcat/6.0.30
不懂是哪步错了。估计可能是web.xml没写好。
我的流程是:在Tomcat6.0目录下,webapps目录下新建sb这个文件夹,这个文件夹里又有classes文件夹以及Registration.html和web.xml。我将Registration.class,Registration$Student.class放入classes中。
这是我的Registration.java:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class Registration extends HttpServlet{
private PreparedStatement pstmt;
public void init() throws ServletException{
initializeJdbc();
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String lastName=request.getParameter("lastName");
String firstName=request.getParameter("firstName");
String password=request.getParameter("password");
String email=request.getParameter("email");
Student student=new Student(lastName,firstName,password,email);
HttpSession httpSession=request.getSession();
httpSession.setAttribute("student", student);
out.println("You entered the following data:");
out.println("<p>Last name:"+lastName);
out.println("<p>First name:"+firstName);
out.println("<p>Password:"+password);
out.println("<p>Email:"+email);
out.println("<p>form method=\"post\" action=/sb/Registration>");
out.println("<p><input type=\"submit\" value=\"Confirm\">");
out.println("</form>");
out.close();
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession httpSession = request.getSession();
Student student = (Student)(httpSession.getAttribute("student"));
try{
storeStudent(student);
out.println(student.lastName+" "+student.firstName+"is now registered in the database");
out.close();
}
catch(Exception ex){
out.println("Error:"+ex.getMessage());
return;
}
}
private void initializeJdbc(){
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded");
Connection conn=DriverManager.getConnection("jdbc:MYSQL://localhost/test");
System.out.println("Database connected");
pstmt = conn.prepareStatement("insert into user(lastName,firstName,password,email)values(?,?,?,?)");
}
catch(Exception ex){
System.out.println(ex);
}
}
private void storeStudent(Student student) throws SQLException{
pstmt.setString(1,student.getLastName());
pstmt.setString(2,student.getFirstName());
pstmt.setString(3,student.getPassword());
pstmt.setString(4,student.getEmail());
pstmt.executeUpdate();
}
class Student{
private String lastName="";
private String firstName="";
private String password="";
private String email="";
Student(String lastName,String firstName,String password,String email){
this.lastName=lastName;
this.firstName=firstName;
this.password=password;
this.email=email;
}
public String getLastName(){
return lastName;
}
public String getFirstName(){
return firstName;
}
public String getPassword(){
return password;
}
public String getEmail(){
return email;
}
}
}
这是我的xml:
<web-app>
<servlet>
<servlet-name>Registration</servlet-name>
<servlet-class>Registration</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Registration</servlet-name>
<url-pattern>/Registration</url-pattern>
</servlet-mapping>
</web-app>
这是我的Registration.html:
<html>
<head>
<title>Registration</title>
</head>
<body>
Please register.
<form method="get" action ="http://localhost:8080/sb/Registration">
<p>Last Name <font color = "#FF0000">*</font>
<input type = "text" name = "lastName" >
First Name <font color = "#FF0000">*</font>
<input type = "text" name = "firstName" >
</p>
<p>Password
<input type = "text" name = "password" >
Email
<input type = "text" name = "email" >
</p>
<p><input type = "submit" name = "Submit" value="Submit" >
<input type = "reset" value="Reset" >
</p>
</form>
</body>
</html>
这是一条镜像帖。来源:北邮人论坛 / java / #19665同步于 2011/8/10
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Java机器人发帖
求助啊。HTTP Status 404错误
czxttkl
2011/8/10镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
你在浏览器地址栏中输入的是啥 是不是http://localhost:8080/sb/Registration.html ?
是不是应该建立一个WEB-INF文件夹?把classes和web.xml都放进去
恩。谢谢啊。新手不大熟悉这个流程。
【 在 lookstar 的大作中提到: 】
: 你在浏览器地址栏中输入的是啥 是不是http://localhost:8080/sb/Registration.html ?
: 是不是应该建立一个WEB-INF文件夹?把classes和web.xml都放进去
: --
: ...................