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>
这里的配置文件我就不贴出来了。
请自行配置:
如图效果
细节:
Top10Servlet的更多相关文章
- java web用于保持状态的4种方法
方法一:网址重写 通过在url地址后面添加若干的token作为查询字符串来实现.token的值一般为 键=值 url?key1=value1&key2=value2&...&k ...
- servlet之重写
package app02a;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;impo ...
- servlet 会话管理
一.URL 重写 URL 重写是一种会话跟踪技术,它将一个或多个token添加到URL的查询字符串中,每个token通常为 key=value形式,如下: url?key-1=value-1& ...
- Servlet会话管理一(URL重写和表单隐藏域)
会话可以简单的理解为客户端用户打开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器的整个过程称为一个会话.即一个客户端用户和服务器端进行通讯的过程,也是客户端和服务器端之间的数据传 ...
随机推荐
- fcc html5 css 练习2
<form action="/submit-cat-photo" >action属性的值指定了表单提交到服务器的地址 <input type="text ...
- Android FileProvider相关 Failed to find configured root that contains
问题: 使用FileProvider构造SD卡中文件uri时异常 java.lang.IllegalArgumentException: Failed to find configured root ...
- Android开发笔记(5)——方法调用(基础)
转载请注明——博客园igoslly:http://www.cnblogs.com/igoslly/p/6833544.html 在实际方法调用中,程序按顺序逐句执行,直到“}”结束. 为避免程序大 ...
- Caffe2:python -m caffe2.python.operator_test.relu_op_test
1. 进行语句测试时候,出现问题, 设置环境变量CUDA_VISIBLE_DEVICES 参考: cuda设置指定可见方法 在/etc/profile文件或者-/.bashrc末尾添加以下行: exp ...
- CNN结构:色彩空间建模-色彩空间分析
原文: 色彩空间基础 好一个NB的知乎专栏:色彩空间基础 第一章:色彩空间基础 关于色彩分析,引出了专门的数学基础.整个过程给出了完备的数学阐述,虽然没有试验数据,论述的相当精彩. 摘抄出一段: 上 ...
- [文章转载]-我的Java后端书架-江南白衣
我的Java后端书架 (2016年暮春3.0版) 04月 24, 2016 | Filed under 技术 书架主要针对Java后端开发. 3.0版把一些后来买的.看的书添补进来,又或删掉或降级一些 ...
- Python_多线程1(创建线程,简单线程同步)
threading 模块除了包含 _thread 模块中的所有方法外,还提供的其他方法: threading.currentThread(): 返回当前的线程变量. threading.enumera ...
- Android获取SD卡路径/内存的几种方法
Android获取SD卡路径 本篇将会带领大家学习如何获取android路径的几种常见用法,但在我开始bb之前需要大家清楚android中内存和外存之间的区别,下面进行简短介绍:android中的内存 ...
- 用 foreach()循环三维数组
<?php $data = array( 0 => array( '中国' => 'china', '美国' => 'usa', '德国' => ' Germany', ...
- cvpr2016论文
http://openaccess.thecvf.com/ICCV2017.py http://openaccess.thecvf.com/CVPR2017.py http://www.cv-foun ...