Tuesday, April 9, 2013

How to return a List in Ajax using servlet


selectuser.js


var xmlHttp;

function showUser(str)
{ 
    
xmlHttp=GetXmlHttpObject()
    
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="TestServlet"
//alert(url=url+"?q="+str);
url=url+"?q="+str

url=url+"&sid="+Math.random()
    
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
 } 
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();

 }
catch (e)
 {

 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
// JavaScript Document


index.jsp


<html>
<head>
<script src="selectuser.js"></script>
</head>

<body>
<center>

<form><br>
<br>
Select a ID: <select name="users"
    onchange="return showUser(this.value);">
    <option name=select>plz select</option>

    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select></form>
<p>
<div id="txtHint"><b></b></div>
</center>
</body>
</html>



TestServlet

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

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

public class TestServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;



      public TestServlet()
      {
            super();
      }

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

            String str = request.getParameter("q");
            System.out.println("data="+str);
            PrintWriter out = response.getWriter();
            int counter = 1;
            if (str.equals("1")) {
                 
                  out.print("<table border=1>");
                  out.println("<tr >");
                  out.println("<td >EMPLOYEE DETAILS</td>");
                  out.println("</tr >");
                  out.println("<tr >");
                  out.println("<td ><b>ID</b></td><td>1</td>");
                  out.println("</tr >");
                  out.println("<tr >");
                  out.println("<td ><b>Name</b></td><td>Himanshu</td>");
                  out.println("</tr >");
                  out.println("<tr >");
                  out.println("<td ><b>DOB</b></td><td  >1987</td>");
                  out.println("</tr >");
                  out.println("<tr >");
                  out.println("<td ><b>DOJ</b></td><td>1986</td>");
                  out.println("</tr >");
                  out.println("<tr >");
                  out.println("<td ><b>Location</b></td><td>UP</td>");
                  out.println("</tr >");
                  out.println("<tr >");
                  out.println("<td ><b>Email</b></td><td>swain.himansu@gmail.com</td>");
                  out.println("</tr >");
                  out.println("<tr >");
                  out.println("<td ><b>Mobile</b></td><td>8010999606</td>");
                  out.println("</tr >");
                  out.println("<tr >");
                  out.println("<td ><b>Address</b></td><td  >UP India</td>");
                  out.println("</tr >");
                  out.println("</table>");
            }
           
            else if(str.equals("2"))
            {
                  out.println("<h3>There is no information on ID=2</h3>");
            }
            else if(str.equals("3"))
            {
                  out.println("<h3>There is no information on ID=3</h3>");
            }
            }

}


web.xml


<?xml version="1.0" encoding="UTF-8"?>
  <display-name>Test2</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>TestServlet</display-name>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/TestServlet</url-pattern>
  </servlet-mapping>
</web-app>

Java Quick Program

1. Write a Code to generate Random numbers public class GenerateRandomNumbers { public static void main(String[] args) { System.out...