1

Please can somebody help me out,I need to verify the url action mapping of struts 1 on Filter level.If the provided url action mapping exist in Struts1 then it is ok otherwise show 404 error. This code show 404 page but i need to validate url before showing it:

p_hsResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
asad khan
  • 65
  • 1
  • 3
  • 9
  • Why exactly? What are you trying to achieve? – Buhake Sindi Sep 22 '14 at 07:58
  • I cant tell in comment but It is simple that i want to check the url mapping exist in struts-config or not.... like i am access hhtp://localhost:8080/ABC/home.do If it is exist in struts-config.xml then it validate... – asad khan Sep 25 '14 at 10:16
  • i mean home.do exist in struts-config.xml or not... – asad khan Sep 25 '14 at 13:32
  • Struts do that already, why do you want to create a filter for the same purpose? – Buhake Sindi Sep 25 '14 at 14:42
  • If u dont know then don't waste mine & ur time bro .... – asad khan Oct 02 '14 at 10:22
  • Stop that attitude of yours. I am trying to help you. It's not compulsory for me to help you. Also, who said that we don't know how to accomplish this? What I'm saying is that Struts has implemented this already. It occurs during the Struts initialization/startup phase. Why do you want to do the same thing Struts already does? – Buhake Sindi Oct 02 '14 at 12:54
  • because i am authenticating user in filter level due to Single Sign-on as i have to check each requ contain user ID in header or not... – asad khan Oct 03 '14 at 10:41
  • so when user is not valid then it display error page while the requested url is not exist in struts-config.... – asad khan Oct 03 '14 at 10:42
  • Don't forget that the Servlet Filter is called before a Servlet call and after an execution of a Servlet. There will be time when the filter will be called prior to the `ActionServlet` is called, so the solution you want shouldn't depend on Struts config. – Buhake Sindi Oct 03 '14 at 12:05
  • Yes iknow that i think i need to authenticate user in action level .... but it will change the whole flow and it will take time... – asad khan Oct 09 '14 at 05:29

1 Answers1

1

Based on your comment, I don't think that its wise to validate the request path info to the struts action mapping.

What I will suggest is to map your Filter to the same <url-mapping> your Struts ActionServlet is mapped to.

Example:, should your ActionServlet is mapped on to *.do (suffix mapping), your filter should be mapped to the same.

<filter>
    <filter-name>My filter</filter-name>
    <filter-class>com.myApp.myFilter</filter-class>        
</filter>
<filter-mapping>
    <filter-name>My filter</filter-name>
    <url-pattern>*.do</url-pattern><!-- The same <url-pattern> to my Struts ActionServlet class
</filter-mapping>

That way, it will execute prior/after your struts ActionServlet call.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
  • 1
    what if user tried abc.do while abc path doesnot exist in struts-config ??? this solution is not feasible on that case... – asad khan Oct 09 '14 at 05:28
  • Then there will be an HTTP 404 error. In Struts it returns a blank screen. That's what you want. If one cannot access a page or resource that doesn't exist, an HTTP 404 error should return. – Buhake Sindi Oct 09 '14 at 07:44
  • Need to do the check after the filter.chain completes as a post action, and then see if http status is 404 – tgkprog Feb 17 '15 at 09:17