标签:
Servlet implementation class NewsListServlet
1 package com.wangjialin.server.xml; 2 3 import java.io.IOException; 4 import java.util.List; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 import com.wangjialin.server.domain.News; 12 import com.wangjialin.server.service.NewsService; 13 import com.wangjialin.server.service.implement.NewsServiceBean; 14 15 /** 16 * Servlet implementation class NewsListServlet 17 */ 18 public class NewsListServlet extends HttpServlet { 19 20 private static final long serialVersionUID = 1L; 21 private NewsService newsService = new NewsServiceBean(); 22 23 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 24 doPost(request, response); 25 } 26 27 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 28 29 List<News> newes = newsService.getLastNews(); // 获取最新的视频资讯 30 // [{id:20,title:"xxx",timelength:90},{id:10,title:"xbx",timelength:20}] 31 StringBuilder json = new StringBuilder(); 32 json.append(‘[‘); 33 34 for(News news : newes){ 35 json.append(‘{‘); 36 json.append("id:").append(news.getId()).append(","); 37 json.append("title:\"").append(news.getTitle()).append("\","); 38 json.append("timelength:").append(news.getTimelength()); 39 json.append("},"); 40 } 41 json.deleteCharAt(json.length() - 1); 42 json.append(‘]‘); 43 request.setAttribute("json", json.toString()); 44 request.getRequestDispatcher("/WEB-INF/page/jsonnewslist.jsp").forward(request, response); 45 } 46 47 }
NewsServiceBean
1 package com.wangjialin.server.service.implement; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.wangjialin.server.domain.News; 7 import com.wangjialin.server.service.NewsService; 8 9 10 public class NewsServiceBean implements NewsService { 11 /** 12 * 获取最新的视频资讯 13 * @return 14 */ 15 public List<News> getLastNews(){ 16 List<News> newes = new ArrayList<News>(); 17 newes.add(new News(10, "王家林", 20)); 18 newes.add(new News(45, "家林哥哥", 10)); 19 newes.add(new News(89, "Android is amazing", 50)); 20 return newes; 21 } 22 }
NewsService
1 package com.wangjialin.server.service; 2 3 import java.util.List; 4 5 import com.wangjialin.server.domain.News; 6 7 8 public interface NewsService { 9 10 /** 11 * 获取最新的视频资讯 12 * @return 13 */ 14 public List<News> getLastNews(); 15 16 }
News
1 package com.wangjialin.server.domain; 2 3 public class News { 4 5 private Integer id; 6 private String title; 7 private Integer timelength; 8 9 public News(Integer id, String title, Integer timelength) { 10 this.id = id; 11 this.title = title; 12 this.timelength = timelength; 13 } 14 15 public Integer getId() { 16 return id; 17 } 18 19 public void setId(Integer id) { 20 this.id = id; 21 } 22 23 public String getTitle() { 24 return title; 25 } 26 27 public void setTitle(String title) { 28 this.title = title; 29 } 30 31 public Integer getTimelength() { 32 return timelength; 33 } 34 35 public void setTimelength(Integer timelength) { 36 this.timelength = timelength; 37 } 38 39 }
jsonnewslist.jsp
<%@ page language="java" contentType="text/plain; charset=UTF-8" pageEncoding="UTF-8"%>
${json}
itcast.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 6 <title>Insert title here</title> 7 </head> 8 <body> 9 传智播客<br/> 10 传智播客<br/> 11 传智播客<br/> 12 传智播客<br/> 13 传智播客<br/> 14 传智播客<br/> 15 传智播客<br/> 16 传智播客<br/> 17 传智播客<br/> 18 19 </body> 20 </html>
Android 开发服务类 02_NewsListServlet
标签:
原文地址:http://www.cnblogs.com/renzimu/p/4540082.html