2011-12-14 69 views
2

Iam從html表單調用servlet,servlet接受表單數據並將表單數據插入到數據庫中。但是當我單擊提交按鈕時,錯誤頁面會到來。請幫助我的servlet代碼有什麼問題。從HTML表單調用servlet,但從不調用servlet

我的servlet代碼:

import javax.servlet.http.HttpServlet; 

import java.io.*; 
import java.sql.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 




public class Loginservlet extends HttpServlet { 

    public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException 
    { 
    System.out.println("login servlet"); 
    String connectionURL = "jdbc:mysql://localhost:3306/mysql"; 
    Connection connection=null; 
    res.setContentType("text/html"); 
    PrintWriter out = res.getWriter(); 
    String username= req.getParameter("username"); 
    String password = req.getParameter("password"); 
    try { 
    Class.forName("com.mysql.jdbc.Driver"); 
    connection = DriverManager.getConnection(connectionURL, "root", "root"); 
    String sql = "insert into signup values (?,?)"; 
    PreparedStatement pst = connection.prepareStatement(sql); 
    pst.setString(1, username); 
    pst.setString(2, password); 

    int numRowsChanged = pst.executeUpdate(); 
    out.println(" Data has been submitted "); 

    pst.close(); 
    } 
    catch(ClassNotFoundException e){ 
    out.println("Couldn't load database driver: "+ e.getMessage()); 
    } 
    catch(SQLException e){ 
    out.println("SQLException caught: " + e.getMessage()); 
    } 
    catch (Exception e){ 
    out.println(e); 
    } 
    finally { 
    try { 
    if (connection != null) 
     connection.close(); 
    } 
    catch (SQLException ignored){ 
    out.println(ignored); 
    } 
    } 
    } 
} 

我的html代碼:

會員註冊

 <form action="servlet/Loginservlet" method="post" > 

       <font size='5'>Create your Account:</font><br/><br> 

        <label for="username" accesskey="u" style="padding-left:3px;">User Name: </label> 

            <input type="text" style="background-color:#ffffff;margin-left:14px;padding-top:7px;border-width:0px;margin-top:6px;padding-right:85px;" id="username" name="username" tabindex="1"><br/><br> 

        <label for="password" accesskey="p" style="padding-left:4px;">Password: </label> 

            <input type="password" style="background-color:#ffffff;margin-left:14px;padding-top:7px;border-width:0px;padding-right:85px;" id="password" name="pasword" tabindex="2"><br/><br> 


        <input type="submit" value="Submit" style="margin-left:164px;"/> 

        <input type="reset" value="Reset" style="margin-left:17px;"/> 

    </form> 

web.xml文件:

    <?xml version="1.0" encoding="UTF-8"?> 
         <web-app version="2.5" 
         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_2_5.xsd"> 

          <servlet> 
    <servlet-name>login</servlet-name> 
    <servlet-class>Loginservlet</servlet-class> 
          </servlet> 
         <servlet-mapping> 
    <servlet-name>login</servlet-name> 
    <url-pattern>/login</url-pattern> 
         </servlet-mapping> 

請幫助你的項目

+3

你也應該發佈你使用過的`web.xml`文件 – fyr 2011-12-14 11:39:35

+1

哪個錯誤出現的片段?並且在控制檯中是否有任何異常? – Piscean 2011-12-14 11:40:55

+0

作爲fyr點,你必須在一個URL上註冊servlet,以便服務器使用它。如果你已經完成了,檢查控制檯/日誌以查看在加載servlet類/實例化它時是否有一些異常。 – SJuan76 2011-12-14 11:42:12

回答

1

檢查web.xml文件,你必須有registrate你的servlet。 check this

使用<form action="/login" method="post" >在HTML 和

在web.xml

<servlet-class>your.class.package.Loginservlet</servlet-class> 
          </servlet> 
1

當你看着你的servlet類,沒有package定義,這是必需的。並在<servlet-class/>標記中將該類與包(意爲完全限定名稱)映射。

另一件事是你設置的動作爲url servlet/LogininServlet,但給出不同的網址<url-pattern/>標記,這是錯誤的。你可以簡單地將表單動作設置爲login

0

一切都很好....在html頁面中使用action =「./ login」.........它會工作我已經做了相同的