Jump to content

accessing servlet response in ajax


Recommended Posts

Posted (edited)

Sir Actually i am using servlet to check whether the username is already existing or not in html text box?

 

The servlet code is

 

package ser;

import java.sql.*;

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.IOException;

import javax.swing.*;

import java.io.*;

public class AJAXCheckUsernameServlet extends HttpServlet{

boolean isExist;

public void init(ServletConfig config) throws ServletException {

super.init(config);

}

 

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

String Username = request.getParameter("username").trim();

 

isExist = check(Username);

if(isExist){

response.setContentType("text/html");

response.setHeader("Cache-Control", "no-cache");

response.getWriter().write("true");

}

else{

response.setContentType("text/html");

response.setHeader("Cache-Control", "no-cache");

response.getWriter().write("false");

}

}

public boolean check(String Username){

String uuse = Username;

Connection con;

isExist= false;

//JOptionPane.showMessageDialog(null,"");

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con = DriverManager.getConnection("jdbc:odbc:mail_server","Admin","12061985");

PreparedStatement stat = con.prepareStatement("SELECT USER_NAME FROM REG_USER WHERE USER_NAME = '"+uuse+"'");

ResultSet rs = stat.executeQuery();

while(rs.next()){

isExist = true;

// con.close();

}

con.close();

stat.close();

}

catch(Exception e){

System.out.println("Error"+e);

}

return isExist;

}

}

 

 

The AJAX-Javascript code is

 

function check7(){

var username_ = document.getElementById('username').value;

var url = "http://localhost:8081/CLIENT/AJAXCheckUsernameServlet.class?username="+username_;

//alert(url);

if(window.XMLHttpRequestObject){

xmlHttpRequestObject = new XMLHttpRequest();

}

else if(window.ActiveXObject){

xmlHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");

}

xmlHttpRequestObject.open("GET",url);

 

xmlHttpRequestObject.onreadystatechange = callback;

xmlHttpRequestObject.send(null);

}

function callback() {

if( xmlHttpRequestObject.readyState==4 ){

alert(xmlHttpRequestObject.status);

if( xmlHttpRequestObject.status==200 ) {

alert (xmlHttpRequestObject.responseText);

document.getElementById('username').value = xmlHttpRequestObject.responseText;

}

}

 

}

 

It showing only Empty message in the alert box...

This line -> alert (xmlHttpRequestObject.responseText);

please help me...

Edited by Ramesh

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...