上节回顾和零碎知识
Servlet
和HttpServlet
的几个重要函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| public interface Servlet { init() destroy() getServletConfig() service(ServletRequest req, ServletResponse res) throws ServletException, IOException() } public interface HttpServlet { public void service(HttpServletRequest req, HttpServletResonpse res) throws IOException, ServletException { String method = req.getMethod(); if("POST".equals(method)) { doPost(req, res); } else if("GET".equals(method)) { doGet(req, res); } ...... res.setContentType("text/html"); ...... } }
|
有关MIME:点击查看
get
默认缓存,post
默认不缓存,但客户端和服务端都可以设置。
迭代器要好好复习一下。
今天所学的几个重要函数与接口
HttpServlet
中的service
, doGet
, doPost
, …
HttpServletRequest
中的getParameter
HttpServletResponse
中的getWriter
, getOutputStream
, setHeader
, addCookie
, getCookies
Servlet
中的ServletConfig
, ServletContext
, HttpSession
, RequestDispatcher
若下文未提到,需要翻阅文档自学。
以下这串代码没有实际的应用意义
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package com.abc.servlet import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloServlet extends HttpServlet { public void doGet(HttpServletResponse res, HttpServletResponse res) throws ServletException, IOException { ServletConfig config = getServletConfig(); ServletContext application = config.getServletContext(); ServletContext sc = getServletContext(); res.setContentType("text/html;charset=utf-8"); res.getWriter().println("hello world"); String wd = req.getParameter("wd"); String[] abc = req.getParameterValues("abc"); HttpSession session = res.getSession(); session的内容可以存在客户端的cookie,或者url重写 } }
|