creating a custom RequestProcessor, we will change our sample application to implement these two business requirements:
- We want to create a
ContactImageActionclass that will generate images instead of a regular HTML page. - Before processing every request, we want to check that user is logged in by checking for
userNameattribute of the session. If that attribute is not found, we will redirect the user to the login page.
- Create your own
CustomRequestProcessorclass, which will extend theRequestProcessorclass, like this:
In the
public class CustomRequestProcessor
extends RequestProcessor {
protected boolean processPreprocess (
HttpServletRequest request,
HttpServletResponse response) {
HttpSession session = request.getSession(false);
//If user is trying to access login page
// then don't check
if( request.getServletPath().equals("/loginInput.do")
|| request.getServletPath().equals("/login.do") )
return true;
//Check if userName attribute is there is session.
//If so, it means user has allready logged in
if( session != null &&
session.getAttribute("userName") != null)
return true;
else{
try{
//If no redirect user to login Page
request.getRequestDispatcher
("/Login.jsp").forward(request,response);
}catch(Exception ex){
}
}
return false;
}
protected void processContent(HttpServletRequest request,
HttpServletResponse response) {
//Check if user is requesting ContactImageAction
// if yes then set image/gif as content type
if( request.getServletPath().equals("/contactimage.do")){
response.setContentType("image/gif");
return;
}
super.processContent(request, response);
}
}processPreprocessmethod of ourCustomRequestProcessorclass, we are
checking for theuserNameattribute of the session and if it's not found,
redirect the user to the login page.
For our requirement of generating images as output from theContactImageActionclass,
we have to override theprocessContentmethod and first check if the request is
for the/contactimagepath. If so, we set thecontentTypetoimage/gif; otherwise,
it'stext/html...More Details
No comments:
Post a Comment