@@@@servlect常用对象对象的总结@@@@ NO-1对象@1 ServletConfig对象。 NO-2用途:获取静态数据的 NO-3静态数据获得方发一
Servlet1 com.Servlet.Servlet1 home 二家酒店 money 200万 h 173cm Servlet1 /S NO-4静态数据获得方发二 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> Insert title here
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; @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 { private static final long serialVersionUID = 1L; 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"); System.out.println(a+"===="+b+"#######"+c+"-----"+e); // String d=config.getInitParameter("money"); System.out.println(d); System.out.println(e); }对象@@2 ServletContext对象。 NO-1用途:获取共享静态数据,获取共享动态数据,获取共享文件数据. 方法一在web.xml
方法二lq 篮球 zq 足球
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获得静态数据用 1 动态数据获取的方法
·
response.setCharacterEncoding("UTF-8"); //获取ServletContext 对象 ServletContext sc=getServletContext(); //将排球共享数据 setAttribute方法共享数据。 //在java中object是任何数据类型的父类。 //身份的多种类型;向上转型 sc.setAttribute("pq", "排球"); //删除1动态数据 sc.removeAttribute("pq"); //身份的多种类型;向上转型(强制转型) //获得动态数据 //疯狂java讲义 //getimitParameter只能获得静态数据 String a= (String) sc.getAttribute("pq"); response.getWriter().append("Servlet4获得动态数据 "+a); }2 图片数据的共享
ServletContext sc=getServletContext(); //指定文件名称获取完整路径 String path=sc.getRealPath("text.txt"); //控制台打印输出文件完整路径 System.out.println(path); //将text.txt读取出来 //IO inputStream /outputStream输出流。 //网上下载的图片以自己为主场输入 //总结:始终以自己为主场,观察数据流向。 InputStream is=sc.getResourceAsStream("/text.txt"); //分析:1.假设text.txt是一部一百万的小说 //当读取大量数据的时候,采取分段读取,部分读取 //一般情况下,一次读取1024字节byte=1kb 512个字 byte[] bytes=new byte[1024]; //分析:2.数据非常少的时候 //定义一个变量,用于记录每一次读取的字节数 //判断记录的字节数是否小于1024,表示快读取结束 int count=-1;//-1标记为读取结束 //定义一个字符串用于汇总每次读取的部分字符串内容 String sum=""; //循环读取 while ((count=is.read(bytes))!=-1) { //需要将每一次读取的字节数组转换成字符串 String s=new String(bytes, 0, count); sum+=s; } System.out.println(sum); // 指定文件夹 Set3 获取文件数据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:"); }
ServletContext sc=getServletContext(); //指定文件名称获取完整路径 String path=sc.getRealPath("text.txt"); //控制台打印输出文件完整路径 System.out.println(path); //将text.txt读取出来 //IO inputStream /outputStream输出流。 //网上下载的图片以自己为主场输入 //总结:始终以自己为主场,观察数据流向。 InputStream is=sc.getResourceAsStream("/text.txt"); //分析:1.假设text.txt是一部一百万的小说 //当读取大量数据的时候,采取分段读取,部分读取 //一般情况下,一次读取1024字节byte=1kb 512个字 byte[] bytes=new byte[1024]; //分析:2.数据非常少的时候 //定义一个变量,用于记录每一次读取的字节数 //判断记录的字节数是否小于1024,表示快读取结束 int count=-1;//-1标记为读取结束 //定义一个字符串用于汇总每次读取的部分字符串内容 String sum=""; //循环读取 while ((count=is.read(bytes))!=-1) { //需要将每一次读取的字节数组转换成字符串 String s=new String(bytes, 0, count); sum+=s; } System.out.println(sum); //response.getWriter().append("Served at: ").append(request.getContextPath()); response.getWriter().append("Servlet1获得静态数据用来访问lq:"); response.getWriter().append("response.getWriter().append方法打印内容"); response.getWriter().append("Servlet1获得静态数据用来访问pq:");
4 静态数据的获取ServletContext //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);对象@@3 HttpServletResponse 对象。 HttpServlet调用HttpResponse的有关方法,生成响应数据: 用法一
//设置servlet发送的数据支撑HTML格式 response.setContentType("text/html"); //设置发送servlet 浏览html格式 utf-8 response.setCharacterEncoding("UTF-8"); PrintWriter pw=response.getWriter(); String info1="你好世界"; pw.print(info1);
用法二生成图片的下载
// 使用对象完成图片的下载 //设置servlet发送的数据支撑HTML格式 response.setContentType("text/html"); //设置发送servlet 浏览html格式 utf-8 response.setCharacterEncoding("UTF-8"); //设置响应头 response.setHeader("content-disposition", "attachment;filenname=flower.jpg"); ServletContext sa=getServletContext(); String path=sa.getRealPath("/imges/yu.png"); InputStream is =new FileInputStream(path); byte [] bytes =new byte[1024]; int count=-1; OutputStream os=response.getOutputStream(); while((count=is.read(bytes))!=-1) { os.write(bytes,0,count); }用法三生成随机数
response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); response.setHeader("refresh", "5"); //设置要求浏览器 int i=(int)(Math.random()*900000+100000); int a=(int)(Math.random()*9000+1000); int c=(int)(Math.random()*900+100); System.out.println("@@@@"+(new Random().nextInt(900000)+100000)); System.out.println(new Random().nextInt(100)); System.out.println(Math.random()); PrintWriter pw =response.getWriter(); pw.print("你的支付宝到账1000万元,需要的码号为:"+i+",打死也不说 "";"); pw.print("你的QQ到账1000万元,需要的码号为:"+c+",打死也不说 "";"); pw.print("你的微信到账1000万元,需要的码号为:"+a+",打死也不说"); pw.print("你的手机支付宝到账1000万"+i+"");用法四 response.sendRedirect("Servlet2");重定向 重定向:属于浏览器的跳转.超链接
浏览地址发生改变
重定向为二次请求。
重定向可以访问不同服务器的跳转。 对象@4HttpServletRequest对象。 HttpServlet调用HttpResponse的有关方法,生成响应数据,用于服务器上接收浏览器发送过来的数据信息
HttpResponse一些用法 //获得浏览器的请求,得到请求信息 //得到请求的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); }作用二
//获得浏览器发送过来的姓名和年龄的数据 String a=request.getParameter("name"); String b=request.getParameter("age"); String c=request.getParameter("phone"); String d=request.getParameter("QQ"); String e=request.getParameter("wexing"); System.out.println("获得浏览器发送给Servlet的数据为:"+a+"--"+b+"--"+c); //当浏览器发送的数据是同一个名称多个不同的数据 //servlet需要使用以下这个方法去接收这种数据 String[] ss=request.getParameterValues("hobby"); System.out.println(Arrays.toString(ss)); //tomcat服务器的编码格式是:ISO-8859-1 //注意:servlet获得浏览器表单发送的数据时, //获得的名称一定要和表单上文本框定义的name属性中的名称保持一致 //当浏览器发送大量数据给servlet的时候 //如果使用getParameter一个个获得,效率太慢 //使用以下方法可以节省代码,加快开发速度 Enumeration作用三 请求转发e1= request.getParameterNames(); while (e1.hasMoreElements()) { //循环获得的是浏览器发给servlet的数据的名称 String name=e1.nextElement(); if (name.equals("hobby")) { String[] str=request.getParameterValues(name); System.out.println(name+":"+Arrays.toString(str)); }else { String value=request.getParameter(name); System.out.println(name+":"+value); } } //使用Map集合获得浏览器发送的所有数据 Map map=request.getParameterMap(); for (Entry entry : map.entrySet()) { String name=entry.getKey(); String[] values=entry.getValue(); System.out.println(name+"%"+Arrays.toString(values)); } System.out.println("运行完成"); System.out.println("http://localhost:8080/Day06-2/Servlet2?name=hubinjkf&age=23&phone=16745637856&qq=2675648967&wexing=16745679076&hobby=%E7%9C%8B%E4%B9%A6&hobby=%E7%9C%8B%E5%B0%8F%E5%AD%A6%E5%A6%B9&hobby=%E5%AD%A6%E4%B9%A0mysql"); }
request.getRequestDispatcher("Servlet4") .forward(request, response); 请求转发和重定向区别:对象@5 cookie对象。对象@6 session对象。
重定向跳转浏览器地址会发生变化,
请求转发浏览器地址不会发生变化
重定向是两次请求,请求转发是一次请求
重定向属于浏览器跳转,请求转发属于服务器内部跳转
重定向跳转页面是不能发送数据,
请求转发是可以携带数据的
1.请求转发是在服务器完成的,重定向是在浏览器完成的
2.请求转发的速度快,重定向速度慢
3.请求转发的是同一次请求,重定向是二次不同的请求
4.请求转发地址栏没有变化,重定向地址栏有变化
5.转发必须是在同一台服务器下完成,重定向可以在不同的服务器下完成的
回话管理 会话>>同一个浏览器与服务器之间在一段时间多次请求称一次会话, 浏览器关闭,会话结束。 cookie>> :的概念 cookie的本意 cookie是客户端技术,程序把每个用 户数据以cookie的形式写给用户各自浏览器 cookie
Session, 类名HttpSession cookie的概念 cookie的本意是“小甜饼”,我们俗称“曲奇饼” cookie是客户端技术,程序把每个用户的数据以cookie的形式写给用户各自的浏览器。 当用户使用浏览器再去访问服务器的时候,就会带着各自的数据去。这样,web资源处理的就是用户各自的数据。 1.一个cookie只能标识一种信息,它至少含有一个标识该信息的名称(name)和值(value)。 2.一个web网站可以给一个web浏览器发送多个cookie,一个浏览器也可以存储多个cookie。 3.浏览器一般只允许存放300个cookie,每个网站最多存放20个cookie,每个cookie的大小限制为4kb。 4.如果创建了一个cookie,并将他发送到了浏览器,默认情况下他是一个会话级别的cookie(即存储在浏览器的内存中),用户退出浏览器之后即被删除。 Session的概念 HttpSession对象 服务器为每个会话创建一个HttpSession对象 每个会话对象都有一个唯一的ID 把 用户的数据保存在相应的HttpSession对象内cookie对象
package com.Servlect; import java.io.IOException; import java.io.PrintWriter; import java.net.URLDecoder; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/Servlet1") public class servlect1 extends HttpServlet { private static final long serialVersionUID = 1L; public servlect1() { super(); System.out.println("servlect在运行中使用cookie访问"); // TODO Auto-generated constructor stub } @Override public void init() throws ServletException { // TODO Auto-generated method stub super.init(); System.out.println("servlectinit方法"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter pw=response.getWriter(); //浏览器通过地址访问服务器的servelt //servlet要获得该浏览器所携带的所有cookie对象 cookie[] cookies=request.getcookies(); if (cookies!=null) { //循环遍历每一个cookie,直到找到servlet能识别的cookie值为止 //tag,china for (int i = 0; i < cookies.length; i++) { //通过cookie中存储的数据的名称 String name=cookies[i].getName(); if ("AAA".equals(name)) { //这个cookie是服务器要找的东西,从cookie中获得中文数据需要进行解码 String value=URLDecoder.decode(cookies[i].getValue(), "UTF-8"); System.out.println(value); pw.print(value); break; } } }else { //没有cookie,说明当前浏览器以前没有访问过服务器 //创建cookie对象,存储中文数据的时候,需要对数据进行转码 cookie cookie=new cookie("AAA", URLEncoder.encode("bbbbbbbb", "UTF-8")); //设置浏览器退出后,cookie还能继续存在内存中的时间 cookie.setMaxAge(30); response.addcookie(cookie); pw.print("服务器给浏览器创建了一个cookie对象"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
Session对象
package com.Servlect; 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; import javax.servlet.http.HttpSession; @WebServlet("/Servlet2") public class Servlet2 extends HttpServlet { private static final long serialVersionUID = 1L; public Servlet2() { super(); // TODO Auto-generated constructor stub System.out.println("session servlect2在运行中"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter pw=response.getWriter(); //档浏览器访问服务器的servlet的时候,需要检查该浏览器的session对象是否是刚刚创建的 HttpSession session=request.getSession(); //session.isNew()判断session到值是否为空 if (session.isNew()) { //session是刚刚创建的 //浏览器是第一次访问服务器 pw.print("浏览器是第一次访问服务器"); //设置三个动态数据 session.setAttribute("text1", "一等奖学金"); session.setAttribute("name", "hug"); session.setAttribute("pw", "12345"); //删除指定的数据 //session.removeAttribute("text1"); //当session中存放大量数据的时候,如果需要全部销毁,直接干掉session对象 //session.invalidate(); }else { String info=(String) session.getAttribute("text1"); String info1=(String) session.getAttribute("name"); String info2=(String) session.getAttribute("pw"); System.out.println("浏览器不是第一次访问服务器的文件内容"); System.out.println("浏览器不是第一次访问服务器的名字"); System.out.println("浏览器不是第一次访问服务器了的密码"); System.out.println("====================================================="); pw.print("浏览器不是第一次访问服务器的文件内容"+info); pw.print("浏览器不是第一次访问服务器的名字"+info1); pw.print("浏览器不是第一次访问服务器了的密码
@过滤器总结:从上面图片过程可以看出:当Servlect与Filter同时创建时,运行服务器首先调用的是Filter中。MyFilter():构造方法<第二部分观察Servlect与Filter在web.xml注册的相似 第一:什么是监听器。概念:监听器就是一个实现了特定接口的Java类。 分类:
第一类监听器:监听域对象的创建、销毁 第二类监听器:监听域对象中的属性变更(属性设置、属性替换、属性移除) 第三类监听器:监听域对象中的java对象的绑定。
Servlet监听器:事件源:request、session、servletContext三大域对象 监听器:Servlet对象(三类监听器) 绑定:web.xml配置 或 @WebListener注解 事件:域对象发生改变
Listener 工作原理实现了特定接口的类为监听器,用来监听另一个Java类的方法调用或者属性改变;** 当被监听的对象发生了方法调用或者属性改变后,监听器的对应方法就会立即执行。
第一种方式@第二类类接口:1 ServletContextListener 监听ServletContext域对象的创建、销毁 // 服务器启动,ServletContext域对象创建,该监听器方法则执行 public void contextInitialized(ServletContextEvent servletContextEvent) // 服务器关闭,ServletContext域对象销毁,该监听器方法则执行 public void contextDestroyed(ServletContextEvent servletContextEvent) 2 HttpSessionListener 监听HttpSession域对象的创建、销毁 // 服务器第一次调用getSession方法时,该监听器方法被执行 public void sessionCreated(HttpSessionEvent httpSessionEvent) // session过期/调用了invalidate方法销毁session时,该监听器方法被执行 public void sessionDestroyed(HttpSessionEvent httpSessionEvent) 3 ServletRequestListener 监听ServletRequest域对象的创建、销毁 // 客户端向服务器发送了一个请求,服务器就会为该请求创建一个request对象,该监听器方法就被执行 public void requestInitialized(ServletRequestEvent servletRequestEvent) // 当服务器为这次请求做出了响应后,将request对象销毁,该监听器方法就被执行 public void requestDestroyed(ServletRequestEvent servletRequestEvent) 第二种方式@第二类接口ServletContextAttributeListener HttpSessionAttributeListener ServletRequestAttributeListener // 监听ServletContext域对象中属性的【添加】 public void attributeAdded(ServletContextAttributeEvent var1); // 监听ServletContext域对象中属性的【替换】 public void attributeReplaced(ServletContextAttributeEvent var1); // 监听ServletContext域对象中属性的【移除】 public void attributeRemoved(ServletContextAttributeEvent var1); 第三种方式@第三类接口HttpSessionBindingListener 监听session域对象中的java对象的状态(绑定和解绑) 绑定:将java对象存储到session域对象 解绑:将java对象从session域对象移除
该监听器不需要在web.xml中配置
public class User implements HttpSessionBindingListener { private int id; private String username; private String password; @Override public void valueBound(HttpSessionBindingEvent httpSessionBindingEvent) { System.out.println("HttpSession 与 User 绑定"); } @Override public void valueUnbound(HttpSessionBindingEvent httpSessionBindingEvent) { System.out.println("HttpSession 与 User 解绑"); } }第二点 Listener 开发步骤① 定义类实现监听器接口 ② 重写方法 ③ 配置 web.xml Listener的全限定名 本案例的实现采用了过滤器,监听器,Servlect.运行界面
代码
package com.Listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class MyServlectListener implements ServletContextListener { public MyServlectListener() { // TODO Auto-generated constructor stub System.out.println("MyServlectListener的构造方法"); } @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("Listener程序初始化程序开启"); System.out.println("WEB ServletContextListener程序初始化程序开启ServletContext" + "Listener开始@@程序开始运行@@"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("Listener程序初始化程序结束"); System.out.println("WEB contextDestroyed==程序初始化程序in the endontextDestroyed程序结束" + "程序在结束中"); } }
package com.Listener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class MySessionListener implements HttpSessionListener { private static int count=0; public MySessionListener() { // TODO Auto-generated constructor stub System.out.println("MySessionListener() 构造方法"); } @Override public void sessionCreated(HttpSessionEvent se) { // TODO Auto-generated method stub System.out.println("当sesssion创建时执行程序 sessionCreated()构造方法"); se.getSession().getServletContext().setAttribute("count", ++count); } @Override public void sessionDestroyed(HttpSessionEvent se) { System.out.println("当sesssion销毁时执行程序 sessionDestroyed()构造方法"); } }package com.Listener; import javax.servlet.ServletContext; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; public class User implements HttpSessionBindingListener{ @Override public void valueBound(HttpSessionBindingEvent event) { System.out.println("HttpSession 与 User 绑定"); // 有用户登陆成功 // 判断是否是第一个登陆的人 ServletContext servletContext = event.getSession().getServletContext(); Integer count = (Integer) servletContext.getAttribute("count"); // 第一个登陆为1,非第一个则++ count = null == count ? 1 : (count += 1); servletContext.setAttribute("count", count); } @Override public void valueUnbound(HttpSessionBindingEvent event) { System.out.println("HttpSession 与 User 解绑"); // 有用户注销登陆 ServletContext servletContext = event.getSession().getServletContext(); Integer count = (Integer) servletContext.getAttribute("count"); // count--是因为在同浏览器下重复登陆时session.setAttribute(name, value) // 每次会覆盖value值进而触发监听器valueBound()的count++ servletContext.setAttribute("count", count); } //定义三个数据类型 private int id; private String username; private String password; public User() { } public User(int id, String username, String password) { this.id = id; this.username = username; this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + ''' + ", password='" + password + ''' + '}'; } }
package com.Servlect; 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; import com.Listener.User; @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; public LoginServlet() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); if ("root".equals(username) && "1234".equals(password)) { // 登陆成功,修f改登陆状态,转发到showIndex页 User user = new User(); user.setUsername(username); user.setPassword(password); request.getSession().setAttribute("existUser", user); // 转发会导致每次刷新页面都会重新登陆,然后被统计新新用户中 //request.getRequestDispatcher("/showIndex").forward(request, response); response.sendRedirect("ServlectA"); } else { // 登陆失败,转发到login.html request.getRequestDispatcher("ServlectB").forward(request, response); } } }package com.Servlect; 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; @WebServlet("/LogoutServlet") public class LogoutServlet extends HttpServlet { private static final long serialVersionUID = 1L; public LogoutServlet() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 注销登陆:将existUser从session域中移除 或 销毁session //request.getSession().removeAttribute("existUser"); request.getSession().invalidate(); // 注销成功 request.getRequestDispatcher("/ShowIndexServlect").forward(request, response); } }
package com.Servlect; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; //你要阻止Servlect2 @WebFilter("/Servlect2") public class MyFilter1 implements Filter { public MyFilter1() { // TODO Auto-generated constructor stub System.out.println("构造方法"); } public void destroy() { // TODO Auto-generated method stub System.out.println("销毁数据"); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); System.out.println("我是myfilter1开始拦截请求地址哦哦"); //实施访问 chain.doFilter(request, response); } public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub System.out.println("加载数据"); } }
package com.Servlect; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; @WebFilter("/Servlect2") public class MyFilter2 implements Filter { public MyFilter2() { // TODO Auto-generated constructor stub } public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub // place your code here response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); System.out.println("我是myfilter2开始拦截请求地址哦哦"); //实施访问 chain.doFilter(request, response); } public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } }package com.Servlect; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; @WebFilter("/Servlect2") public class MyFilter3 implements Filter { public MyFilter3() { // TODO Auto-generated constructor stub } public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub // place your code here response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); System.out.println("我是myfilter3开始拦截请求地址哦哦"); //实施访问 chain.doFilter(request, response); } public void init(FilterConfig fConfig) throws ServletException { // TODO Auto-generated method stub } }
package com.Servlect; 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; @WebServlet("/Servlect1") public class Servlect1 extends HttpServlet { private static final long serialVersionUID = 1L; public Servlect1() { super(); // TODO Auto-generated constructor stub System.out.println("Servlext构造方法"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); PrintWriter pw =response.getWriter(); pw.print("我是第一个servlect"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
package com.Servlect; 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; @WebServlet("/Servlect2") public class Servlect2 extends HttpServlet { private static final long serialVersionUID = 1L; public Servlect2() { super(); System.out.println("这是Servlect2的抽象方法"); // TODO Auto-generated constructor stub } @Override public void init() throws ServletException { // TODO Auto-generated method stub System.out.println("Servlect2加载数据"); super.init(); } @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("这里开始运行Servlect的方法"); super.service(req, resp); } @Override protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub super.doDelete(req, resp); System.out.println("Servlect2的删除方法"); } @Override public void destroy() { // TODO Auto-generated method stub super.destroy(); System.out.println("Servlect2销毁数据"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter pw =response.getWriter(); pw.print("我是第一个servlect2"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }package com.Servlect; 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; @WebServlet("/Servlect4") public class Servlect4 extends HttpServlet { private static final long serialVersionUID = 1L; public Servlect4() { super(); System.out.println("这是Servlect4的抽象方法"); // TODO Auto-generated constructor stub } @Override public void init() throws ServletException { // TODO Auto-generated method stub System.out.println("Servlect4加载数据"); super.init(); } @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("这里开始运行Servlect4的方法"); super.service(req, resp); } @Override protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub super.doDelete(req, resp); System.out.println("Servlect4的删除方法"); } @Override public void destroy() { // TODO Auto-generated method stub super.destroy(); System.out.println("Servlect4销毁数据"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter pw =response.getWriter(); pw.print("我是第一个servlect4"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }package com.Servlect; 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; @WebServlet("/Servlect3") public class Servlect3 extends HttpServlet { private static final long serialVersionUID = 1L; public Servlect3() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter pw =response.getWriter(); pw.print("我是第一个servlect3"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } package com.Servlect; 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; @WebServlet("/ServlectA") public class ServlectA extends HttpServlet { private static final long serialVersionUID = 1L; public ServlectA() { System.out.println("ServlectA的创建过程"); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter pw =response.getWriter(); pw.append("---A-------session---------Servlect-----"); pw.append("----------欢迎光临--------------"); request.getSession(); Integer count =(Integer)request.getServletContext().getAttribute("count"); pw.append(""); pw.append("-----欢迎光临----你是第《"+count+"》----为客人----"); pw.append(""); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } package com.Servlect; 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; @WebServlet("/ServlectB") public class ServlectB extends HttpServlet { private static final long serialVersionUID = 1L; public ServlectB() { super(); System.out.println("ServlectB的创建过程"); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter pw =response.getWriter(); pw.append("---ServlectB-------session---------Servlect-----"); pw.append("----------欢迎下次光临--------------"); pw.append(""); request.getSession(); Integer count1 =(Integer)request.getServletContext().getAttribute("coun"); pw.append(""); pw.append("-----欢迎光临----你是第《"+count1+"》----退出的客人----"); pw.append("----------欢迎下次光临--------------"); pw.append(""); request.getSession().invalidate(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } package com.Servlect; 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; import org.apache.catalina.User; @WebServlet("/ShowIndexServlet") public class ShowIndexServlet extends HttpServlet { private static final long serialVersionUID = 1L; public ShowIndexServlet() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); response.getWriter().write(response.toString()); User existUser = (User) request.getSession().getAttribute("existUser"); StringBuffer responseBody = new StringBuffer(); if (null == existUser) { // 不在登陆状态 responseBody.append("您还没有登陆,请登录"); } else { // 在登陆状态 responseBody.append("欢迎回来,").append(existUser.getUsername()).append(" 注销"); } // 获取在线人数 ServletContext servletContext = getServletContext(); Integer count = (Integer) servletContext.getAttribute("count"); System.out.println("在线人数:" + count); if (null == count) { // 没有人登陆时,0人 count = 0; } responseBody.append("
在线人数为:").append(count).append("人"); } } package com.Servlect; 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; @WebServlet("/ZhuJie") public class ZhuJie extends HttpServlet { private static final long serialVersionUID = 1L; public ZhuJie() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter pw =response.getWriter(); pw.append(""); pw.append(""); pw.append(""); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }欢迎分享,转载请注明来源:内存溢出
评论列表(0条)