2012-02-17 54 views
1

我創建了一個Web應用程序,編輯數據庫...頁面重定向... Netbeans的

我想知道我怎麼加或點擊提交後重定向的頁面。

對不起,我還在學習...

目前即時通訊使用「useBean的」插入表格到數據庫中的內容。我想知道如果如何重定向頁面在字段中輸入的所有信息,然後點擊提交後..

感謝

下面的代碼:

<%@ page language="Java" import="java.sql.*" %> 


<head> 
    <link rel="stylesheet" type="text/css" href="style.css"> 
</head> 
<html> 
    <head><title>CSN Survey</title></head> 


<body bgcolor="#ffffff"> 

<div> 
    <img id="title" src="images/CSN.gif" width="243" height="27" alt="CSN"/> 
    <img id="logo" src="images/tr_logo_40.gif" width="178" height="40" alt="tr_logo_40"/> 
</div> 
    <hr> 
    <h1> insert comment </h1> 

<div id="container"> 

    <form action="" name="form1" method="POST"> 
     <br> 
     <br> 
     <br> 

     <td><br>Write your comment here:</td> 

       <div id="q1" 

       <td>id:<%=request.getParameter("id")%></td> 
       <td>First name:<textarea name="first_name" rows="1" cols="10"></textarea></td> 
       <td>Last name:<textarea name="last_name" rows="1" cols="10"></textarea></td> 
       <br> 
     <br> 
       </div> 

       <td> 


       <input type = "submit" value="Submit"> 
       </td> 

    </form> 
    </div> 

       <jsp:useBean id="survey" class="csnsurveysource.csnsurveyclass" scope="page"> 
      <jsp:setProperty name="survey" property="*"/> 


     </jsp:useBean> 

    <% survey.insert();%> 

</body> 
</html> 

csnsurveyclass.java

package csnsurveysource; 
import java.io.*; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.Statement; 
import java.sql.ResultSet; 


public class csnsurveyclass 
{ 

    private int id; 
    private String first_name; 
    private String last_name; 

    private Connection connection=null; 
    private ResultSet rs = null; 
    private Statement st = null; 
    String connectionURL = "jdbc:postgresql://localhost:5432/test"; 


    public csnsurveyclass() 
    { 
     try { 
      // Load the database driver 
      Class.forName("org.postgresql.Driver"); 
      // Get a Connection to the database 
      connection = DriverManager.getConnection(connectionURL, "postgres", "qqQQ11!!"); 
      }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 setfirst_name(String first_name) 
    { 
     this.first_name = first_name; 
    } 

    public String getfirst_name() 
    { 
     return (this.first_name); 
    } 
     public void setlast_name(String last_name) 
    { 
     this.last_name = last_name; 
    } 

    public String getlast_name() 
    { 
     return (this.last_name); 
    } 


    public void insert() 
    { 

     try 
     { 
      String sql = "update testing set fname = '"+first_name+"',lname = '"+last_name+"' where id = "+id+""; 
      Statement s = connection.createStatement(); 
      s.executeUpdate (sql); 
      s.close(); 

     } 
     catch(Exception e){ 
      System.out.println("Exception is ;"+e); 
        } 
    } 
    } 

回答

2

這將是很好用MVC模式,您可以將業務邏輯

survey.insert(); 

到控制器,然後發送重定向

response.sendRedirect("..."); 
+0

您好e鋅感謝您的迴應。我會在哪裏做到這一點 – toink 2012-02-17 08:58:42

1

放在哪裏要在行動重定向你的窗體標記內

<form action="put here ur action/url" name="form1" method="POST"> 
+0

的問題,如果我做的是,該字符我在文本字段中輸入的內容不會轉發到數據庫。未執行的SQL語句。 – toink 2012-02-17 08:42:10