We will change our sample application in two steps to implement the content type and user login in.
1. Create your own CustomRequestProcessor class, which will extend the RequestProcessor class, like :
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);
}
}
In the processPreprocess method of our CustomRequestProcessor class, we are checking for the userName attribute 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 the ContactImageAction class, we have to override the processContent method and first check if the request is for the /contactimage path. If so, we set the contentType to image/gif; otherwise, it's text/html.
2. Add these lines to your struts-config.xml file after the <\action-mapping> element to inform Struts that CustomRequestProcessor should be used as the RequestProcessor class:
<\controller>
<\set-property property="processorClass"
value="com.sample.util.CustomRequestProcessor"/>
Please note that overriding processContent() is OK if you have very few Action classes where you want to generate output whose contentType is something other than text/html. If that is not the case, you should create a Struts sub-application for handling requests for image-generating Actions and set image/gif as the contentType for it.
The Tiles framework uses its own RequestProcessor for decorating output generated by Struts.
ActionServlet
If you look into the web.xml file of your Struts web application, it looks like this:
<\web-app >
<\servlet>
<\servlet-name>action=
<\servlet-class>org.apache.struts.action.ActionServlet
<\servlet-mapping>
<\servlet-name>action
<\url-pattern>*.do
That means ActionServlet is responsible for handling all of your requests to Struts. You can create a sub-class of the ActionServlet class if you want to do something at application startup or shutdown or on every request, but you should try creating a PlugIn or RequestProcessor before extending the ActionServlet class.
On the other hand, the decision to extend Struts should not be taken lightly. If you put some low-performance code in your RequestProcessor class, it will execute on every request and can reduce the performance of your whole application. And there will be situations where it will better for you to create your own MVC framework than extend Struts.
No comments:
Post a Comment