我寫過這段代碼,但是當我在服務器上運行它時,onsubmit表單沒有提供任何反應。 我在使用TOMCAT服務器的Eclipse IDE中進行編碼。 我不知道是什麼問題.....我的html沒有提交到servlet
servlet代碼
package webpackage;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
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 mypackage.Connecting;
//import com.sun.corba.se.pept.transport.Connection;
/**
* Servlet implementation class Registration
*/
@WebServlet(name = "reg", urlPatterns = { "/reg" })
public class Registration extends HttpServlet {
private static final long serialVersionUID = 1L;
public static Connection con=null;
/**
* @see HttpServlet#HttpServlet()
*/
public Registration() {
super();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
try{
String username = request.getParameter("user");
String email = request.getParameter("email");
String institute = request.getParameter("institute");
String pass1 = request.getParameter("pass1");
String pass2 = request.getParameter("pass2");
pw.println(username);
pw.println(email);
pw.println(institute);
pw.println(pass1);
pw.println(pass2);
con= Connecting.getConn();
pw.println(con);
PreparedStatement pst = con.prepareStatement("insert into Validate_login values(?,?)");
pst.setString(1,"112hhsh");
pst.setString(2,pass1);
int i = pst.executeUpdate();
PreparedStatement pst1=con.prepareStatement("insert into User_info values(?,?,?,?)");
pst1.setString(1,"112hhsh");
pst1.setString(2,username);
pst1.setString(3,institute);
pst1.setString(4,email);
i=pst1.executeUpdate();
System.out.println(i);
if(i!=0){
pw.println("<br>Record has been inserted");
}
else{
pw.println("failed to insert the data");
}
}
catch (Exception e){
pw.println(e);
}
}
}
HTML代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Register</title>
</head>
<body>
<form action="/reg" method="post">
Username:<input type="text" name="user"><br>
Email:<input type="text" name="email"><br>
Institute:<input type="text" name="institute"><br>
Password:<input type="text" name="pass1"><br>
Confirm Password:<input type="text" name="pass2"><br>
<input type="button" name="btn1" value="OK"><br>
</form>
</body>
</html>
XML代碼
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Sample</display-name>
<welcome-file-list>
<welcome-file>register.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>reg</servlet-name>
<servlet-class>webpackage.Registration</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>reg</servlet-name>
<url-pattern>/reg</url-pattern>
</servlet-mapping>
</web-app>
我是新手程序員
像我說的我是一個新手在這.....糾正我,如果我錯了 – user3725251 2014-09-24 20:09:48
明白了.....它說服務器類型:狀態報告 message/reg description請求的資源不可用。 – user3725251 2014-09-24 20:17:31
你可以幫忙,如果你可以解決它 – user3725251 2014-09-24 20:23:14