Android 开发服务类 01_ServletForXML

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.XMLService;
13 import com.wangjialin.server.service.implement.XMLServiceBean;
14 
15 /**
16  * Servlet implementation class NewsListServlet
17  */
18 public class ServletForXML extends HttpServlet {
19     
20     private static final long serialVersionUID = 1L;
21     private XMLService newsService = new XMLServiceBean();
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         List<News> newes = newsService.getLastNews();//获取最新的视频资讯
29         
30             request.setAttribute("newes", newes);
31             request.getRequestDispatcher("/WEB-INF/page/news.jsp").forward(request, response);
32     }
33 
34 }

XMLServiceBean

 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.XMLService;
 8 
 9 
10 public class XMLServiceBean implements XMLService {
11     /**
12      * 获取最新的视频资讯
13      * @return
14      */
15     public List<News> getLastNews(){
16         
17         List<News> newes = new ArrayList<News>();
18         newes.add(new News(10, "wangjialin", 20));
19         newes.add(new News(45, "jialingege", 10));
20         newes.add(new News(89, "android", 50));
21         return newes;
22     }
23 }

XMLService

 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 XMLService {
 9 
10     /**
11      * 获取最新的视频资讯
12      * @return
13      */
14     public List<News> getLastNews();
15 
16 }

News.java

 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 }

news.jsp

1 <%@ page language="java" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><?xml version="1.0" encoding="UTF-8"?>
2 <newslist>
3     <c:forEach items="${newes}" var="news">
4         <news id="${news.id}">
5             <title>${news.title}</title>
6             <timelength>${news.timelength}</timelength>
7         </news>
8     </c:forEach>
9 </newslist>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。