How to catch non exist requested URL in Java servlet?
My objects are stored online in two different places :
<1开发者_StackOverflow社区> On my nmjava.com site, where I can put them in a directory called "Dir_My_App/Dir_ABC/"
<2> On Google App Engine datastore
When my Java app runs it checks both places for the objects, I designed the app so that it tries to get an object from a Url, it doesn't care whether it's an object in a directory or an object returned by a servlet.
My_Object Get_Object(String Site_Url,String Object_Path)
{
... get object by the name of Object_Path from the Site_Url ...
}
Now the request Url for my web site nmjava.com might look like this :
http://nmjava.com/Dir_My_App/Dir_ABC/My_Obj_123 [ In a directory ]
Or in the case of Google App Engine servlet :
http://nm-java.appspot.com/Check_License/Dir_My_App/Dir_ABC/My_Obj_123 [ Non exist ]
The "Object_Path" was generated by my app automatically. It can now get the object from my site by the above method like this :
My_Object Get_Object("http://nmjava.com","/Dir_My_App/Dir_ABC/My_Obj_123");
In the Google App Engine, my servlet is running and ready to serve the object, if the request comes in correctly, but since I don't want to design my app to know whether the object is in one site's directory or in other site's datastore, I need to design the servlet to catch the non exist Url, such as the one above, and be able to make a call :
My_Object Get_Object("http://nm-java.appspot.com/Check_License","/Dir_My_App/Dir_ABC/My_Obj_123");
So my question is : When a request comes into the servlet with a non exist Url, how should it catch it and analyze the url in order to respond properly, in my case it should know that :
http://nm-java.appspot.com/Check_License/Dir_My_App/Dir_ABC/My_Obj_123
is asking for the object "My_Obj_123" [ ignore the dirs ] and return the object from the datastore.
Now I'm getting this :
Error: Not Found
The requested URL /Check_License/Dir_My_App/Dir_ABC/My_Obj_123 was not found on this server.
Where in my servlet and how do I detect the request for this non exist Url ?
What comes to mind initially is creating a ServletFilter (configured in your web.xml) to look at each request and re-route the request if it meets your specific criteria. See tutorial here.
精彩评论