The send, receive, and reply operations may be synchronous or asynchronous. A synchronous operation blocks a process till the operation completes. An asynchronous operation is non-blocking and only initiates the operation.
While load of the page by synchronous request will refresh the total page Where as Asynchronous response will refresh only a specific fragment.
Based on our Requirement, we need to choose wheather to go with Asynchronous request or Synchronous Request.
Here is an example containing the use of both Synchronous Request/Response and Asynchronous Request/Responsee.
Here I have used jQuery+Ajax+JSP as Client Side Resource and Servlet as Server Side Resource.
Folder Structure Should be like,
Client Side Code :
<script src="./jquery-1.11.0.min.js"></script>
<script>
$(document).ready(function(){
$('#name').keyup(function(){
var username=$('#name').val();
if(username==""){
username=" ";
}
$.post('SynchronousValidation?username='+username, function(responseText) {
$('#resultAsyn').text(responseText);
});
});
});
</script>
<form action="./SynchronousValidation">
Enter Your Name : <input type="text" name="username" id="name">
<br>
<!-- For Synchronous Call Output -->
<font color=red>Synchronous Response : </font><span id="resultSyn">
<%if(null != request.getAttribute("result")){
out.print(request.getAttribute("result"));
}%>
</span>
<br>
<!-- Asynchronous Output -->
<font color=red>Asynchronous Response : </font><span id="resultAsyn">
</span>
<br>
<input type="submit" value="Validate User Name">
</form>
Server side Code :
package com.skc.username.validate;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class SynchronousValidation
*/
public class SynchronousValidation extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public SynchronousValidation() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
// Getting the Request Parameter from Form Page
String name=request.getParameter("username");
List<String> myNameList = new ArrayList<String>();
myNameList.add("Sitakant");
myNameList.add("SKC");
myNameList.add("Chaudhury");
myNameList.add("BLGR");
RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
if(myNameList.contains(name)){
request.setAttribute("result", "Username already Exist");
}else{
request.setAttribute("result", "No such username is registered.");
}
rd.forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String result = request.getParameter("username");
List<String> myNameList = new ArrayList<String>();
myNameList.add("Sitakant");
myNameList.add("SKC");
myNameList.add("Chaudhury");
myNameList.add("BLGR");
if(myNameList.contains(result)){
result= "Username already Exist";
}else{
result = "No such username is registered.";
}
response.setContentType("text/plain"); // Set content type of the response so that jQuery knows what it can expect.
response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
response.getWriter().write(result); // Write response body.
}
}
No comments:
Post a Comment