我有一个jsp注册表单,在提供有效输入和成功验证时,应该指向WEB-INF / WebPages.Am中的Success.jsp,使用DTO将该jsp传递给servlet-服务和DAO然后数据将被插入数据库内。但是现在注册成功后如何将其重定向到successPage。
以下是我的源代码:
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) {}
}
}
}
我在afs.createAffiliate(af)之后尝试在AffiliateServlet中使用RequestDispatcher;但仍无法重定向
请有人在这方面帮助我......
答案 0 :(得分:1)
尝试
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);
阅读此topic,这将澄清您的所有疑虑。 =)
答案 1 :(得分:1)
您可以使用三种方法从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");
最佳方式是使用请求调度程序,它只执行一次往返。