Servlet+Json代码

package com.brmoney.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

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 com.brmoney.flight.pojo.DomeTicketPsg;

@SuppressWarnings("serial")
public class CreateFeedServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public CreateFeedServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy();
	}

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

	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		String feed = request.getParameter("feed");

		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");

		DomeTicketPsg user1 = new DomeTicketPsg();
		user1.setPsgName(feed);
		DomeTicketPsg user2 = new DomeTicketPsg();
		user2.setPsgName(feed);

		List list = new ArrayList();
		list.add(user1);
		list.add(user2);
		JSONArray jsonList = JSONArray.fromObject(list);

		PrintWriter out = response.getWriter();
		out.write(jsonList.toString());
		out.flush();
		out.close();
	}

	public void init() throws ServletException {
	}

}

 

 

Ext.define('FV.lib.FeedValidator', {
			singleton : true,

			/**
			 * @cfg {String} url The url to validate feeds on
			 */
			url : 'http://localhost:8080/utils/servlet/CreateFeedServlet',

			/**
			 * Validates a given feed's formating by fetching it and ensuring it
			 * is well formed
			 * 
			 * @param {FV.model.Feed}
			 *            feed The feed to validate
			 */
			validate : function(feed, options) {
				options = options || {};

				Ext.applyIf(options, {
							scope : this,
							success : Ext.emptyFn,
							failure : Ext.emptyFn
						});

				Ext.Ajax.request({


							url : this.url,
							params : {
								feed : feed.get('url')
							},
							scope : this,
							success : function(response) {
								if (this.checkResponse(response, feed)) {
									options.success.call(options.scope, feed);
								}
							},
							failure : function() {
								options.failure.call(options.scope);
							}
						});
			},

			/**
			 * @private Validates that a response contains a well-formed feed
			 * @param {Object}
			 *            response The response object
			 */
			checkResponse : function(response, feed) {
				// var dq = Ext.DomQuery, url = feed.get('url'),xml, channel,
				// title;

				try {
					alert(response.responseText);
					var objs = eval("("+response.responseText+")");
					alert(objs.length);
					for(var i=0;i<objs.length;i++){
						alert(objs[i].psgName);
					}
					
					// channel = xml.getElementsByTagName('channel')[0];

					// if (channel) {
					// title = dq.selectValue('title', channel, url);
					// return true;
					// }
				} catch (e) {
					alert(e.description);
				}
				return false;
			}
		});

  

 

 

posted @ 2013-10-30 07:22  佛法无边  阅读(436)  评论(0编辑  收藏  举报