2017-01-19 5 views
1

サーブレットプログラミングには新しいだけでなく、サーブレットプログラミングにもIDEを使用しています。 NetBeans 8.2をサーブレットプログラミング用にインストールしましたが、サーブレットプログラムの実行中に次のエラーが発生しました。 これは、これは私のサーブレットプログラムサーブレットの使用中にNetbeansエラーが発生しました

import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
public class CheckPass extends HttpServlet 
{ 
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException 
{ 
response.setContentType("text/html"); 
PrintWriter out=response.getWriter(); 
out.println("<form name='frm' method='post' action='/WT Practical 2/servlet/CheckPass'>"); 
out.println("UserName<input type='text' name='unm'>"); 
out.println(" Password<input type='password' name='psw'>"); 
out.println(" <input type='submit' value='Login'>"); 
out.println("</form>"); 
out.close(); 
} 
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException 
{ 
PrintWriter out=response.getWriter(); 
String unm = request.getParameter("unm"); 
String ps=request.getParameter("psw"); 

if(ps.equals("hello123")) 
out.println("Welcome "+unm); 
else 
out.println("invalid entry"); 
} 

} 

である私のindex.html

<html> 
    <head> 
     <title>TODO supply a title</title> 
     </head> 
     <body> 
      <h4>Click here to go to <a href="CheckPass">CheckPass Page</a></h4> 



    </body> 
</html> 

ですこれはこのサーブレットは、実用的な大学のために実行されているweb.xmlファイル

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> 
    <servlet> 
     <servlet-name>CheckPass</servlet-name> 
     <servlet-class>CheckPass</servlet-class> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>CheckPass</servlet-name> 
     <url-pattern>/CheckPass</url-pattern> 
    </servlet-mapping> 
    <welcome-file-list> 
     <welcome-file> index.html</welcome-file> 
    </welcome-file-list> 

</web-app> 

です私のプロジェクト名はWT Practical 2です。今、これは私が得るエラーです

HTTP Status 404 - /WT%20Practical%202/servlet/CheckPass 

type Status report 

message /WT%20Practical%202/servlet/CheckPass 

description The requested resource is not available. 

Apache Tomcat/8.0.27 

私はそれはので、私は行うことに期待何のフォームaction属性に

<form name='frm' method='post' action='/WT Practical 2/servlet/CheckPass'> 

をパスしたラインである疑いは、ユーザー名とパスワードを取ることを確認し、応じて適切な出力が得られていますパスワード。私を助けてください。私の疑いが正しいかどうかも教えてください。また、サーブレット・プログラムにhtmlコードを書くのは良い方法ではないと聞いています。また、上記のhtmlコードをindex.htmlファイルに書き込む方法を知りたいのですが、代わりにこのプログラムが動作するように動作するはずです。ありがとう。

答えて

1

はい、サーブレットにHTMLコードを書くのは良い方法ではありません。これをお読みくださいCoding style and recommendations

ここに基本コードがあります。別のファイルを作成しstart.htmlと呼ばれ、以下のコード

<html> 
    <body> 
    <a href="login.html">Goto Login</a> 
    </body> 
</html> 

を書いて、あなたのweb.xmlファイルを更新し、このよう

<welcome-file-list> 
    <welcome-file>start.html</welcome-file> 
</welcome-file-list> 

に別のファイルを作成しますlogin.htmlと呼ばれ、以下のコードを記述します。あなたのサーブレットすなわち、コードの下CheckPassクラスの書き込みで

<html> 
<body> 
    <form action="CheckPass" method="POST"> 
     <input type="text" name="unm" id="unm"/> 
     <input type="password" name="psw" id="psw"/> 
     <input type="submit" value="Login"/> 
    </form> 
</body> 

@Override 
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 

    String unm = req.getParameter("unm"); 
    String psw = req.getParameter("psw"); 

    PrintWriter out = resp.getWriter(); 

    if (unm.equals("someusername") && ps.equals("hello123")) { 
     out.println("Welcome " + unm); 
    } else { 
     out.println("invalid entry"); 
    } 
    out.close(); 
} 

このアプリケーションを実行します。ブラウザはstart.htmlとしてウェルカムページが表示されます。また、後藤ログインハイパーリンクをクリックしたとき、ページがより良く理解するためにlogin.html

にリダイレクトStackOverflowの下の文書に

  1. Working with doGet() in Java Servlet
  2. Working with doPost() in Java Servlet

を読みますこの回答はJSP/HTMLとサーブレットの基本的な接続に過ぎないことに注意してください。

+1

ありがとうございました。これは私の仕事です。私は今htmlファイルを作成し、Webを更新する良いアイデアを持っています。それに応じてサーブレットプログラムを変更します。 –

関連する問題