如何在 Java servlet 中捕获不存在的请求 URL? [英] How to catch non exist requested URL in Java servlet?

查看:46
本文介绍了如何在 Java servlet 中捕获不存在的请求 URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的对象在线存储在两个不同的地方:

My objects are stored online in two different places :

<1> 在我的 nmjava.com 站点上,我可以将它们放在名为Dir_My_App/Dir_ABC/"的目录中

<1> On my nmjava.com site, where I can put them in a directory called "Dir_My_App/Dir_ABC/"

<2> Google App Engine 数据存储区

<2> On Google App Engine datastore

当我的 Java 应用程序运行时,它会检查两个地方的对象,我设计了该应用程序,以便它尝试从 Url 获取对象,它不关心它是目录中的对象还是由小服务程序.

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 ...
}

现在我的网站 nmjava.com 的请求 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 [ 在目录中]

或者在 Google App Engine servlet 的情况下:

Or in the case of Google App Engine servlet :

http://nm-java.appspot.com/Check_License/Dir_My_App/Dir_ABC/My_Obj_123 [不存在]

Object_Path"是由我的应用程序自动生成的.它现在可以通过上述方法从我的站点获取对象,如下所示:

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");

My_Object Get_Object("http://nmjava.com","/Dir_My_App/Dir_ABC/My_Obj_123");

在 Google App Engine 中,如果请求正确传入,我的 servlet 正在运行并准备好为对象提供服务,但由于我不想设计我的应用程序以了解对象是在一个站点的目录中还是在其他站点的数据存储,我需要设计servlet来捕获不存在的Url,例如上面的那个,并且能够进行调用:

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");

My_Object Get_Object("http://nm-java.appspot.com/Check_License","/Dir_My_App/Dir_ABC/My_Obj_123");

所以我的问题是:当请求进入带有不存在的 URL 的 servlet 时,它应该如何捕获它并分析 URL 以便正确响应,在我的情况下它应该知道:

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

正在请求对象My_Obj_123"[忽略目录]并从数据存储区返回该对象.

is asking for the object "My_Obj_123" [ ignore the dirs ] and return the object from the datastore.

现在我明白了:

错误:未找到

在此服务器上找不到请求的 URL/Check_License/Dir_My_App/Dir_ABC/My_Obj_123.

The requested URL /Check_License/Dir_My_App/Dir_ABC/My_Obj_123 was not found on this server.

在我的 servlet 中的何处以及如何检测对这个不存在的 Url 的请求?

Where in my servlet and how do I detect the request for this non exist Url ?

推荐答案

最初想到的是创建一个 ServletFilter(在您的 web.xml 中配置)来查看每个请求并在它满足您的特定请求时重新路由该请求标准.请参阅教程 这里.

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.

这篇关于如何在 Java servlet 中捕获不存在的请求 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆