Note that there are some explanatory texts on larger screens.

plurals
  1. POJSP + Ajax not working
    primarykey
    data
    text
    <p>Objective: If I type an email id, in the html form it has to send the request to jsp where it does the logic and has to print(in the html form) whether the email is available or not. I have the following code. Please do help me which part I am doing wrong.</p> <p><strong>CreateAccount.html</strong></p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt; &lt;script type="text/javascript" src="emailStore.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body onload="process()"&gt; &lt;form name="login"&gt; &lt;table align="center"&gt; &lt;tr&gt; &lt;td&gt;Email*&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="userinput" id="userinput"/&gt;&lt;/td&gt; &lt;td&gt;&lt;div id = "underInput"&gt;&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Password*&lt;/td&gt; &lt;td&gt;&lt;input type="password" name="pswrd" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Repeat Password*&lt;/td&gt; &lt;td&gt;&lt;input type="password" name="pswrd1" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;First Name*&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="userid" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Last Name*&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="userid" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Phone Number&lt;/td&gt; &lt;td&gt;&lt;input type="text" name="userid" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;div style="text-align: center"&gt; &lt;input type="submit" value="Create Account"/&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>The ajax part in a javascript file. <strong>emailStore.js</strong></p> <pre><code>var xmlHttp = createXmlHttpRequestObject(); function createXmlHttpRequest() { var xmlHttp; if(window.ActiveXObject) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { xmlHttp = false; } } else { try { xmlHttp = new ActiveXObject(); } catch(e) { xmlHttp = false; } } if(!xmlHttp) { alert("can't create that object"); } else { return xmlHttp; } } function process() { if(xmlHttp.readyState==0 || xmlHttp.readyState==4) { email = encodeURIComponent(document.getElementById("userinput").value); xmlHttp.open("GET", "emailStore.jsp?email=" + email, true); xml.onreadystatechange = handle.ServerResponse; xmlHttp.send(null); } else { setTimeout('process()', 1000); } } function handleServerResponse() { if(xmlHttp.readyState==4) { if(xmlHttp.status==200) { xmlResponse = xmlHttp.responseXML; xmlDocumentElement = xmlResponse.documentElement; message = xmlDocumentElement.firstChild.data; document.getElementById("underInput").innerHTML = '&lt;span style = "color:blue"&gt;' + message + '&lt;/span&gt;'; setTimeout('process()',1000); } else { alert('Something went Wrong'); } } } </code></pre> <p>And the logic part in a jsp file- <strong>emailStore.jsp</strong></p> <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&gt; &lt;%@ page import="java.util.ArrayList"%&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt; &lt;title&gt;Insert title here&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;% String email = request.getParameter("userinput"); ArrayList&lt;String&gt; emails = new ArrayList&lt;String&gt;(); emails.add("something@gmail.com"); if (emails.contains(email)) { out.println("Email already taken!"); } else { out.println("Email available"); } %&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload