2012-03-12 8 views
0

を提出...それが正常に動作しているのリダイレクト...前方/私はすべてのパラメータが、データベースに提出してサーブレットを渡す得るために、私のフォーム上でこれを持っているJSP

<jsp:useBean id="survey" class="csnsurveysource.csnsurveyclass" scope="page"> 

    <jsp:setProperty name="survey" property="*"/> 


</jsp:useBean> 

<%survey.insert();%> 

しかし、私はそれをしたいです提出...

上のユーザーのクリックがここに私のサーブレットは、サーブレットで

package csnsurveysource; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.Statement; 
import java.sql.ResultSet; 



public class csnsurveyclass 
{ 

    private int id; 
    private String requested_by; 
    private String assigned_to; 
    private String question1; 
    private String question2; 
    private String question3; 
    private String question4; 
    private String comment1; 
    private String comment2; 
    private String comment3; 
    private String comment4; 
    private String comment5; 
    private Connection connection=null; 
    private ResultSet rs = null; 
    private Statement st = null; 
    String connectionURL = "jdbc:postgresql://localhost:5432/MyDB"; 


    public csnsurveyclass() 
    { 
     try { 
      // Load the database driver 
      Class.forName("org.postgresql.Driver"); 
      // Get a Connection to the database 
      connection = DriverManager.getConnection(connectionURL, "username", "password"); 
      }catch(Exception e){ 
      System.out.println("Exception is ;"+e); 
      } 

     } 

     public void setid(int id) 
    { 
     this.id = id; 
    } 

    public int getid() 
    { 
     return (this.id); 
    } 
     public void setrequested_by(String requested_by) 
    { 
     this.requested_by = requested_by; 
    } 

    public String getrequested_by() 
    { 
     return (this.requested_by); 
    } 

    public void setassigned_to(String assigned_to) 
    { 
     this.assigned_to = assigned_to; 
    } 

    public String getassigned_to() 
    { 
     return (this.assigned_to); 
    } 

     public void setquestion1(String question1) 
    { 
     this.question1 = question1; 
    } 

    public String getquestion1() 
    { 
     return (this.question1); 
    } 
     public void setquestion2(String question2) 
    { 
     this.question2 = question2; 
    } 

    public String getquestion2() 
    { 
     return (this.question2); 
    } 
     public void setquestion3(String question3) 
    { 
     this.question3 = question3; 
    } 

    public String getquestion3() 
    { 
     return (this.question3); 
    } 
     public void setquestion4(String question4) 
    { 
     this.question4 = question4; 
    } 

    public String getquestion4() 
    { 
     return (this.question4); 
    } 
     public void setcomment1(String comment1) 
    { 
     this.comment1 = comment1; 
    } 

    public String getcomment1() 
    { 
     return (this.comment1); 
    } 
     public void setcomment2(String comment2) 
    { 
     this.comment2 = comment2; 
    } 

    public String getcomment2() 
    { 
     return (this.comment2); 
    } 
     public void setcomment3(String comment3) 
    { 
     this.comment3 = comment3; 
    } 

    public String getcomment3() 
    { 
     return (this.comment3); 
    } 
     public void setcomment4(String comment4) 
    { 
     this.comment4 = comment4; 
    } 

    public String getcomment4() 
    { 
     return (this.comment4); 
    } 
     public void setcomment5(String comment5) 
    { 
     this.comment5 = comment5; 
    } 

    public String getcomment5() 
    { 
     return (this.comment5); 
    } 

    public void insert() 
    { 

     try 
     { 
      String sql = "insert into csnsurvey (id,assigned_to,requested_by,q1,comment1,q2,comment2,q3,comment3,q4,comment4,comment5) values('"+id+"', '"+assigned_to+"','"+requested_by+"','"+question1+"','"+comment1+"','"+question2+"','"+comment2+"','"+question3+"','"+comment3+"','"+question4+"','"+comment4+"','"+comment5+"')"; 
      Statement s = connection.createStatement(); 
      s.executeUpdate (sql); 
      s.close();     


     }catch(Exception e){ 
      System.out.println("Error on the database"); 

     } 
    } 



} 
+0

質問が不明です。送信を処理するサーブレットでは、リダイレクトまたは転送を行います。 –

+0

サーブレットコードの代わりにBeanを貼り付けたようです。あなたのサーブレットコード –

+0

Peeweeを表示してください。**サーブレットではありません**。それはただのjavabeanです。実際にサーブレットがどのようなものかを知るには、http://stackoverflow.com/tags/servlets/infoをチェックしてください。あなたのコードはDBリソースを漏らしており、SQLインジェクション攻撃を受けやすいです。私はそれが「ちょうど」宿題であることを望みますか? – BalusC

答えて

1

だ後に別のページに転送される、公共ボイド挿入で、私は、tryおよびcatchの後に何かをリダイレクトを置きますlike:

String redirectURL = "http://hostname.com/"; 
response.sendRedirect(redirectURL); 
+0

OPはサーブレットを使用していません。 OPはlegacy/oldschoolの 'jsp:useBean'を使用しています。 – BalusC

+0

良いコール@BalusC。いずれにせよ、単純なリダイレクトはこの場合は正常に動作するはずです。うまくいけばjsp:usebeanはテストの目的のためだけです。 –

0

リクエストディスパッチャを使用して、必要と要件に応じてリダイレクトすることができます。

ServletContext sc = getServletContext(); 
RequestDispatcher rd = sc.getRequestDispatcher("url"); 

rd.forward(request,response); 

又は

response.sendRedirect("url"); 

のsendRedirect()先頭にリダイレクトされるリソースの名前が含まれているブラウザにヘッダを送信します。

forward()アクションは、ブラウザを知らずにサーバー内で発生します。

関連する問題