2014-09-24 78 views
0

我寫過這段代碼,但是當我在服務器上運行它時,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> 

我是新手程序員

+0

像我說的我是一個新手在這.....糾正我,如果我錯了 – user3725251 2014-09-24 20:09:48

+0

明白了.....它說服務器類型:狀態報告 message/reg description請求的資源不可用。 – user3725251 2014-09-24 20:17:31

+0

你可以幫忙,如果你可以解決它 – user3725251 2014-09-24 20:23:14

回答

0

正如已經BalusC指出你不應該使用

<input type="button" name="btn1" value="OK"> 

<input type="submit" name="btn1" value="OK"> 
action="/reg" /好好嘗試一下

也代表了項目結構的根,但更像服務器結構,因此action="/reg"將代表類似

http://server.addres/reg 

,但你很可能希望它代表

http://server.addres/web-app-name/reg 

因此,也許它更改爲

  • action="reg"從相同位置的形式表示reg
  • 或者加入你的應用程序名稱之前像action="/web-app-name/reg",
    (如果您使用的是JSP,您可以通過動態獲取您的應用程序的名稱EL(Expression Language)action="${pageContext.request.contextPath}/reg"
+0

謝謝你的幫助 – user3725251 2014-09-24 20:20:57

+0

@ user3725251不客氣:) – JEENoob 2014-09-24 20:23:37

+0

你有什麼好的資源,你可以推薦我從@JEENoob學習 – user3725251 2014-09-24 20:30:32