简体   繁体   English

在这种情况下使用异步请求是一种好习惯吗?

[英]Is it a good practice to use asynchronous requests in this scenario?

I have a scenario that I have a button in JSP page which sends an email, the request is send to servlet asynchronously using jQuery Ajax and JSON, servlet searches in DB, if the user has an email, it returns the email address and sends an email to it, then forwards to the result page with success or fail of sending the email, but in a case that the user doesn't have an email, it returns false values using JSON to JSP and then a JSP form appears to the user to enter his email. 我有一个场景,我在JSP页面中有一个按钮来发送电子邮件,该请求使用jQuery Ajax和JSON 异步发送到servlet,servlet在数据库中搜索,如果用户有电子邮件,它将返回电子邮件地址并发送电子邮件,然后发送成功或失败发送到结果页面,但是如果用户没有电子邮件,则使用JSON向JSP返回错误值,然后向用户显示JSP表单输入他的电子邮件。

Is it good practice to use Ajax and I know that not each time there's a return value to the user or send request to servlet using get method which return a parameter in a case that the user doesn't have an email? 使用Ajax是一种好习惯吗?我知道不是每次都有一个返回值给用户使用get方法将请求发送到servlet时 ,如果用户没有电子邮件, get方法会返回一个参数?

Using ajax is in practically all cases very good for User Experience . 在几乎所有情况下,使用ajax对于用户体验都非常有用 With ajax, the user will experience instant feedback without the need to face an annoying "flash of content" or a (partially) empty page because the whole HTML response needs to be generated/buffered by the server first. 使用ajax,用户将获得即时反馈,而无需面对烦人的“内容刷新”或(部分)空白页面,因为整个HTML响应首先需要由服务器生成/缓冲。 This is really a huge plus of using JS/ajax. 这确实是使用JS / ajax的巨大优势。

Using JSON is generally favorable above XML, HTML or even plain text. 使用JSON通常比XML,HTML甚至纯文本更受欢迎。 But there is no "best practice" with regard to the ajax data exchange format between client and server. 但是,关于客户端和服务器之间的ajax数据交换格式,没有“最佳实践”。 Just pick whatever suits the requirement the best. 只要选择最适合要求的东西即可。 JSON is perfectly fine for this case. JSON非常适合这种情况。 jQuery understands it out-the-box and in Java you have choice of a plethora of easy-to-use JSON parsers. jQuery是开箱即用的,并且在Java中,您可以选择大量易于使用的JSON解析器。

However, when developing an ajax-enabled webapplication, you really need to take into account that the core functionality does not break when the client has JS disabled . 然而,开发一个支持AJAX的web应用时,你真的需要考虑的是,当客户端有JS 禁用的核心功能破。 This is called Unobtrusive JavaScript . 这被称为Unobtrusive JavaScript Most of the searchbots, mobile browsers and textbased browsers don't use JS. 大多数搜索机器人,移动浏览器和基于文本的浏览器都不使用JS。 You should try to use JS only for Progressive Enhancements . 您应该尝试仅将JS用于渐进增强 To test this yourself, in Firefox you can use for example the Web Developer Toolbar to easily enable/disable JS support. 为了自己进行测试,在Firefox中,您可以使用例如Web Developer Toolbar来轻松启用/禁用JS支持。 Run your website with JS disabled and observe if the core functionality is maintained as well. 在禁用JS的情况下运行您的网站,并观察核心功能是否也得到维护。

The best way to achieve this is to start developing the website without any single line of JS code, even without a single onclick , onsubmit , onwhatever attribute on the HTML elements. 实现此目的的最佳方法是开始开发网站,而没有任何一行JS代码,即使HTML元素上没有一个onclickonsubmitonwhatever属性。 Once you get the core functionality to work, then you can start adding JS in flavor of a script which executes during document ready and attachs functions to the HTML elements of interest (even here, you should not change the original HTML code!). 一旦可以使用核心功能,就可以开始添加JS脚本风格的JS,该脚本在文档准备就绪时执行,并将功能附加到感兴趣的HTML元素上(即使在这里, 也不应更改原始HTML代码!)。 Let the JS functions fire ajax requests on the same URL or maybe a different one, depending on the requirement. 让JS函数根据需要在相同URL或不同URL上触发ajax请求。 You can in the Servlet distinguish between an ajax and normal request as follows: 您可以在Servlet中区分ajax请求和普通请求,如下所示:

if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) {
    // Handle ajax request. Return JSON response here.
} else {
    // Handle normal request. Return normal HTML response here (by JSP).
}

See also: 也可以看看:

Json is just a data-interchange format. Json只是一种数据交换格式。 Using Json or not has nothing to do with using asynchronous communication or not... You can do both communication types using Json (or XML, or serialized objects, it doesn't matter). 是否使用Json与是否使用异步通信无关...您可以使用Json(或XML或序列化对象,这都没有关系)来进行两种通信类型。

Now, in your problem, it looks like you just want to use Asynchronous communication to improve the user experience (it will not flick the user's browser). 现在,在您遇到的问题中,您似乎只想使用异步通信来改善用户体验(它不会轻拂用户的浏览器)。 If that's the case, Asynchronous communication is the way to go! 如果是这样,异步通信是必经之路!

I don't think you need ot use AJAX in this. 我认为您不需要在其中使用AJAX。 The main idea of the ajax is to render server response without postback and in your case you are redirecting page after you get some kind of result. Ajax的主要思想是在不回发的情况下呈现服务器响应,在这种情况下,您将在获得某种结果后重定向页面。

In my opinion you shoul choose on of these two ways. 我认为您应该选择这两种方式。

1) Use AJAX, send data to servlet and then render response from server wether the mail is sent or not. 1)使用AJAX,将数据发送到servlet,然后从服务器渲染响应,无论是否发送邮件。

2) Submit your form to servlet and sent email and then redirect to jsp with the success/fail result. 2)将您的表单提交到servlet并发送电子邮件,然后将成功/失败结果重定向到jsp。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM