【为什么要用Servlet】
Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面。 它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器响应(HTTP服务器上的数据库或应用程序)的中间层。 Servlet是位于Web 服务器内部的服务器端的Java应用程序,与传统的从命令行启动的Java应用程序不同,Servlet由Web服务器进行加载,该Web服务器必须包含支持Servlet的Java虚拟机。
【Servlet】
Servlet(Server Applet),全称Java Servlet,未有中文译文。是用Java编写的服务器端程序。其主要功能在于交互式地浏览和修改数据,生成动态Web内容
Servlet运行于支持Java的应用服务器中。从实现上讲,Servlet可以响应任何类型的请求,但绝大多数情况下Servlet只用来扩展基于HTTP协议的Web服务器。
【HttpServlet】
1、Servlet是服务器小程序的意思,使用一个HTML表单发送和接收数据
2、用来完成B/S架构下,客户端请求和响应的处理
3、跨平台,通常以多线程方式运行
4、Servlet API为Servlet提供了统一的编程接口
5、Servlet一般在容器中运行,Servlet的生命周期由容器管理
6、常见的Servlet容器:Tomcat、Resin、Jetty
2、用来完成B/S架构下,客户端请求和响应的处理
3、跨平台,通常以多线程方式运行
4、Servlet API为Servlet提供了统一的编程接口
5、Servlet一般在容器中运行,Servlet的生命周期由容器管理
6、常见的Servlet容器:Tomcat、Resin、Jetty
【HttpServlet详解】
1、HttpServet继承了GenericServlet
2、GenericServlet类是所有Servlet的祖先类
3、HttpServlet主要方法
4、请求处理方法:(分别对应http协议的7种请求)
1、doGet 响应Get请求,常用
2、doPost 响应Post请求,常用
3、doPut 用于http1.1协议
4、doDelete 用于http1.1协议
5、doHead 仅响应Get请求的头部。
6、doOptions 用于http1.1协议
7、doTrace 用于http1.1协议
service方法,当请求一个Servlet首先到达该方法,该方法再分发到相应的处理方法
2、GenericServlet类是所有Servlet的祖先类
3、HttpServlet主要方法
4、请求处理方法:(分别对应http协议的7种请求)
1、doGet 响应Get请求,常用
2、doPost 响应Post请求,常用
3、doPut 用于http1.1协议
4、doDelete 用于http1.1协议
5、doHead 仅响应Get请求的头部。
6、doOptions 用于http1.1协议
7、doTrace 用于http1.1协议
service方法,当请求一个Servlet首先到达该方法,该方法再分发到相应的处理方法
【HttpServletRequest和HttpServletResponse】
HttpServletRequest
包装客户端提交过来的所有数据:客户端的IP地址、客户端的表单数据、Cookies信息
HttpServletResponse
包装了向客户端写出的数据:如将学生信息输出给客户端,这样客户端就可以看到从数据库中取出的学生了、可以向客户端输出图片等等
包装客户端提交过来的所有数据:客户端的IP地址、客户端的表单数据、Cookies信息
HttpServletResponse
包装了向客户端写出的数据:如将学生信息输出给客户端,这样客户端就可以看到从数据库中取出的学生了、可以向客户端输出图片等等
【Servlet 生命周期】
生命全过程:1、加载Servlet并实例化 new
2、初始化 init
3、处理请求 service doGet doPost
4、退出服务 destroy()
注意:Servlet只实例化一次、init方法也只执行一次、Servlet不是线程安全的
TestLifeCycleServlet
public class TestLifeCycleServlet
extends HttpServlet
{
public TestLifeCycleServlet()
{
System.out.println("--------TestLifeCycleServlet()----------");
}
public void init()
throws ServletException
{
System.out.println("-----------init-------------");
}
public void doGet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
throws ServletException, IOException
{
System.out.println("------------doGet-------------");
}
}
【Cookie】
1、Cookie:保存到客户端的一个文本文件,与特定客户相关
2、Cookie以“名-值”对的形式保存数据
3、创建Cookie:new Cookie(name,value)
4、可以使用Cookie 的setXXX方法来设定一些相应的值
setName(String name)/getName()
setValue(String value)/getValue()
setMaxAge(int age)/getMaxAge()
利用HttpServletResponse的addCookie(Cookie)方法将它设置到客户端
利用HttpServletRequest的getCookies()方法来读取客户端的所有Cookie,返回一个Cookie数组
5、服务器可以向客户端写内容,只能是文本内容
6、客户端可以阻止服务器写入,禁用Cookies
7、只能读取自己webapp写入的东西
8、Cookie分为两种
属于窗口/子窗口(放在内存中的)
属于文本(有生命周期的)
9、一个servlet/jsp设置的cookies能够被同一个路径下面或者子路径下面的 servlet/jsp读到,其他路径无法读取
2、Cookie以“名-值”对的形式保存数据
3、创建Cookie:new Cookie(name,value)
4、可以使用Cookie 的setXXX方法来设定一些相应的值
setName(String name)/getName()
setValue(String value)/getValue()
setMaxAge(int age)/getMaxAge()
利用HttpServletResponse的addCookie(Cookie)方法将它设置到客户端
利用HttpServletRequest的getCookies()方法来读取客户端的所有Cookie,返回一个Cookie数组
5、服务器可以向客户端写内容,只能是文本内容
6、客户端可以阻止服务器写入,禁用Cookies
7、只能读取自己webapp写入的东西
8、Cookie分为两种
属于窗口/子窗口(放在内存中的)
属于文本(有生命周期的)
9、一个servlet/jsp设置的cookies能够被同一个路径下面或者子路径下面的 servlet/jsp读到,其他路径无法读取
SetCookies
public class SetCookies
extends HttpServlet
{
public void doGet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
throws ServletException, IOException
{
Cookie localCookie1 = new Cookie("password", "123");
paramHttpServletResponse.addCookie(localCookie1);
Cookie localCookie2 = new Cookie("client_ip", paramHttpServletRequest.getRemoteAddr());
localCookie2.setMaxAge(3600);
paramHttpServletResponse.addCookie(localCookie2);
paramHttpServletResponse.getWriter().println("SetCookies OK!");
}
}
ShowCookies
public class ShowCookies
extends HttpServlet
{
public void doGet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
throws ServletException, IOException
{
Cookie[] arrayOfCookie = paramHttpServletRequest.getCookies();
for (int i = 0; i < arrayOfCookie.length; i++)
{
Cookie localCookie = arrayOfCookie[i];
paramHttpServletResponse.getWriter().println(localCookie.getName() + ", " + localCookie.getValue());
}
}
}
【Servlet Session】
1、服务器的一块内存(存key-value),可以保存信息到服务器端
2、和客户端窗口对应(子窗口)
3、客户端和服务器有对应的SessionID
4、客户端向服务器端发送SessionID的时候两种方式:
cookie(内存cookie)
rewriten URL
5、浏览器禁掉cookie,就不能使用session
6、如果想安全的使用session(不论客户端是否禁止cookie),只能使用URL重写(大大增加编程负担),所以很多网站要求客户端打开cookie
2、和客户端窗口对应(子窗口)
3、客户端和服务器有对应的SessionID
4、客户端向服务器端发送SessionID的时候两种方式:
cookie(内存cookie)
rewriten URL
5、浏览器禁掉cookie,就不能使用session
6、如果想安全的使用session(不论客户端是否禁止cookie),只能使用URL重写(大大增加编程负担),所以很多网站要求客户端打开cookie
SetSessionServlet
public class SetSessionServlet
extends HttpServlet
{
public void doGet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
throws ServletException, IOException
{
HttpSession localHttpSession = paramHttpServletRequest.getSession(true);
localHttpSession.setAttribute("ip", paramHttpServletRequest.getRemoteAddr());
paramHttpServletResponse.getWriter().println("SetSession OK!");
}
}
ShowSessionServlet
public class ShowSessionServlet
extends HttpServlet
{
public void doGet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
throws ServletException, IOException
{
HttpSession localHttpSession = paramHttpServletRequest.getSession(true);
String str = (String)localHttpSession.getAttribute("ip");
paramHttpServletResponse.getWriter().println("ip=" + str);
}
}
UrlRewriteSession
public class UrlRewriteSession
extends HttpServlet
{
public void doGet(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
throws ServletException, IOException
{
HttpSession localHttpSession = paramHttpServletRequest.getSession(true);
paramHttpServletResponse.getWriter().println("<a href='" + paramHttpServletResponse.encodeURL(paramHttpServletRequest.getRequestURL().toString()) + "'>UrlRewrite</a>");
}
}