欢迎来到Jsp编程课时四
模块一:Servlect的注册回顾与周期回顾。
知识点:
代码编译:
package com.servlet; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class OneServlet implements Servlet { public OneServlet() { System.out.println("OneServlet的抽象方法"); } @Override public void destroy() { System.err.println("OneServlet释放内存的方法内容"); } @Override public ServletConfig getServletConfig() { System.out.println("获得静态数据的方法"); return null; } @Override public String getServletInfo() { // TODO Auto-generated method stub System.out.println("getServletInfo() 的 Servlect的方法内容"); return null; } @Override public void init(ServletConfig arg0) throws ServletException { // TODO Auto-generated method stub System.out.println("Servlect init 的方法内容"); } @Override public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { // TODO Auto-generated method stub //等待浏览器访问 System.out.println("servlect处于运行状态"); } }
wel.xml文件
模块二:Servlect对象@1ServletConfig静态数据
package com.Servlet; import java.io.IOException; import javax.servlet.ServletConfig; 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 Servlet2 */ @WebServlet(urlPatterns = "/B",initParams = { @WebInitParam(name="aaaa",value="张三"), @WebInitParam(name="age",value="23"), @WebInitParam(name="height",value="179cm"), @WebInitParam(name="weight",value="60kg"), @WebInitParam(name="x",value="我是第二种获取静态数据的方式晕染") }) public class Servlet2 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Servlet2() { System.out.println("Servlet2值一送到"); } @Override public void init() throws ServletException { // TODO Auto-generated method stub super.init(); //定义五个静态数据 ServletConfig config=getServletConfig(); String a=config.getInitParameter("aaaa"); String b=config.getInitParameter("age"); String c=config.getInitParameter("height"); String e=config.getInitParameter("weight"); String f=config.getInitParameter("x"); System.out.println(a+"===="+b+"#######"+c+"-----"+e); // String d=config.getInitParameter("money"); System.out.println(d); System.out.println(e); System.out.println(f+"我是第二种获取静态数据的方式晕染"); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @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); } }
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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Day03jsp</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>B.jsp</welcome-file> <!-- <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> --> </welcome-file-list> <!-- --> <!-- 在web.xml文件中定义静态数据的方式 --> <servlet> <servlet-name>Servlet1</servlet-name> <servlet-class>com.Servlet.Servlet1</servlet-class> <!-- 注意<init-param>写在里面 --> <init-param> <param-name>home</param-name> <param-value>二家酒店</param-value> </init-param> <init-param> <param-name>money</param-name> <param-value>200万</param-value> </init-param> <init-param> <param-name>h</param-name> <param-value>173cm</param-value> </init-param> <init-param> <param-name>a</param-name> <param-value>我是静态数据的获取方式a</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Servlet1</servlet-name> <!--设置浏览器地址 --> <url-pattern>/A</url-pattern> <!-- 设置地址访问一定加/asd --> </servlet-mapping> <servlet> <servlet-name>Servlet4 </servlet-name> <servlet-class>com.Servlet.Servlet4</servlet-class> <!--静态数据写人web.xml --> <init-param> <param-name>h</param-name> <param-value>10000</param-value> </init-param> <init-param> <param-name>b</param-name> <param-value>2000</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Servlet4</servlet-name> <url-pattern>/D</url-pattern> </servlet-mapping> </web-app>
模块三Servlect对象二@ServletContext.共享动态数据,文件数据,静态数据。
运行图
package com.servlet; 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 = "/ServletA",initParams = { //增加静态数据的第一个学生 @WebInitParam(name="a",value="我是获取静态数据的方式") }) /** * 12/4回顾获取静态数据和学习动态数据的方式 * @author MZFAITHDREAM * */ public class Servlet1 extends HttpServlet { private static final long serialVersionUID = 1L; /** * Default constructor. */ public Servlet1() { // 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); } /* * 浏览器none do-get请求 * 第二个对象 ServletContext {con 上下文 } tomcat 服务器为每个项目创建一个对象 ServletContext 1个web项目只有一个ServletContext对象 ServletContext 服务器开始创建 tomcat服务器 结束是 没有 ServletContext 作用 给同一个项目的所有同一个项目共享数据。 1 共享静态数据 2 共享动态数据 3 共享文件数据 * */ 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"); String c= (String) sc.getInitParameter("dt"); //获得servlet4共享的排球 String b= (String) sc.getAttribute("pq"); String d= (String) sc.getAttribute("as"); //删除排球 sc.removeAttribute("pq"); //给浏览器发送数据 response.getWriter().append("<h1>Servlet1获得静态数据用来访问lq:"+a+"</h1><br>"); response.getWriter().append("<h1>Servlet1获得静态数据用来访问dt:"+c+"</h1><br>"); response.getWriter().append("<h1>response.getWriter().append方法打印内容</h1>"); response.getWriter().append("<h1>Servlet4获得动态态数据用来访问pq:"+b+"</h1><br>"); response.getWriter().append("<h1>Servlet4获得动态态数据用来访问as:"+d+"</h1><br>"); } /** * @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 com.servlet; /* * 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 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("/Servlet4") public class Servlet4 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() * 由个人共享出来的动态数据 */ public Servlet4() { 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("as", "我是共享的数据as采用的是setAttribut获得"); //身份的多种类型;向上转型(强制转型) //获得动态数据 //疯狂java讲义 //getimitParameter只能获得静态数据 String a= (String) sc.getAttribute("pq"); String b= (String) sc.getAttribute("as"); response.getWriter().append("Servlet4获得动态数据 "+a); response.getWriter().append("Servlet4获得动态数据 "+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); } }
<?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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Day04JSp</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>B.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自己静态数据<init-param>--> <!--动态数据的获取方式 --> <context-param> <param-name>lq</param-name> <param-value>篮球</param-value> </context-param> <context-param> <param-name>zq</param-name> <param-value>足球</param-value> </context-param> <context-param> <param-name>dt</param-name> <param-value>恭喜你获得了我的动态数据的内容</param-value> </context-param> </web-app>
模块四Servlect对象三Response对象。
运行图(仔细观察)
代码编译
package com; import java.io.IOException; 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("/Servlet1") public class Servlet1 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Servlet1() { super(); System.out.println("Servlet1 在运行HttpServletRequest 的方法"); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获得浏览器的请求,得到请求信息 //得到请求的URL地址 String a=request.getRequestURL().toString(); System.out.println(a); //得到请求的数据 String b=request.getRequestURI(); System.out.println(b); //得到请求的URL地址中附带的参数 String c=request.getQueryString(); System.out.println(c); //得到来访者的IP地址 String d=request.getRemoteAddr(); System.out.println(d); //来访者的主机名 String e=request.getRemoteHost(); System.out.println(e); //来访者的端口号 int f=request.getRemotePort(); System.out.println(f); //得到请求URL地址时使用的方法(重要) String h=request.getMethod(); System.out.println(h); //获取WEB服务器的IP地址 String l=request.getLocalAddr(); System.out.println(l); //获取WEB服务器的主机名 String n=request.getLocalName(); System.out.println(n); //获取WEB服务器的端口号 int m=request.getLocalPort(); System.out.println(m); } /** * @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 com; import java.io.IOException; import java.io.PrintWriter; 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("/Servlet4") public class Servlet4 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Servlet4() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter pw=response.getWriter(); String a=(String) request.getAttribute("apple"); String b=(String) request.getAttribute("banner"); String c =(String) request.getAttribute("ma"); pw.print("<h1 style=\"color: pink;\">收到Servlet3的请求,回复消息给浏览器,任务已完成,获得Servlet3携带的数据为:"+a+"</h1>"); pw.print("<h1 style=\"color: red;\">收到Servlet3的请求,回复消息给浏览器,任务已完成,获得Servlet3携带的数据为:"+b+"</h1>"); pw.print("<h1 style=\"color: yellow;\">收到Servlet3的请求,回复消息给浏览器,任务已完成,获得Servlet3携带的数据为:"+c+"</h1>"); } /** * @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); } }