ExtJs读取servlet传送数据

时间:2021-06-30 19:40:04

EXTJS

application.js

Ext.onReady(function() {
//			var data = [{
//						name : '唐伯虎',
//						age : 25,
//						sex : '男'
//					}, {
//						name : '楚留香',
//						age : 24,
//						sex : '女'
//					}];
			var ds = new Ext.data.JsonStore({

						proxy : new Ext.data.HttpProxy({
									url : 'dataList'// 相对路径
								}),
						root : ' ',
						fields : [{
									name : 'name'
								}, {
									name : 'age'
								}, {
									name : 'sex'
								}]

					});

			// ds.loadData(data);
			var grid = new Ext.grid.GridPanel({
						viewConfig : {
							forceFit : true
						},
						store :ds,
						columns : [{
									header : '姓名',
									dataIndex : 'name'
								}, {
									header : '年龄',
									dataIndex : 'age'
								}, {
									header : '出生日期',
									dataIndex : 'sex'
								}]
					});
			var win = new Ext.Window({
						title : 'HttpProxy',
						width : 400,
						height : 300,
						layout : 'fit',
						items :grid
					});
			ds.load();
		
			win.show();
		});



servlet

dataList.java

package com.ext.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
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.JSONArray;
import net.sf.json.JSONObject;

import org.apache.jasper.tagplugins.jstl.core.Out;

/**
 * Servlet implementation class dataList
 */
public class dataList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public dataList() {
        super();
        // TODO Auto-generated constructor stub
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		  //  response.setContentType("text/html");  
	   response.setCharacterEncoding("UTF-8");  
	        String _str = "[{name:'唐伯虎',age:25,sex:'男'},{name:'楚留香',age:24,sex:'女'}]";  
         	//_str ="[{name:'唐伯虎',age:25,sex:'男'},{name:'楚留香',age:24,sex:'女'}];"	;
	    	response.getWriter().write(_str);
	    //	request.setAttribute("ooo", _str);
	    //	request.getRequestDispatcher("datawrite.jsp").forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}
web.xml

  <servlet>
    <description></description>
    <display-name>dataList</display-name>
    <servlet-name>dataList</servlet-name>
    <servlet-class>com.ext.servlet.dataList</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>dataList</servlet-name>
    <url-pattern>/dataList</url-pattern>
  </servlet-mapping>
  <servlet>

前台测试路径

http://localhost:8080/ExtJsApplication/index.html