HttpServletResponse概述

response的運行流程

 

 

response:javascript

          1. 屬於重定向請求;html

          2.  其地址欄的URL會改變;java

           3.會向服務器發送兩次請求;apache

用response,A沒有錢,請求失敗,可是A告訴B,C有錢。因而B再次向C借錢,C借給B,請求成功。瀏覽器

response是響應對象。服務器

經過response設置響應行

設置響應行的狀態碼oracle

setStatus(int sc)ui

package com.oracle.demo01;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HeadServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//設置響應行中的狀態碼(只能改變響應行中的狀態碼,版本號與狀態信息沒法改變,狀態信息隨着狀態的改變而改變)
		response.setStatus(404);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

 

經過response設置響應頭

有衆多方法,可是經常使用的就是setHeader(String name,String value)編碼

重定向須要:1.狀態碼:302url

      2.響應頭:location 表明重定向地址

使用該方法設置的響應頭最終會發送給客戶端瀏覽器

package com.oracle.demo01;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.org.apache.regexp.internal.RESyntaxException;

public class Servlet01 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*		//設置響應行狀態碼
		response.setStatus(302);
	      //設置響應頭中的Location
		response.setHeader("Location","/WEB05/Servlet02");*/
		//重定向(與上面的代碼是一致的)簡潔版
		response.sendRedirect("/WEB05/Servlet02");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

設置定時刷新響應頭的信息

 

package com.oracle.demo01;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RefreshServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//設置定時刷新的頭
		response.setHeader("Refresh","5;url=https://www.baidu.com");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
 window.onload=function(){
	 //獲取span元素
	 var second=document.getElementById("second");
	 //定義秒數
	 var time=5;
	 //設置定時器
	 var timer=setInterval(function(){
		 second.innerHTML=time;
		 time--;
		 if(time<0){
			 clearInterval(timer);
			 location.href="https://www.baidu.com";
		 }
	 },1000);
 }
</script>
</head>
<body>
  	 恭喜你,註冊成功!
  	 <span id="second" style="color:red">5</span>
  	 秒後跳轉,如沒跳轉,請點擊
  	 <a href="https://www.baidu.com">這裏</a>
</body>
</html>

  

 

經過response設置響應體

解決中文亂碼問題

須要設置兩個地方的中文亂碼問題,一個是緩衝區的時候設置,一個是客戶端解碼的時候進行設置。

package com.oracle.demo01;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BodyServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		/*//設置緩衝區的編碼表
		response.setCharacterEncoding("UTF-8");
		//設置客戶端瀏覽器
		response.setHeader("Content-Type","text/html;charset=utf-8");*/
		//解決響應的中文亂碼問題(與上面的代碼是同樣的,簡潔版)
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("中國");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
相關文章
相關標籤/搜索