Is there any particular reason you can't add this logic to the existing "processRequest" method? I'm all for aspects ;), but I also believe in doing the "simplest thing that can possibly work". I may not really understand your requirement, however.
Assuming an aspect is the correct approach, here's an untested skeleton aspect that should do what you want:
after(HttpServletRequest request, HttpServletResponse response): execution (* MyServlet.processRequest(request, response)) && args(request, response) { String p = (String)request.getParameter("p"); request.setAttribute("p_length", p.length()); // should test for null first! }
Getting the string again from the request is redundant with the original code; another reason to consider adding the logic to processRequest, instead.
Note: I'm assuming that you don't want to change the redirection to index.jsp logic. If you want to redirect somewhere else, you could use around advice to override the original behavior, but I don't recommend it for two reasons. 1) YOu'll have to reproduce most of the processRequest logic in the advice, since you won't be able to call "proceed", since it will do the old redirect. 2) Because of #1, you won't be able to call "proceed", so your design will violate the "principle of least surprise"; if someone reads the servlet code, they will expect it to be executed, but in fact the aspect will "hijack" the control flow and reproduce all the logic in the around advice instead. So, if you need to change the control flow, you really should just modify the original method. I hope all that made sense...
For help on using AspectJ with Servlets, Russ Miles "AspectJ Cookbook" (O'Reilly) discusses this in Chapter 3.
There is also this example on the Eclipse site:
Presumably, it's also in the online help for AJDT!
See also this article in the AOP@Work series by Ron Bodkin:
dean On Jan 23, 2008, at 8:05 AM, kissm1985 wrote: Hi all,
I'm quite new to AOP and have a quiestion about using AspectJ to modularize crosscutting properties in servlets. It would be helpful to find a solution for this problem:
I have a servlet wich gets a parameter from a jsp (form) and forwards it to an other....
... protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { String p = (String)request.getParameter("p"); request.setAttribute("p", p); request.getRequestDispatcher("Index.jsp").forward(request, response); } catch (Exception e) { e.printStackTrace(); } } ...
Now I would like to implement a new requirement : "count the carachters of the string parameter and forward it to Index.jsp". How can I do it with AspectJ? I have the idea (the execution of the getParameter have to be cougth by a pointcut, and after:pointcut the aspect have to post this value (length) to Index.jsp) but I haven't the syntax. Can please you help me?
Thanks, Istvan
-- View this message in context: http://www.nabble.com/AspectJ-with-servlets-tp15039655p15039655.html Sent from the AspectJ - users mailing list archive at Nabble.com.
_______________________________________________ aspectj-users mailing list aspectj-users@xxxxxxxxxxx https://dev.eclipse.org/mailman/listinfo/aspectj-users
Dean Wampler, Ph.D. dean at objectmentor.com See also:
|