20130428 sturts2对ajax的支持

基础内容:ajax基础

简单的ajax的测试,向服务器发送请求(post或者get)并获取所传送的参数。在文本框中显示
1 :index.jsp页面:测试页面
  
My JSP 'index.jsp' starting page
 
  
 
   
   
用户:
时间:
2:AjaxServlet 服务器端servlet代码:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
    /*  List list = new ArrayList();     
 list.add( "first" );     
 list.add( "second" );     
 list.add( "three" );     
 JSONArray jsonArray2 = JSONArray.fromObject( list );     
 System.out.println( jsonArray2 );*/  
/*
  Map map = new HashMap();     
  map.put( "name", "json" );     
  map.put( "bool", Boolean.TRUE );    
  map.put( "int", new Integer(1) );     
  map.put( "arr", new String[]{"a","b"} );     
  map.put( "func", "function(i){ return this.arr[i]; }" );     
  JSONObject json = JSONObject.fromObject( map );   
  out.write(json.toString());
  */
//uname="+uname+"&pwd="+pwd",true);
System.out.println(request.getParameter("uname")+"||"+request.getParameter("pwd"));
Map map = new HashMap();     
map.put( "uname", request.getParameter("uname"));     
map.put( "pwd", request.getParameter("pwd") );    
JSONObject json = JSONObject.fromObject( map );   
out.write(json.toString());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
3:web.xml中配置:
 
    AjaxServlet
    AjaxServlet
 
 
    AjaxServlet
    /servlet/AjaxServlet
 
4:jar(由于用到了json封装java,有关json的):
说明:
org.apache.commons(3.2以上版本)
org.apache.oro
net.sf.ezmorph(ezmorph-1.0.4.jar)
nu.xom
JSON-lib

/
sturts2 中ajax的简单的例子(利用ajax的json插件进行开发相当的方便)

你可能感兴趣的:(java)