Thursday 25 April 2013

Web Technology - HTML progrm using AJAX and Java



SOURCE CODE:



Index.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!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=UTF-8">

<title>JSP and Servlet using AJAX</title>

<script type="text/javascript">

function getXMLObject() //XML OBJECT

{

var xmlHttp = false;

try {

xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") // For Old Microsoft

Browsers

}

catch (e) {

try {

xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") // For Microsoft IE 6.0+

}

catch (e2) {

xmlHttp = false // No Browser accepts the XMLHTTP Object then false

}}

if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {

xmlHttp = new XMLHttpRequest();

//For Mozilla, Opera Browsers

}

return xmlHttp; // Mandatory Statement returning the ajax object created

}

var xmlhttp = new getXMLObject(); //xmlhttp holds the ajax object

function ajaxFunction() {

var getdate = new Date(); //Used to prevent caching during ajax call

if(xmlhttp) {

xmlhttp.open("GET","gettime?" + getdate.getTime(),true); //gettime will be the servlet

name

xmlhttp.onreadystatechange = handleServerResponse;

xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

xmlhttp.send(null);

}}

function handleServerResponse() {



if (xmlhttp.readyState == 4) {

if(xmlhttp.status == 200) {

document.myForm.time.value=xmlhttp.responseText; //Update the HTML Form

element

}

else {

alert("Error during AJAX call. Please try again");

}}}

</script>

<body>

<form action="" name="myForm">

Server Time:<input type="text" name="time" />

<br />

<input type="button" onClick="javascript:ajaxFunction();" value="Click to display

Server Time on Textbox"/>

<br />

</form></body>

</head></html>




Gettime.java:



import java.io.IOException;

import java.io.PrintWriter;

import java.util.Date;

import javax.servlet.ServletConfig;

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(name="gettime", urlPatterns={"/gettime"})

public class gettime extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse

response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

try {

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet gettime</title>");

out.println("</head>");



out.println("<body>");

out.println("<h1>Servlet gettime at " + request.getContextPath () + "</h1>");

out.println("</body>");

out.println("</html>");

} finally {

out.close();



}}

@Override

public void init(ServletConfig config) throws ServletException {

super.init(config);

}

@Override

public void destroy() {

}

@Override

public void doPost(HttpServletRequest request,HttpServletResponse response)

throws IOException, ServletException {

PrintWriter out = response.getWriter();

Date df = new Date();

out.println(df.getTime());

}

@Override

public void doGet(HttpServletRequest request,HttpServletResponse response)

throws IOException, ServletException {

doPost(request,response);

}

}

No comments:

Don't You Think this Awesome Post should be shared ??
| Web Technology - HTML progrm using AJAX and Java |
Back To Top Related Posts Plugin for WordPress, Blogger...