实现 Servlet,用于识别请求者并发送浏览器的动态 html 或仅发送移动/桌面应用程序的信息
我试图以一种方式思考,我可以使用相同的方法从 JDO 数据库获取数据并将其打印到所有 3 个平台,但不同之处在于,如果它是浏览器请求,它将在动态网页生成器中打印。但我似乎想不出什么好的方法。
例如,如果浏览器请求发布帖子,它将转到打印动态页面+信息请求的代码。像这样
for (Texto e : results)
{
print = "<table width='100%' border='2' cellspacing='2' cellpadding='2'>"
+ "<tr><td colspan='2'>"
+ results.get(0).titulo
+ ";</td></tr><tr><td colspan='2'>"
+ results.get(0).texto
但是,如果其中一个应用程序要求发布帖子,它只会返回:
for (Texto e : results)
{
resp.getWriter().println("Titulo:"
+ results.get(0).titulo);
resp.getWriter().println("Nome:"
+ results.get(0).nome);
因为它不需要打印到动态网页并从此处获取信息。所以我希望它根据用户平台返回不同的内容。但使用相同的方法调用。
现在我有不同的链接来请求相同的信息,一个链接到网络,一个链接到应用程序(桌面和安卓)。但我正在考虑只为这三个链接提供一个链接。到目前为止,我认为
1° 在登录 cookie 中获取有关正在执行请求的平台的信息,并通过两种不同类型在打印方法中执行 if 。
2° 或者只是发送一个带有该信息的参数并执行 if...等等...
但我认为这种方法确实不复杂。
有人对此有任何困难吗?
I trying to think in a way that I can have the same methods to get and print data from a JDO database to all the 3 platforms, but with the difference that if it's a browser request it will prints in a dynamic web page generator. But I cant seem to think of a good method.
For example, if a browser asks for a post it will go to the code that prints a dynamic page + the info request. Like this
for (Texto e : results)
{
print = "<table width='100%' border='2' cellspacing='2' cellpadding='2'>"
+ "<tr><td colspan='2'>"
+ results.get(0).titulo
+ ";</td></tr><tr><td colspan='2'>"
+ results.get(0).texto
But if one of the apps asks for a post it just returns:
for (Texto e : results)
{
resp.getWriter().println("Titulo:"
+ results.get(0).titulo);
resp.getWriter().println("Nome:"
+ results.get(0).nome);
Because it doesn't need to print to a dynamic web page and gets the info from here. So i want it to return different content based on the user platform. But using the same method call.
Now I have different links to request the same info, one to the web and one to apps (desk and android). But I was thinking in having just one link for all the three. So far i though of
1º Get information in the login cookie about the platform that is doing the request and do a if in the print methods by two different types.
2º Or just send one more parameter always with that information and do a if... etc...
But I'm thinking this methods are really unsophisticated.
Anyone has any toughs about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它通常是通过检查“扩展”和/或请求的
Accept
标头来完成的。例如,对于像/person/profile.html
这样的 url,您将将此数据呈现为 HTML,或者如果 url 是/person/profile
+ headerAccept: application /json
您会将其呈现为 JSON。您可以对两种方法使用相同的数据,但使用不同的序列化类型。如果您使用“Spring Web”或类似框架,则配置起来很容易。
it's usually made by checking "extension" and/or requests's
Accept
header. For example for url like/person/profile.html
you'll render this data as HTML, or if url is/person/profile
+ headerAccept: application/json
you'll render it as JSON.You can use same data for both methods, but with different serialization type. It's easy to configure if you are using 'Spring Web', or similar, framework.