提交jsp的转发/重定向

时间:2012-03-12 16:30:04

标签: java 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");

        }
    }



}

2 个答案:

答案 0 :(得分:1)

在你的servlet中,在public void insert中,我会在try和catch之后进行重定向,例如:

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

答案 1 :(得分:0)

您可以根据需要和要求使用请求调度程序和重定向。

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

rd.forward(request,response);

response.sendRedirect("url");

sendRedirect()将标头发送回浏览器,其中包含要重定向到的资源的名称。

forward()动作在服务器内发生,不知道浏览器。