石头JAVA摆地摊儿

JAVA所有的讨论-要一点一点的进步

  BlogJava :: 首页 ::  :: 联系 ::  :: 管理 ::
  30 随笔 :: 0 文章 :: 11 评论 :: 0 Trackbacks
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

    
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="json2.js"></script>

        
<script>
var ajaxObject;
var username;
var password;
function createAjax()
{
   
if (window.ActiveXObject)
   
{
      ajaxObject 
= new ActiveXObject("Microsoft.XMLHTTP");
   }

   
else if(window.XMLHttpRequest)
   
{
      ajaxObject 
= new XMLHttpRequest();
   }

}




function showText()
{
   
var username = document.getElementById("username").value;
   
var password = document.getElementById("password").value;
   
var createUserInfoJSON = JSON.stringify(new createUserInfo(username, password));

   createAjax();
   ajaxObject.onreadystatechange 
= backCall;
   ajaxObject.open(
"GET""write"true);
   ajaxObject.setRequestHeader(
"Content-Type""application/x-www-form-urlencoded");
   ajaxObject.send(createUserInfoJSON);
}


function backCall()
{
   
if (ajaxObject.readyState == 4)
   
{
      
if (ajaxObject.status == 200)
      
{
         
var xmlString = ajaxObject.responseText;
         alert(
"response Text is:\n" + xmlString)
      }

   }

}


function createUserInfo(username, password)
{
   
this.username = username;
   
this.password = password;

}



 
</script>
    
</head>

    
<body>
username:
<input id="username" type="text" name="username"/><br/>
password:
<input id="password" type="text" name="password"/>
        
<input type="button" onclick="showText()" value="click me!"
            name
="button" />
    
</body>

</html>

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

public class write extends HttpServlet {

    
public write() {
        
super();
    }


    
public void destroy() {
        
super.destroy();
    }


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

    }


    
public void doPost(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException {

        String line 
= "";
        StringBuffer buffer 
= new StringBuffer();
        BufferedReader reader 
= request.getReader();
        
while ((line = reader.readLine()) != null{
            buffer.append(line);
        }

        
        System.out.println(buffer.toString());
        JSONObject json 
= JSONObject.fromObject(buffer.toString());

        String responseText 
= "username=" + json.getString("username")
                
+ " password=" + json.getString("password");

        response.getWriter().print(responseText);

    }


    
public void init() throws ServletException {

    }


}


别忘了下载一些JAR文件,www.json.org中有详细的资料。
posted on 2009-05-13 00:26 石头JAVA摆地摊儿 阅读(296) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: