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