2011-12-14 13 views
0

可能性の重複:
Calling servlet from HTML form, but servlet is never invokedHTMLフォームから呼び出すサーブレット、ないサーブレットからの応答

私はHTMLフォームからサーブレットを呼んでいる、サーブレットは、フォームデータを取り、それが挿入されますそのフォームデータをdatabase.But私は送信ボタンをクリックしてエラーページが来ている。私のサーブレットコードで何が間違っているのを助けてください。

私のhtmlコード:

<html> 
    <head> 
     <title> Sign Up </title> 
    </head> 
    <body> 
     <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> 
    </body> 
</html> 

私のサーブレットコード:

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); 
      } 
     } 
    } 
} 

私の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> 
</web-app> 
+1

マルチポストの質問に行くなら、少なくともコードとHTMLのフォーマット方法を学んでください。あなたの投稿のすべてを編集することは、すぐに面倒になるでしょう! –

答えて

1

私が思うに、間違いはここにある:<form action="servlet/Loginservlet"

試してみる<form action="/<your-app-name>/login"

+0

アプリ名はどういう意味ですか? –

+0

私はMyeclipse IDEを使用しています –

+0

@krishna bhargavi: 'your-app-name'はWebアプリケーションのコンテキストパスです。 – home

0

フォームアクションではservlet/Loginservletとなっています。あなたは

<url-pattern>/servlet/Loginservlet</url-pattern> 

ようweb.xmlで同じマップまたはあなたが上記のいずれかを行うことができます

action='login' 

のような形で変更する必要があります。

0

あなたサーブレットURLは

<url-pattern>/login</url-pattern> 

ローカルホストのWebアプリケーションを実行すると、その後

localhost:8080/test 

のようなURLは、テストが名前である。この

<form action="login" method="post" > 

を試してみてくださいWebアプリケーションを含むアプリケーション名をWebディレクトリに追加します。今、あなたは、インデックスファイルを作成し、それはあなたがあなたのログインページを開始page1.htmlから今

localhost:8080/test/page1.html 

のように、こののみと別のページのURLからインデックスファイルを実行すると仮定したサーブレットのため

localhost:8080/test/login 
であるように、リンクが見えます

asは、上記のような特定のサーブレットを呼び出すために設定する必要のある特定のサーブレットのurlパターンを定義します。このURLは、クライアントの実際のサーブレットを非表示にします。あなたは何でもURLパターンで設定できます

+0

それは来ていません... –

+0

このチュートリアルをチェックしてください。http://www.servletworld.com/servlet-tutorials/simple-servlet-exampleもし何か間違いがありましたら – Pratik

+0

あなたのウェブディレクトリ構造は何ですか?あなたはあなたのウェブディレクトリ構造の画像を完全に開いてすべてのサブフォルダとファイルを表示することができます – Pratik

関連する問題