一.开发思路
1.需求
2.设计(图文)
- 先将功能拆分成若干请求
- 每个请求画出程序执行的过程图
根据操作推导出本功能所包含的请求
3.开发
二.资费查询
三.问题
1.如何格式化jsp上显示的时间?
2.路径相关问题
- 在jsp上引用样式文件和图片为什么没写../
- 为什么要将jsp放在WEB-INF下
- 为什么要将静态资源放在webapp下
3.编码问题
- 为什么jsp上有2处声明了编码
- pageEncoding是给服务器翻译jsp时用的
- meta中的编码是给浏览器加载HTML时用的
四.增加资费
FindCostServlet.java
package web;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class FindCostServlet extends HttpServlet {
@Overrideprotected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {Cookie[] cs = req.getCookies();if(cs != null) {res.setContentType("text/html;charset=utf-8");PrintWriter out = res.getWriter();for(Cookie c : cs) {out.println(c.getName()+":"+c.getValue());}out.close();}}}
IndexServlet.java
package web;import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class IndexServlet extends HttpServlet {
@Overrideprotected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {Cookie[] cs = req.getCookies();if(cs != null) {res.setContentType("text/html;charset=utf-8");PrintWriter out = res.getWriter();for(Cookie c : cs) {String value = URLDecoder.decode(c.getValue(),"utf-8");out.println(c.getName()+":"+value);}out.close();}}}
LoginServlet.java
package web;import java.io.IOException;
import java.net.URLEncoder;import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class LoginServlet extends HttpServlet {
@Overrideprotected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {String code = req.getParameter("code");Cookie c1 = new Cookie("code",code);c1.setMaxAge(600000);res.addCookie(c1);Cookie c2 = new Cookie("city",URLEncoder.encode("北京", "utf-8"));res.addCookie(c2);Cookie c3 = new Cookie("name","Tarena");c3.setPath("/jsp3");res.addCookie(c3);}}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"><display-name>jsp3</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><servlet><servlet-name>login</servlet-name><servlet-class>web.LoginServlet</servlet-class></servlet><servlet-mapping><servlet-name>login</servlet-name><url-pattern>/main/login</url-pattern></servlet-mapping><servlet><servlet-name>index</servlet-name><servlet-class>web.IndexServlet</servlet-class></servlet><servlet-mapping><servlet-name>index</servlet-name><url-pattern>/main/index</url-pattern></servlet-mapping><servlet><servlet-name>findCost</servlet-name><servlet-class>web.FindCostServlet</servlet-class></servlet><servlet-mapping><servlet-name>findCost</servlet-name><url-pattern>/cost/find</url-pattern></servlet-mapping></web-app>