码迷,mamicode.com
首页 > 其他好文 > 详细

Top10Servlet

时间:2015-06-30 23:39:46      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

<span style="font-size:18px;">/**
 * Top10
 * author:杨鑫
 */
package servlet;

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

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Top10Servlet
 */
@WebServlet("/top10")
public class Top10Servlet extends HttpServlet {
	private List<String> londonAttractions;
	private List<String> parisAttractions;
	
	/**
	 * 默认的无参数构造方法
	 */
    public Top10Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	public void init() throws ServletException {
		// TODO Auto-generated method stub
		londonAttractions = new ArrayList<String>(10);
		londonAttractions.add("Buckingham Palace");
		londonAttractions.add("London Eye");
		londonAttractions.add("British Museum");
		londonAttractions.add("National Gallery");
		londonAttractions.add("BigBen");
		londonAttractions.add("Tower of London");
		londonAttractions.add("Natural History Museum");
		londonAttractions.add("Canary Wharf");
		londonAttractions.add("2012 Olympic Park");
		londonAttractions.add("St Paul's Cathedral");
		
		parisAttractions = new ArrayList<String>(10);
		parisAttractions.add("Eiffel Tower");
		parisAttractions.add("Notre Dame");	
		parisAttractions.add("The Louvre");
		parisAttractions.add("Champs Elysees");
		parisAttractions.add("Arc de Triomphe");
		parisAttractions.add("Sainte Chapelle Church");
		parisAttractions.add("Les Invalides");
		parisAttractions.add("Musee d'Orsay");
		parisAttractions.add("Montmarte");
		parisAttractions.add("Sacre Couer Basilica");	
	}

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String city = request.getParameter("city");
		if(city != null && (city.equals("london") || city.equals("paris"))){
			showAttractions(request, response, city);
		}else{
			showMainPage(request, response);
		}
	}
	
	private void showMainPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print("<html><head>" + "<title>Top 10 Tourist Attractions</title>" + "</head><body>" + "please select a city:" + "<br/><a href='?city=london'>London</a>" + "<br/><a href='?city=paris'>Paris</a>" + "</body></html>");
	}
	
	private void showAttractions(HttpServletRequest request, HttpServletResponse response, String city) throws ServletException, IOException{
		int page = 1;
		String pageParameter = request.getParameter("page");
		if(pageParameter != null){
			try{
				page = Integer.parseInt(pageParameter);
			}catch(NumberFormatException e){
				e.printStackTrace();
			}
			if(page > 2){
				page = 1;
			}
		}
		
		List<String> attractions = null;
		if(city.equals("london")){
			attractions = londonAttractions;
		}else if(city.equals("paris")){
			attractions = parisAttractions;
		}
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html><head>" + "<title>Top 10 Tourist Attraction</title>" + "</head><body>");
		out.println("<a href ='top10'>Select City</a>");
		out.println("<hr/>Page " + page + "<hr/>");
		
		int start = page * 5 - 5;
		for(int i = start; i < start + 5; i++)
		{
			out.println(attractions.get(i) + "<br/>");
			out.print("<hr style ='color:blue'/>" + "<a href='?city=" + city + "&page=1'>Page 1</a>");
			out.println("  <a href='?city=" + city + "&page=2'>Page 2</a>");
			out.println("</body></html>");
		}
	}
	
}
</span>



这里的配置文件我就不贴出来了。

请自行配置:

如图效果

技术分享


细节:


技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

Top10Servlet

标签:

原文地址:http://blog.csdn.net/u012965373/article/details/46702427

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!