Reputation: 242
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...
Upvotes: 0
Views: 3105
Reputation: 2647
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.
Upvotes: 1
Reputation: 19533
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. =)
Upvotes: 1