要点复习
1 Servlet的定义?
Servlet是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改数据,生成动态Web内容。
狭义的Servlet是指Java语言实现的一个接口,广义的Servlet是指任何实现了这个Servlet接口的类,一般情况下,人们将Servlet理解为后者。
Servlet运行于支持Java的应用服务器中。
从原理上讲,Servlet可以响应任何类型的请求,但绝大多数情况下Servlet只用来扩展基于HTTP协议的Web服务器。最早支持Servlet标准的是JavaSoft的Java Web Server,一些其它的基于Java的Web服务器开始支持标准的Servlet。
2Servlet的定义的四种形式?
public class OneServlet implements Servlet { } //采用接口
public class TwoServlet extends GenericServlet {}//采用继承
public class ThreeServlet extends HttpServlet{}//采用继承
@WebServlet(urlPatterns="/d")
public class fourServlect extends HttpServlet {}采用快键
ALT+/ 在写代码中常用
3 Servlet get与post的区别?
第一点:get请求和post请求的区别:
浏览器使用get请求访问servlet服务器的时候doget方法接收
浏览器使用post请求访问servlet服务器的时候dopost方法接收
第二点
get请求所带的数据是拼接在地址上的,地址的长度是有限的,1kb=1024 一个字母2个字节 512字母
post传输数据是不受限制的
第三点
登录功能一律不允许使用get请求给服务器发送数据
浏览器默认为get请求
4 Servlet的周期?
servlet周期
构造方法(创建servlet 对象执行)
Servlet 初始化后调用 init () 方法。
Servlet 调用 service() 方法来处理客户端的请求。
Servlet 销毁前调用 destroy() 方法 长时间未访问。
最后,Servlet 是由 JVM 的垃圾回收器进行垃圾回收的。
5 Servlet的注册在哪里?
web.xml文件
<!-- 注册oneServlet --> <!-- 第一部分 注册OneServlet--> <servlet> <!-- srvlet 增加名字 --> <servlet-name>OneServlet</servlet-name> <!--注册servlet地址 --> <servlet-class>com.servlet.OneServlet</servlet-class> <load-on-startup>4</load-on-startup> </servlet> <!--第二部分 :设置一个浏览器可以访问的servlet的地址 --> <servlet-mapping> <servlet-name>OneServlet</servlet-name> <!--设置浏览器地址 --> <url-pattern>/a</url-pattern> <!-- 设置地址访问一定加/asd --> </servlet-mapping>
7 Servlet的常用对象有哪些?
ServletConfig
ServletContext 方法的作用
HttpServlet
HttpServletRequest
HttpServletResponseHttpServletRequest
ServletConfig
获取静态数据的二个方式
在web.xml文件中写
<!-- 注意<init-param>写在<servlet>里面 --> <init-param> <param-name>home</param-name> <param-value>二家酒店</param-value> </init-param>
方式二
/** * Servlet implementation class Servlet2 */ @WebServlet(urlPatterns = "/Servlet2",initParams = { @WebInitParam(name="aaaa",value="张三"), @WebInitParam(name="age",value="23"), @WebInitParam(name="height",value="179cm"), @WebInitParam(name="weight",value="60kg") }) public class Servlet2 extends HttpServlet {
ServletContext 方法的作用
ServletContext {con 上下文 }
tomcat 服务器为每个项目创建一个对象
ServletContext
1个web项目只有一个ServletContext对象
ServletContext 服务器开始创建 结束 而消失
ServletContext 作用 同一个项目共享数据。
1 共享静态数据
2 共享动态数据 写在java代码
3 共享文件数据
* 创建对象
* ServletContext sc=getServletContext();获取对象
* sc.setAttribute("pq", "排球"); 接收对象
* 删除对象 sc.removeAttribute("pq");
HttpServle
HttpServlet首先必须读取Http请求的内容。Servlet容器负责创建HttpServlet对象,并把Http请求直接封装到HttpServlet对象中,大大简化了HttpServlet解析请求数据的工作量。HttpServlet容器响应Web客户请求流程如下:
创建项目下图所示
package Aservlet; import java.io.IOException; import java.util.Enumeration; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.xml.ws.Response; public class OneServlet implements Servlet{ public OneServlet() { System.out.println(" Servlet构造方法"); } @Override public void destroy() { System.out.println("Servlet的销毁,释放cpu"); } @Override public ServletConfig getServletConfig() { // TODO Auto-generated method stub return null; } @Override public String getServletInfo() { // TODO Auto-generated method stub return null; } @Override public void init(ServletConfig config) throws ServletException { //增加静态数据;到web.html去定义 /* * 第一种方式 */ String a=config.getInitParameter("hont"); System.out.println(a+"输入值为???"); System.out.println("加载数据信息"); /* * 第二种方式 */ Enumeration<String> e=config.getInitParameterNames(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); String value=config.getInitParameter(key); System.out.println(key+"@@@@@@"+value); } } @Override public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { System.out.println("Servlet处于运行状态"); } }
package Aservlet; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Servlet1 */ @WebServlet(urlPatterns = "/TwoServlet",initParams = { //增加静态数据的第一个学生 @WebInitParam(name="a",value="胡一伤悲"), @WebInitParam(name="age",value="30岁"), @WebInitParam(name="height",value="179cm"), @WebInitParam(name="weight",value="60kg") }) public class TwoServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public TwoServlet() { // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ @Override public void init() throws ServletException { // TODO Auto-generated method stub super.init(); //定义4个静态数据 ServletConfig config=getServletConfig(); String a=config.getInitParameter("a"); String b=config.getInitParameter("age"); String c=config.getInitParameter("height"); String e=config.getInitParameter("weight"); System.out.println(a+"===="+b+"#######"+c+"-----"+e); System.out.println(e); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //设置 发送给浏览器的文本信息HTML文本格式 //response.setContentType("text/html"); response.setContentType("text/html"); //设置发送给浏览器文本utf-8 //response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); //因为浏览器是使用get请求 //所以Servlet do-get //servlet1相打篮球 //1获取ServletContext对象 ServletContext sc=getServletContext(); //获取web.xml文件中的镜头数据 String a=sc.getInitParameter("LQ"); //获得servlet4共享的排球 String b= (String) sc.getAttribute("PQ"); //删除排球 sc.removeAttribute("PQ"); //给浏览器发送数据 response.getWriter().append("Servlet1获得静态数据用来访问lq:"+a); response.getWriter().append("response.getWriter().append方法打印内容"); response.getWriter().append("Servlet1获得静态数据用来访问pq:"+b); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package Aservlet; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Servlet3 */ @WebServlet(urlPatterns = "/TreeServlet",initParams = { @WebInitParam(name="aaa",value="胡三定"), @WebInitParam(name="age",value="67岁"), @WebInitParam(name="height",value="180cm"), @WebInitParam(name="weight",value="60kg"), }) public class TreeServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public TreeServlet() { // TODO Auto-generated constructor stub } @Override public void init() throws ServletException { // TODO Auto-generated method stub super.init(); ServletConfig config=getServletConfig(); String a=config.getInitParameter("aaa"); String b=config.getInitParameter("age"); String c=config.getInitParameter("height"); String e=config.getInitParameter("weight"); System.out.println(a+"===="+b+"========"+c+"======="+e); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //设置 发送给浏览器的文本信息HTML文本格式 //response.setContentType("text/html"); response.setContentType("text/html"); //设置发送给浏览器文本utf-8 //response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); //因为浏览器是使用get请求 //所以Servlet do-get //servlet1相打篮球 //1获取ServletContext对象 ServletContext sc=getServletContext(); //获取web.xml文件中的镜头数据 String a=sc.getInitParameter("LQ"); //给浏览器发送数据 response.getWriter().append("Servlet3获得静态数据用来访问lq:"+a); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package Aservlet; /* * ServletContext {con 上下文 } tomcat 服务器为每个项目创建一个对象 ServletContext 1个web项目只有一个ServletContext对象 ServletContext 服务器开始创建 结束是 没有 ServletContext 作用 同一个项目共享数据。 1 共享静态数据 2 共享动态数据 写在java代码 3 共享文件数据 * 创建对象 * ServletContext sc=getServletContext();获取对象 * sc.setAttribute("pq", "排球"); 接收对象 * 删除对象 sc.removeAttribute("pq"); */ import java.io.IOException; import java.io.OutputStream; import javax.servlet.ServletContext; 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 Servlet4 */ @WebServlet("/fourServlet") public class fourServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() * 由个人共享出来的动态数据 */ public fourServlet() { super(); // TODO Auto-generated constructor stub } /** * 创建对象 ServletContext sc=getServletContext();获取对象 * sc.setAttribute("pq", "排球"); 接收对象 * 删除对象 sc.removeAttribute("pq"); * * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //servlet4共享班级 共享数据 //共享动态数据 response.setContentType("text/html"); //设置发送给浏览器文本utf-8 //response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); //获取ServletContext 对象 ServletContext sc=getServletContext(); //将排球共享数据 setAttribute方法共享数据。 //在java中object是任何数据类型的父类。 //身份的多种类型;向上转型 sc.setAttribute("pq", "排球"); sc.setAttribute("yq", "羽毛球"); sc.setAttribute("rd", "读书"); //身份的多种类型;向上转型(强制转型) //获得动态数据 //疯狂java讲义 //getimitParameter只能获得静态数据 String a= (String) sc.getAttribute("pq"); String b= (String) sc.getAttribute("yq"); String c= (String) sc.getAttribute("rd"); response.getWriter().append("Servlet4获得动态数据 "+a); response.getWriter().append("Servlet4获得动态数据 "+b); response.getWriter().append("Servlet4获得动态数据 "+c); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package Aservlet; import java.io.IOException; import java.io.InputStream; import java.util.Set; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Servlet1 */ @WebServlet(urlPatterns = "/FiveServlet5", initParams = { // 增加静态数据的第一个学生 @WebInitParam(name = "a", value = "文件下载"), }) public class FiveServlet5 extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public FiveServlet5() { // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ @Override public void init() throws ServletException { // TODO Auto-generated method stub super.init(); // 定义4个静态数据 ServletConfig config = getServletConfig(); String a = config.getInitParameter("a"); System.out.println(a); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); // 获得text.txt完整路径 ServletContext sc = getServletContext(); // 指定文件名称获取完整路径 String path = sc.getRealPath("text.txt"); // 控制台打印输出文件完整路径 System.out.println(path); // 指定文件夹 Set<String> oSet = sc.getResourcePaths("/WEB-INF"); for (String string : oSet) { System.out.println(string); } // response.getWriter().append("Served at: ").append(request.getContextPath()); response.getWriter().append("Servlet1获得静态数据用来访问lq:"); response.getWriter().append("response.getWriter().append方法打印内容"); response.getWriter().append("Servlet1获得静态数据用来访问pq:"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package Aservlet; import java.awt.image.RescaleOp; import java.io.IOException; import java.io.OutputStream; 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 Servlet1 */ @WebServlet("/SxiServlet") public class SxiServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public SxiServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // response 回复 当浏览器根据地址发送请求servlet.servlet 使用response对象回复对象 //设置servlet发送的数据支撑HTML格式 response.setContentType("text/html"); //设置发送servlet 浏览html格式 utf-8 response.setCharacterEncoding("UTF-8"); //response 回复 内容/数据/消息 String info="hellow world java"; String info1="你好,<span style='color:yellow'> java jsp</span>"; //使用response获得输出 OutputStream os =response.getOutputStream(); os.write(info.getBytes()); os.write(info1.getBytes()); } /* * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) * */ }
package Aservlet; import java.io.IOException; import java.io.InputStream; import java.util.Set; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.annotation.WebInitParam; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Servlet1 */ @WebServlet(urlPatterns = "/SevenServlet",initParams = { //增加静态数据的第一个学生 @WebInitParam(name="a",value="胡五伤悲"), @WebInitParam(name="age",value="47岁"), @WebInitParam(name="height",value="169cm"), @WebInitParam(name="weight",value="60kg") }) public class SevenServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public SevenServlet() { // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ @Override public void init() throws ServletException { // TODO Auto-generated method stub super.init(); //定义4个静态数据 ServletConfig config=getServletConfig(); String a=config.getInitParameter("a"); String b=config.getInitParameter("age"); String c=config.getInitParameter("height"); String e=config.getInitParameter("weight"); System.out.println(a+"===="+b+"#######"+c+"-----"+e); System.out.println(e); System.out.println("创建静态数据的方法"); System.out.println("ServletConfig config=getServletConfig();"); System.out.println("访问静态数据的方法"); System.out.println("String a=config.getInitParameter(\"a\");"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); // 获得text.txt完整路径 ServletContext sc=getServletContext(); //指定文件名称获取完整路径 String path=sc.getRealPath("A.txt"); //控制台打印输出文件完整路径 System.out.println(path); // 指定文件夹 Set<String> oSet=sc.getResourcePaths("/WEB-INF"); for (String string : oSet) { System.out.println(string); } //response.getWriter().append("Served at: ").append(request.getContextPath()); response.getWriter().append("Servlet1获得静态数据用来访问lq:"); response.getWriter().append("response.getWriter().append方法打印内容"); response.getWriter().append("Servlet1获得静态数据用来访问pq:"); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
运行结果