簡體   English   中英

轉發/重定向提交jsp

[英]forward/redirect on submit jsp

我在表單上獲取了所有參數,然后將它傳遞給servlet,然后將其提交給數據庫...。

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

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


</jsp:useBean>

 <%survey.insert();%>

但是我希望在用戶單擊提交后將其轉發到另一個頁面。

這是我的servlet

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

        }
    }



}

在您的servlet中,在公共無效插入中,我將在try and catch之后放置一個重定向,例如:

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

您可以根據需要和要求使用請求分派器和重定向。

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

rd.forward(request,response);

要么

response.sendRedirect("url");

sendRedirect()將標頭發送回瀏覽器,該標頭包含要重定向到的資源的名稱。

forward()動作發生在服務器內,而瀏覽器卻不知道。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM