日期:2014-05-16  浏览次数:20344 次

获取服务器上格式为JSON和XML两种格式的信息的小程序

 

 

首先写一个应用服务器端的jsp程序,用jsp和servlet简单实现,如下图所示

 

 

package cn.roco.domain;

public class News {
	private Integer id;
	private String title;
	private Integer timelength;

	public News() {
	}

	public News(Integer id, String title, Integer timelength) {
		this.id = id;
		this.title = title;
		this.timelength = timelength;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Integer getTimelength() {
		return timelength;
	}

	public void setTimelength(Integer timelength) {
		this.timelength = timelength;
	}

}

 

 

package cn.roco.service;

import java.util.List;

import cn.roco.domain.News;

public interface VideoNewsService {

	/**
	 * 获取最新视频资讯
	 * @return
	 */
	public List<News> getLastNews();

}



 

package cn.roco.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.roco.domain.News;
import cn.roco.service.VideoNewsService;

public class VideoNewsServiceBean implements VideoNewsService{
	/**
	 * 模拟从服务器中获取数据  返回
	 */
	public List<News> getLastNews(){
		List<News> newses=new ArrayList<News>();
		for (int i = 1; i < 30; i++) {
			newses.add(new News(i,"Xili"+i,i+90));
		}
		return newses;
	}
}


 
 

package cn.roco.servlet;

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

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

import cn.roco.domain.News;
import cn.roco.service.VideoNewsService;
import cn.roco.service.impl.VideoNewsServiceBean;

public class ListServlet extends HttpServlet {
	
	private VideoNewsService newsService=new VideoNewsServiceBean();
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		List<News> videos=newsService.getLastNews();
		String format=req.getParameter("format");
		//返回JSON格式
		if ("json".equals(format)) {
			StringBuilder builder=new StringBuilder();
			builder.append('[');
			for (News news : videos) {
				builder.append('{');
				builder.append("id:").append(news.getId()).append(',');
				//转义 ""双引号
				builder.append("title:\"").append(news.getTitle()).append("\",");
				builder.append("timelength:").append(news.getTimelength());
				builder.append("},");
			}
			builder.deleteCharAt(builder.length()-1);//去掉最后的','
			builder.append(']');
			req.setAttribute("json", builder.toString());
			req.getRequestDispatcher("/WEB-INF/page/jsonvideonews.jsp").forward(req, resp);
		}else{
			//返回XML格式
			req.setAttribute("videos", videos);
			req.getRequestDispatcher("/WEB-INF/page/videonews.jsp").forward(req, resp);
		}
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
}


 

 如果要返回XML文件  就forward到videonews.jsp页面

<%@ page language="java" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><?xml version="1.0" encoding="UTF-8"?>
<videonews> 
	<c:forEach items="${videos}" var="video">
		<news id="${video.id}">
			<title>${video.title}</title>
			<tim