服务器java代码部分
package
com.johnny.test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Helloworld extends HttpServlet implements javax.servlet.Servlet {
/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public Helloworld() {
super ();
}
/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request , HttpServletResponse response )
throws ServletException, IOException {
response .getWriter().println( "Hello, world 1112!" );
System. out .println( request .getRequestURL());
}
/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request , HttpServletResponse response )
throws ServletException, IOException {
// TODO Auto-generated method stub
}
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Helloworld extends HttpServlet implements javax.servlet.Servlet {
/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public Helloworld() {
super ();
}
/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request , HttpServletResponse response )
throws ServletException, IOException {
response .getWriter().println( "Hello, world 1112!" );
System. out .println( request .getRequestURL());
}
/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request , HttpServletResponse response )
throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
服务器web.xml
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
< web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id = "WebApp_ID" version = "2.5" >
< servlet >
< servlet-name > Helloworld </ servlet-name >
< servlet-class > com.johnny.test.Helloworld </ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name > Helloworld </ servlet-name >
< url-pattern > /Helloworld </ url-pattern >
</ servlet-mapping >
< web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id = "WebApp_ID" version = "2.5" >
< servlet >
< servlet-name > Helloworld </ servlet-name >
< servlet-class > com.johnny.test.Helloworld </ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name > Helloworld </ servlet-name >
< url-pattern > /Helloworld </ url-pattern >
</ servlet-mapping >
</
web-app
>
html页面,或者jsp页面
<!
DOCTYPE
html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< title ></ title >
< script type = "text/javascript" src = "jquery-1.12.2.min.js" ></ script >
< script type = "text/javascript" src = "4_Ajax.js" ></ script >
</ head >
< body >
< form name = "myForm" >
用户: < input type = "text" name = "username" onkeyup = "ajaxFunction()" /> 时间: < input
type = "text" name = "time" />
</ form >
</ body >
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< title ></ title >
< script type = "text/javascript" src = "jquery-1.12.2.min.js" ></ script >
< script type = "text/javascript" src = "4_Ajax.js" ></ script >
</ head >
< body >
< form name = "myForm" >
用户: < input type = "text" name = "username" onkeyup = "ajaxFunction()" /> 时间: < input
type = "text" name = "time" />
</ form >
</ body >
</
html
>
javascript代码(需导入文件jquery
):
/**
* Created by hufangqin on 16/3/30.
*/
function ajaxFunction() {
var xmlHttp;
try {
xmlHttp = new XMLHttpRequest();
} catch (e) {
//Internet Explorer
try {
xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP" );
} catch (e) {
try {
xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
} catch (e) {
alert( "your web browser not support AJAX!" );
return false ;
}
}
}
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState < 4) {
document.myForm.time.value = "what" ;
}
if (xmlHttp.readyState == 4) {
// document.myForm.time.value = "what";
document.myForm.time.value = xmlHttp.responseText;
}
}
* Created by hufangqin on 16/3/30.
*/
function ajaxFunction() {
var xmlHttp;
try {
xmlHttp = new XMLHttpRequest();
} catch (e) {
//Internet Explorer
try {
xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP" );
} catch (e) {
try {
xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );
} catch (e) {
alert( "your web browser not support AJAX!" );
return false ;
}
}
}
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState < 4) {
document.myForm.time.value = "what" ;
}
if (xmlHttp.readyState == 4) {
// document.myForm.time.value = "what";
document.myForm.time.value = xmlHttp.responseText;
}
}
xmlHttp.send(
null
);
}
完整项目 点击打开链接