Shoaib Doulatabad Shoaib Doulatabad - 1 year ago 61
Ajax Question

How to Check for a Unique User?

I'm having any application in which I have

as unique and whenever an end user enters his
I have fired an SQL query through Ajax which check whether this
email id
exists or not. Until here its working fine but now I what that is when
exists then I want to display on the JSP page that This id is already taken and vice-versa.

So my question is how to get the response from struts app and give it to Ajax or JSP page and what to write in "struts.xml" result tag?

This is my Ajax call to

$("#email").blur(function() {
var EmailV = $("#email").val();
if(EmailV.length > 10){
$.get("EmailCheck.action", {
Email_Id: EmailV
}, function(response){
setTimeout("finishAjax('Info', '"+escape(response)+"')", 450);
return false;

This is my
should there be result tag for this Email type checker?

<action name="EmailCheck" class="org.register.customers.EmailCheckAction" method="EmailCheck">
<result name="input">index.jsp</result>
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>

This is my action code:

public String EmailCheck() throws Exception{


boolean create = Check_Email(user);

// "this is my sql call to check the existence of email"

if (true) {

return "input";
} else {

return ERROR;

Answer Source

If you call an Ajax action you can return a dispatcher result. In this case a rendered JSP fragment will output as a response. Also, you have options to return a stream, json, or none results. To your case I will consider the first option. First look at this answer to get you familiar how to return a stream result with text/html content type. Then in the JSP you should create a target div where you put the response.

<div id="result"/>

now modify your JS to insert result into div.

}, function(response){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download