Share on Google+Share on Google+

Shraddha C
Ajax+servlet+js
1 Answer(s)      4 years and 9 months ago
Posted in : Ajax

1) new.jsp

<%@ 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>Simple AJAX Code</title>

</head>
<body>
<script type="text/javascript">
var res;
function fun()
{
    var url="http://localhost:8080/Demo/New";
    if(window.XMLHttpRequest)
        {
        res=new XMLHttpRequest();
        }
    else
        {
        res=new ActiveXObject("Microsoft.XMLHTTP");
        }
    res.onreadystatechange=fun1;
    res.open("Post",url, true);
    res.send();
}

function fun1()

    {
        if(res.readyState==4)
            {
                var r=res.responseText;
                document.getElementById("name1").innerHTML=r;

            }
    }

</script>


<p>Enter something:<input type="text" id="name"></p>
<p>Display here:<input type="text" id="name1"></p>
<button type="button" onclick="fun()">Click here</button>


</body>
</html>

2)New.java

package controller;
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("/New")
public class New extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public New() {
        super();

    }

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

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out=response.getWriter();
        //response.setContentType("text/html");
        out.println("helooo");
        //String s=request.getParameter("name");
        //String r="hiiiii"+s;
        //RequestDispatcher rd=request.getRequestDispatcher("new.jsp");
        //rd.forward(request, response);

    }

}

im enter something in 1st text box,after click on button i must get value from servlet in 2nd text box.but im not getting o/p.kindly help.

Ads

View Answers

July 12, 2012 at 1:13 PM


"mainpage.jsp"

<html>
<head>

<title>Ajax Example</title>

<script language="Javascript">

function postRequest(strURL) {

var xmlHttp;

if (window.XMLHttpRequest) { // Mozilla, Safari, ...

var xmlHttp = new XMLHttpRequest();

} else if (window.ActiveXObject) { // IE

var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

}

xmlHttp.open('GET', strURL, true);

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

xmlHttp.onreadystatechange = function() {

if (xmlHttp.readyState == 4) {

updatepage(xmlHttp.responseText);

}

}

xmlHttp.send(strURL);

}


function updatepage(str){

document.getElementById("result").innerHTML =

"<font color='red' size='5'>" + str + "</font>";;

}

function showCurrentTime(){

var rnd = Math.random();

var url="showTime";

postRequest(url);

}

</script>

</head>

<body>

<h1 align="center"><font color="#000080">Ajax Example</font></h1>

<p><font color="#000080"> This very simple Ajax Example retrieves the

current date and time from server and shows on the form. To view the current

date and time click on the following button.</font></p>

<form name="f1">

<p align="center"><font color="#000080"> <input value=" Show Time "

type="button" onclick='JavaScript:showCurrentTime()' name="showdate"></font></p>

<div id="result" align="center"></div>

</form>

<div id=result></div>

</body>

</html>

"MyServletAjax.java"

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class MyServletAjax extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
Date date = new Date();
PrintWriter pw = response.getWriter();
pw.println(date);

}
}

"web.xml"

<servlet>
<servlet-name>myservletajax</servlet-name>
<servlet-class>MyServletAjax</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservletajax</servlet-name>
<url-pattern>/myServletAjax</url-pattern>
</servlet-mapping>









Related Tutorials/Questions & Answers:
Ajax+servlet+js
Ajax+servlet+js  1) new.jsp <%@ page language="java...-Type" content="text/html; charset=ISO-8859-1"> <title>Simple AJAX... in 1st text box,after click on button i must get value from servlet in 2nd text
Tutorials   
Java Spring Hibernate Struts Training Ajax java How to cascade two text boxes?? How to create runtime drag and drop form builder using asp.net mvc and jquery jsp+ajax validation JQuery-JSP AJAX example not working What is Ajax Framework How to write first example in Ajax? What are Advantages and Disadvantages of AJAX What is Ajax? how to set running watch with descending order in applycation with ajax how to set running watch with descending order in applycation with ajax jsp pages for dispatchaction for adding user to database c++ new, delete, string class using ajax i want to insert data into oracle database how to generate reports from oracle database using jsp and ajax code dynamic creation of multiple drop-down menus Ajax programming tutorials Displaying data from combobox on the web page Displaying data from combobox on the web page Displaying data from combobox on the web page Displaying data from combobox on the web page Displaying data from combobox on the web page What is Java? How to make a ajax application which take data from textarea and that page is not refreshing. how can add new line character into ajax response for java how can add new line character into ajax response for java c# c# asp.net Struts 2 + Ajax bodygaurd java Ajax mew 333 data insertion and fetch 1 my answer what is an ajax call? what is an ajax call? JSF How to easily send and receive recorders from database (in AJAX)? calculating time difference for manually entered in-time and out-time in hours and min for dynamically generated rows in jsp and display in third textfield. how to operate on select box using ajax in struts2? how to operate on select box using ajax in struts2? Auto Search box using ajax Dependant Dropdown Lists AJAX REGISTRATION FORM ajax code for jsp java i want to know how a slide will hide i want to know how a slide will hide

Ads

 
Advertisement null

Ads