Using Ajax after an year...please tell me what I'm missing:

JSP/Ajax File:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">

	function login() {
		var xmlhttp;
		var username = document.getElementById('username').value;
		var queryString = "username="+username;
		if(window.XMLHttpRequest){
			xmlhttp = new XMLHttpRequest();
		}else{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.open("GET", "LoginServlet", true);
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		alert(queryString);
		xmlhttp.send(queryString);
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
				alert(xmlhttp.responseText);
			}
		};
	}
</script>
</head>
<body>
	<input type="text" name="username" id="username" />
	<input type="button" onclick="javascript:login()" value="login" />
</body>
</html>

Servlet:

package Servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public LoginServlet() {
        super();

    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("Servlet Called");
		PrintWriter out = response.getWriter();
		response.setContentType("text/html");
		
		String queryString = request.getParameter("username").toString();
		out.println(queryString);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}

Thank You

Member Avatar for stbuchok

I'd say you're missing an explanation of what the issue is.

I'd say you're missing an explanation of what the issue is.

haha yeah that is one...
Actually I'm getting a null pointer exception in Servlet at line 27:

String queryString = request.getParameter("username").toString();

Probably the way I'm using xmlhttp.send(queryString); in JavaScript is wrong, but not able to figure out.

delete line NO. 22 in jsp file ie xmlhttp.send(queryString);
as in get method u dont need this. This is reqd in POST method..
For passing parameter in get method use this
xmlhttp.open("GET", "LoginServlet?username="+username", true);

replace line NO 19 with this and ur code will work.
Lemme know anymore issues

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.