2022年课时七——JSP第六次课 response对象的作用

简介: 2022年课时七——JSP第六次课 response对象的作用

response的基本知识

HttpServlet

Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象、和代表响应的response对象


request和response对象即代表了请求和响应,那我们要获取客户机提交过来的数据,只需要找request对象就可以了,要想客户机输出数据,只需要找response即可。


response只是一个对象名称,HttpServletResponse类


Student  xiaoming=new Student();   类名: Student       对象名:xiaoming


重定向:重新制定访问的方向

作用:用于从一个Servlet去开启另一个Servlet


重定向:

重定向属于浏览器跳转,不是服务器自己跳转    超链接是一样的

重定向跳转页面浏览器地址会发生变化

重定向为两次请求

an重定向可以访问不同服务器的页面跳转

package com.Servlet;
import java.awt.image.RescaleOp;
import java.io.IOException;
import java.io.OutputStream;
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 Servlet1
 */
@WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {
  private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Servlet1() {
        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="<h1>你好,倒计时servlet<span style='color:red'> java jsp</span></h1>";
    //使用response获得输出
    OutputStream  os =response.getOutputStream();
    os.write(info.getBytes());
    os.write(info1.getBytes());
  }
  /*
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   * 
   */
  }
package com.Servlet;
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 Servlet2
 */
@WebServlet("/Servlet2")
public class Servlet2 extends HttpServlet {
  private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Servlet2() {
        super();
        // 
    }
  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    //设置servlet发送的数据支撑HTML格式
    response.setContentType("text/html");
    //设置发送servlet 浏览html格式 utf-8
    response.setCharacterEncoding("UTF-8");
    String info="abc";
    String info1="20软件三班";
    String info2="你好,<div style='color:red;background-Color:green'> <h1 style='color:red;background-Color:blue'>"
        + "java jsp</h1>"
        +"<ul><li>1111</li><br><li>2111</li><br><li>3111</li><br></ul>"
        +"<ol><li>1111</li><br><li>2111</li><br><li>3111</li><br></ol>"
        +"<hr>"
        +"<table style='color:red;background-Color:yellow'>课表"
        + "<th>4</th><tr><td>1</td><td>2</td><td>3<td/><tr/>"
        +"<th>2</th>4<tr><td>5</td><td>6</td><td>7<td/><tr/>"
        + "</table>"
        +"<hr>"
        +"<hr>"
        +"<h1>hellow world see you </h1>"
        + "</div>";
    //将数据写出的
    PrintWriter pw=response.getWriter();
    pw.print(info);
    pw.print(info1);
    pw.print(info2);
  }
  /**
   * @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;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
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 Servlet3
 */
@WebServlet("/Servlet3")
public class Servlet3 extends HttpServlet {
  private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
      * 图片的下载
     */
    public Servlet3() {
        super();
        // TODO Auto-generated constructor stub
    }
  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 使用对象完成图片的下载
    //设置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.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);
  }
}
package com.Servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.Random;
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("/Servlet6")
public class Servlet4 extends HttpServlet {
  private static final long serialVersionUID = 1L;
  /**
   * @see HttpServlet#HttpServlet()
   */
  public Servlet4() {
    super();
    System.out.println("Servlet4正在运行中");
    // 创建码
  }
  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 生成数字,随机数
    //100000 -1000000
    //[0,1]*900000=[0,900000]+100000
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/html");
    response.setHeader("refresh", "5");
    //设置要求浏览器 
    /* response.sendRedirect("Servlet1"); */
    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万元,需要的码号为:<h1 style='color:red ;background-color:green'>"+i+"</h1>,打死也不说  \"<hr>\";");
    pw.print("你的QQ到账1000万元,需要的码号为:<h2 style='color:red ;background-color:green'>"+c+"</h2>,打死也不说   \"<hr>\";");
    pw.print("你的微信到账1000万元,需要的码号为:<span style='color:green'>"+a+"</span>,打死也不说"); 
    pw.print("你的手机支付宝到账1000万<span style='color :red'>"+i+"</span>");
  }
    /*response.sendRedirect("Servlet1");
     try { Thread.sleep(5000);
     Connection conn =  Time.time();
      } catch (Exception e) 
      { 
      e.printStackTrace();
       response.sendRedirect("Servlet1"); 
      } 
  }
*/
  /**
   * @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;
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 Servlet5
 */
@WebServlet("/Servlet5")
public class Servlet5 extends HttpServlet {
  private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Servlet5() {
        super();
        // TODO Auto-generated constructor stub
    }
  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  //当浏览器访问servlet5时刻,servlet5回复消息访问servlet1
    System.out.println("servlet5回复消息访问servlet1");
    response.sendRedirect("Servlet1");
  }
    /*
   * // 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);
  }
}
package com.Servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.Random;
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("/Servlet6")
public class Servlet6 extends HttpServlet {
  private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Servlet6() {
        super();
        System.out.println("Servlet6正在运行中");
        // 创建码
    }
  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // 生成数字,随机数
    //100000 -1000000
    //[0,1]*900000=[0,900000]+100000
    response.setCharacterEncoding("UTF-8");
    response.setContentType("text/html");
    response.setHeader("refresh", "5");
    //设置要求浏览器 
    /* response.sendRedirect("Servlet1"); */
    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万元,需要的码号为:<h1 style='color:red ;background-color:green'>"+i+"</h1>,打死也不说  \"<hr>\";");
    pw.print("你的QQ到账1000万元,需要的码号为:<h2 style='color:red ;background-color:green'>"+c+"</h2>,打死也不说   \"<hr>\";");
    pw.print("你的微信到账1000万元,需要的码号为:<span style='color:green'>"+a+"</span>,打死也不说"); 
    pw.print("你的手机支付宝到账1000万<span style='color :red'>"+i+"</span>");
    response.sendRedirect("Servlet1");
     try { Thread.sleep(5000);
     Connection conn =  Time.time();
      } catch (Exception e) 
      { 
      e.printStackTrace();
      /* response.sendRedirect("Servlet1"); */
      } 
  }
  /**
   * @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;
import java.sql.Connection;
public class Time {
  public static int time=60*60*60;
  static Connection time() {
    while (time>0) {
      time--;
      try {
        Thread.sleep(1000);
        int hh=time/1/1%1;
        int mm=time/60%1;
        int ss=time%6;
        if(hh==0 &&mm==0 && ss==0) {
          break;
        }
        System.out.println("小时"+hh+"Сʱ"+mm+"分钟"+ss+"秒");
      } catch (InterruptedException e) {
        // TODO: handle exception
    }finally {
      System.out.println("正在跳转页面哦 Servlect1");
    }
    }
    return null;
  }
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    time();
  }
}

运行结果

总结:response对象的作用

图片,音频,文件的下载

对浏览器的访问

随机码的获取

servlet的跳转

HttpServlet

HttpRequest的有关方法,获取HTTP请求信息;

HttpRequest的有关方法,获取HTTP请求信息;

// 生成数字,随机数

//100000 -1000000

//[0,1]*900000=[0,900000]+100000

response.setCharacterEncoding("UTF-8");

response.setContentType("text/html");

//设置要求浏览器

response.setHeader("refresh", "10");

//response 重定向 重新指定访问方向

从一个servlet 去 开启 新的servlet

重定向:属于浏览器的跳转.超链接

<a href=""></a>

浏览地址发生改变

重定向为二次请求。

重定向可以访问不同服务器的跳转。

相关文章
|
5月前
|
Java
JSP中使用response对象实现定时跳转网页
这篇文章讨论了JSP页面中使用response对象实现定时跳转网页的五种跳转方法,包括RequestDispatcher.forward()的使用及其在服务器端的工作原理。
|
5月前
|
缓存 Java 应用服务中间件
JSP的内置对象 request和response
这篇文章详细介绍了JSP的内置对象,包括request、response、out、session和application对象的使用方法和特性,以及如何通过这些对象处理HTTP请求和响应、管理会话和输出数据。
|
8月前
|
缓存 Java 开发者
JSP 教程 之 JSP 隐式对象 10
JSP隐式对象是预定义的Java对象,如request、response、out、session、application等,直接供开发者使用。out对象是JspWriter实例,用于向网页输出内容,类似PrintWriter但有缓存处理和抛出IOException。exception对象保存了页面的异常信息,用于错误处理。
46 3
|
8月前
|
Java 开发者 容器
JSP 教程 之 JSP 隐式对象 3
JSP教程讲解了九个隐式对象,如request、response、out、session、application、config、pageContext、page和Exception。response对象是HttpServletResponse的实例,用于向客户端发送响应,包括设置HTTP头信息、添加cookie和状态码。
42 3
|
8月前
|
Java 开发者 容器
JSP 教程 之 JSP 隐式对象 2
JSP隐式对象是预定义的Java对象,无需声明即可直接使用。包括9大对象:request(HttpServletRequest,处理客户端请求)、response(HttpServletResponse,响应输出)、out(JspWriter,输出到网页)、session(HttpSession,用户会话管理)、application(ServletContext,应用上下文)、config(ServletConfig,配置信息)、pageContext(PageContext,全局访问)、page(当前页面对象)和Exception(异常对象)。
54 3
|
8月前
|
缓存 Java 开发者
JSP 教程 之 JSP 隐式对象 5
JSP教程介绍了九个隐式对象,如request、response、out、session、application等,无需声明即可直接使用。out对象是JspWriter实例,用于向网页输出内容,可配置是否缓存。它扩展了PrintWriter功能,处理缓存并可能抛出IOException。session对象基于HttpSession,用于跨客户端请求的会话跟踪。
33 1
|
8月前
|
缓存 Java 开发者
JSP 教程 之 JSP 隐式对象 9
JSP隐式对象是预定义的Java对象,无需声明即可直接使用。包括9大对象:request(HttpServletRequest)、response(HttpServletResponse)、out(JspWriter,用于输出到网页)、session(HttpSession)、application(ServletContext)、config(ServletConfig)、pageContext(PageContext,访问所有对象和命名空间)、page(页面实例,等同于Java的this)和Exception(异常对象)。
47 0
|
8月前
|
缓存 Java 对象存储
JSP 教程 之 JSP 隐式对象 8
JSP隐式对象是预定义的Java对象,如request、response、out等,无需声明即可直接使用。out对象是JspWriter实例,用于向response输出内容,支持print和println方法,可抛出IOException。pageContext对象代表整个JSP页面,提供访问页面信息和不同scope的对象,如request、response、session等,并有removeAttribute()方法管理页面属性。
41 0
|
8月前
|
缓存 Java 开发者
JSP 教程 之 JSP 隐式对象 7
JSP隐式对象是预定义的Java对象,无需声明即可使用。包括9大对象:request、response、out、session、application、config、pageContext、page和Exception。其中,out对象用于输出内容到网页,基于JspWriter,支持print和println方法。config对象是ServletConfig的实例,提供访问Servlet初始化参数的途径,如获取servlet名称。
39 0
|
8月前
|
缓存 Java 开发者
JSP 教程 之 JSP 隐式对象 6
JSP隐式对象是预定义的Java对象,无需声明即可直接使用。包括:request(HttpServletRequest)、response(HttpServletResponse)、out(JspWriter,用于输出到网页)、session(HttpSession)、application(ServletContext)、config(ServletConfig)、pageContext(PageContext)、page(当前页面对象)和Exception。out对象基于JspWriter,用于内容输出,提供print和println方法。
36 0