我需要有关servlet过滤器的帮助。
目前我们有针对Oracle SSO的ChangePassword servlet。我想拦截对这个servlet的任何调用,如果我的检查失败,我会尝试将响应返回给发送者(更改密码jsp)。另外,如果我的所有检查都没问题,我会将请求传递给ChangePassword servlet。 此过滤器的一个要求是保留原始参数,如果我的任何检查失败,则将它们重新发送回初始jsp页面。
目前的doFilter方法是:
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException,
ServletException {
// __TB__ 2009_11_06 added for customization
String str_old_password = null;
String str_new_password = null;
String str_new_password_confirm = null;
String str_action = null;
String str_user = request.getParameterValues("p_username")[0];
// __TB__ 2009_11_06 added for customization
// Get the old password value
try {
str_old_password = request.getParameterValues("p_old_password")[0];
} catch (Exception e) {
str_old_password = "";
logger.error("Problem s dohvaćanjem starog passworda za username: " +
str_user);
throw new IOException();
}
// Get the new password
try {
str_new_password = request.getParameterValues("p_new_password")[0];
} catch (Exception e) {
str_new_password = "";
logger.error("Problem s dohvaćanjem novog passworda za username: " +
str_user);
throw new IOException();
}
// Get the new password confrimantion string
try {
str_new_password_confirm =
request.getParameterValues("p_new_password_confirm")[0];
} catch (Exception e) {
str_new_password_confirm = "";
logger.error("Problem s dohvaćanjem potvrde novog passworda za username: " +
str_user);
throw new IOException();
}
// Get the action string
try {
str_action = request.getParameterValues("p_action")[0];
} catch (Exception e) {
str_action = "OK";
}
if (logger.isDebugEnabled()) {
logger.debug("Ulazak u RuPassFilter-doFilter za username: " +
str_user);
}
// Prevent update if passwords are not equal and action is not equal OK
if (str_new_password.toString().equals(str_new_password_confirm.toString()) &&
str_action.equals("OK")) {
try {
if (logger.isDebugEnabled()) {
logger.debug("Novi password i njegova potvrda odgovaraju jedno drugom za username: " +
str_user);
}
//Run update and on any exception pass request to password change servlet except on MyException
MyChecker mc = new MyChecker();
if (logger.isDebugEnabled()) {
logger.debug("Poziv check metode u RuPassFilter-u za username: " +
str_user);
}
mc.check (str_user, str_old_password, str_new_password,
_filterConfig.getServletContext());
if (logger.isDebugEnabled()) {
logger.debug("Odrađena check metoda u RuPassFilter-u za username: " +
str_user);
}
//pass to servlet
chain.doFilter(request, response);
} catch (UtilException ue) {
logger.error("Dogodio se UtileException za username: " +
str_user);
logger.error(ue.getLocalizedMessage());
chain.doFilter(request, response);
} catch (NamingException ne) {
logger.error("Dogodio se NamingException za username: " +
str_user);
logger.error(ne.getLocalizedMessage());
chain.doFilter(request, response);
} catch (IOException ioe) {
logger.error("Dogodio se IOException u RuPassFilter-u za username: " +
str_user);
logger.error(ioe.getLocalizedMessage());
throw ioe;
} catch (MyException me) {
logger.error("Dogodio se MyException u RuPassFilter-u za username: " +
str_user);
logger.error(me.getLocalizedMessage());
//!!!!!!!!!!!!
// At this point I need return response back to sender !!!!!!!!!!!!
//!!!!!!!!!!!!!
}
} else {
if (logger.isDebugEnabled()) {
logger.debug("Passwordi ne odgovraju jedna drugom ili je action različit od OK u RuPassFilter-u za username: " +
str_user);
}
//!!!!!!!!!!!!
// At this point I need return response back to sender !!!!!!!!!!!!
//!!!!!!!!!!!!!
}
// Everything is OK
chain.doFilter(request, response);
}
如果我的任何检查失败,MyCheck.check方法会抛出MyException。
我们的J2EE服务器是OC4J版本:10.1.2.0.2
答案 0 :(得分:4)
您可以使用RequestDispatcher
RequestDispatcher rd = request.getRequestDispatcher("path_for_initial_jsp_page");
try{
MyCheck.check();
}catch(MyException me){
logger.error(me.getLocalizedMessage());
rd.include(request, response);
}
调用RequestDispatcher.forward(): 一旦您将来自Servlet A的请求转发到任何其他Servlet / JSP控件,就会从Servlet A转移到转发的patrty&对于该请求,它永远不会返回给A.
RequestDispatcher.include(): 在包含你正在做的是如果Servlet A(上面的例子)包含其他Servlet / JSP(比如B或B.jsp)的响应那么暂时控制转到B或B.jsp(他们会生成响应)控制来回到A&生成的响应在A的响应中添加。