0

i have a jsp registration form which on providing valid inputs and on successful validation should be directed to Success.jsp in WEB-INF/WebPages.Am passing this jsp to a servlet- service and DAO using a DTO and then the data will be inserted inside the database. But now after successful registration how to redirect it to the successPage.

Following are my source codes:

AffiliateServlet

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

}

AffiliateService

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;

}


}

AffiliateDAO

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) {}
        }
    }
}

I tried using RequestDispatcher in AffiliateServlet after afs.createAffiliate(af); but still unable to redirect

Please some one help me in this regard...

user3222718
  • 182
  • 1
  • 6
  • 22

2 Answers2

1

Try

response.sendRedirect("RedirectIfSuccessful.jsp");

Also move jsp outside of the WEB-INF folder, try to move to /WebPages/success.jsp and use

response.sendRedirect("WebPages/success.jsp");

Try after afs.createAffiliate(af)

If you dont want to move your jsp try this.

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

And read this topic, this will clarify all your doubts. =)

Community
  • 1
  • 1
Koitoer
  • 16,802
  • 7
  • 55
  • 79
  • Where should i write this code can u elaborate only after the data is added to the database it should redirect me to Success.jsp...And i cant move jsp out of WEB-INF due to security issues.... – user3222718 Feb 07 '14 at 08:13
  • You cannot redirect to jsp pages under WEB-INF directory. You can however forward request to this particular jsp through Servlet(Or, another jsp pages located outside WEB-INF directory). If problem is security you should avoid that unathenticated users access those resources. This question is very similar http://stackoverflow.com/questions/10466256/is-it-possible-to-use-sendredirect-to-redirect-to-a-page-in-a-sub-directory – Koitoer Feb 07 '14 at 08:15
  • or can i use dispatcher?? – user3222718 Feb 07 '14 at 08:20
  • Yes there is an update on my response, check it there. – Koitoer Feb 07 '14 at 08:20
  • 1
    thank you its redirecting some refresh problem was there on the page i suppose i closed the app n freshly opened and executed its working fine thank u...... and thanks for all the links i learnt a lot from those thank u for the precious time.... – user3222718 Feb 07 '14 at 09:26
1

You can use three ways to redirecting from servlet.

1) Request Dispatcher :-

RequestDispatcher dispatcher = getRequestDispatcher("jsp/servlet Page");
dispatcher.forward( request, response );

2) Use Response send Redirect Method.

 HttpResponce.sendRedirect("jsp/servlet Page");

3) Set Header Location :-

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

The Best Way is to use the Request Dispatcher, it perform in only one round trip.

Mitul Maheshwari
  • 2,557
  • 4
  • 20
  • 38
  • Thank u i got my answer and the question has been closed 22 mins ago anyways thank u for trying my question – user3222718 Feb 07 '14 at 09:55
  • god u down voted thinking am being rude to you?? I was typing by mistake i submitted later it took time to edit the post didnt meant to be rude nyways – user3222718 Feb 07 '14 at 10:07