Java 将我的jsp表单重定向到成功页面

Java 将我的jsp表单重定向到成功页面,java,jsp,servlets,Java,Jsp,Servlets,我有一个jsp注册表单,在提供有效输入和成功验证时,应将其定向到WEB-INF/WebPages中的Success.jsp。我使用DTO将此jsp传递给servlet服务和DAO,然后将数据插入数据库中。但现在成功注册后,如何将其重定向到successPage 以下是我的源代码: 附属服务 Affiliate af= new Affiliate(); af.setFisrtName(request.getParameter("txtFname")); af.setLas

我有一个jsp注册表单,在提供有效输入和成功验证时,应将其定向到WEB-INF/WebPages中的Success.jsp。我使用DTO将此jsp传递给servlet服务和DAO,然后将数据插入数据库中。但现在成功注册后,如何将其重定向到successPage

以下是我的源代码:

附属服务

 Affiliate af= new Affiliate();
    af.setFisrtName(request.getParameter("txtFname"));
        af.setLastName(request.getParameter("txtLname"));
        af.setGender(request.getParameter("txtGender"));
        af.setCategory(request.getParameter("txtCategory"));
        String dob=(request.getParameter("txtDob"));

        try {
            SimpleDateFormat formatter=new SimpleDateFormat("MM/dd/yyyy");
            formatter.setLenient(false);
            Date date=formatter.parse(dob);
            af.setDate(date);
        } catch (ParseException e) {
           e.printStackTrace();
        }

        af.setAge(Integer.parseInt(request.getParameter("txtAge")));
        af.setAddress(request.getParameter("txtAddr"));
        af.setCountry("India");
        af.setState(request.getParameter("txtState"));
        af.setCity(request.getParameter("txtCity"));
        af.setPinCode(Integer.parseInt(request.getParameter("txtPin")));
        af.setEmailId(request.getParameter("txtEmail"));
        String std=request.getParameter("txtStd");
        int Std=Integer.parseInt(std);
        String con=request.getParameter("txtPhone");
        int contactNo=Integer.parseInt(con);
        af.setContactNo(Std+"-"+contactNo);
        String mob=request.getParameter("txtMobile");
        Long mobileNo=Long.parseLong(mob);
        af.setMobileNo("+91-"+mobileNo);

    AffiliateService afs=new AffiliateService();
    afs.createAffiliate(af);
request.getRequestDispatcher("/WEB-INF/WebPages/success.jsp").forward(request, response);
    }

}
附属服务

public class AffiliateService {
    Affiliate affiliate=null;


    public Affiliate createAffiliate( Affiliate affiliate) {
         validateAffiliate(affiliate);
        return affiliate;
            }


    private Affiliate validateAffiliate(Affiliate affiliate) {
        this.affiliate=affiliate;
         if(affiliate!=null){
       AffiliateDAO afd=new AffiliateDAO();
       DataSource dataSource=new DataSource();
       afd.setDataSource(dataSource);
        afd.insertAffiliate(affiliate);
    }
    return affiliate;

}


}
附属AO

public void insertAffiliate(Affiliate affiliate){
    String sql="INSERT INTO REGISTER " +"(id,FisrtName,LastName,Gender,Category,DateOfBirth,Age,Address,Country,State,City,PinCode,EmailId,ContactNo,MobileNo)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    Connection conn = null;

    try {
        conn = dataSource.createConnection();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, affiliate.getId());
        ps.setString(2, affiliate.getFisrtName());
        ps.setString(3, affiliate.getLastName());
        ps.setString(4,affiliate.getGender());
        ps.setString(5, affiliate.getCategory());
        ps.setDate(6, new java.sql.Date(affiliate.getDate().getTime()));
        ps.setInt(7, affiliate.getAge());
        ps.setString(8, affiliate.getAddress());
        ps.setString(9,affiliate.getCountry());
        ps.setString(10,affiliate.getState());
        ps.setString(11, affiliate.getCity());
        ps.setInt(12, affiliate.getPinCode());
        ps.setString(13, affiliate.getEmailId());
        ps.setString(14,affiliate.getContactNo());
        ps.setString(15, affiliate.getMobileNo());

        ps.executeUpdate();
        ps.close();

    } catch (SQLException e) {
        throw new RuntimeException(e);

    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {}
        }
    }
}
我尝试在afs.createAffiliate(af)之后在AffiliateServlet中使用RequestDispatcher;但仍然无法重定向

请找人帮我这方面的忙……试试看

response.sendRedirect("RedirectIfSuccessful.jsp");
同时将jsp移到WEB-INF文件夹之外,尝试移动到/WebPages/success.jsp并使用

response.sendRedirect("WebPages/success.jsp");
在afs.createAffiliate(af)之后重试

如果你不想移动你的jsp,试试这个

request.getRequestDispatcher("/WEB-INF/WebPages/success.jsp").forward(request, response);

阅读这篇文章,这将澄清你所有的疑问

您可以使用三种方法从servlet重定向。

1) 请求调度程序:-

RequestDispatcher dispatcher = getRequestDispatcher("jsp/servlet Page");
dispatcher.forward( request, response );
2) 使用响应发送重定向方法

 HttpResponce.sendRedirect("jsp/servlet Page");
3) 设置标题位置:-

 response.setHeader("Location","jsp/servlet Page");

最好的方法是使用请求调度程序,它只在一次往返中执行。

我应该在哪里编写此代码?只有在将数据添加到数据库后,它才应该将我重定向到Success.jsp…并且由于安全问题,我无法将jsp移出WEB-INF…您无法重定向到WEB-INF目录下的jsp页面。但是,您可以通过Servlet(或者位于WEB-INF目录之外的另一个jsp页面)将请求转发给这个特定的jsp。若问题是安全性,那个么应该避免未经身份验证的用户访问这些资源。这个问题非常相似,或者我可以使用dispatcher吗?是的,我的回复中有更新,请在那里检查。谢谢它重定向页面上有一些刷新问题我想我关闭了应用程序n新打开并执行了它的工作正常谢谢你。。。。。。感谢所有的链接,我从他们那里学到了很多,谢谢你宝贵的时间……谢谢你,我得到了答案,问题22分钟前就结束了,不管怎样,谢谢你尝试我的问题上帝,你否决了,认为我对你很粗鲁??我错打了,我后来提交了。编辑这篇文章花了些时间。这并不意味着粗鲁