Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Ajax Ajax+servlet+js
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Shraddha C
Ajax+servlet+js
1 Answer(s)      2 years and 5 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.


Advertisement
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
 

 

 

DMCA.com