Top10Servlet

<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>

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

请自行配置:

如图效果

细节:

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

时间: 2024-10-05 07:40:01

Top10Servlet的相关文章

servlet和jsp学习指南(二)Session管理

Web语言HTTP是无状态的,默认情况下,Web服务器不知道一个HTTP请求是来自初次用户,还是来自之前已经访问过的用户.但正常应用程序需要记住用户身份的,换句说,它必须能够管理用户session. 用于保持状态的常用方法有4种:网址重写.隐藏域.cookie已经HttpSession. (一)网址重写 网址重写是一种Session追踪技术,需要将一个或多个token作为一个查询字符串添加到一个URL钟.注意URL和token之间要用一个问号(?)隔开,两个token之间则是用一个&符号隔开.

java web用于保持状态的4种方法

方法一:网址重写 通过在url地址后面添加若干的token作为查询字符串来实现.token的值一般为 键=值 url?key1=value1&key2=value2&...&keyn=valuen url与token之间需要用?分开,两个token之间则是需要用一个&符号隔开. 此方法适用于token不需要在多个页面中使用时使用. 缺点是 a.在某些浏览器当中url长度有限制 b.url中的信息是可见的,安全性差 c.某些字符需要进行编码 package com.Sessi