IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> JSP第六次课 response对象的作用 -> 正文阅读

[系统运维]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>
浏览地址发生改变
重定向为二次请求。
重定向可以访问不同服务器的跳转。

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-10-15 12:08:50  更:2021-10-15 12:09:23 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 19:51:19-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码