平台和web简单demo,基于java,html,js,jquery

服务器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
}
}
服务器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 >

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 >


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;
}
}
//            网址应该为 http://localhost:8080/项目名称/web.xml映射的 url-pattern


xmlHttp.send( null );
}

完整项目 点击打开链接
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值