1.使用response對象提供的sendRedirect()方法可以將網頁重定向到另一個頁面。重定向操作支持將地址重定向到不同的主機上,這一點與轉發是不同的。在客戶端瀏覽器上將會得到跳轉地址,并重新發送請求鏈接。用戶可以從瀏覽器的地址欄中看到跳轉后的地址。進行重定向操作后,request中的屬性全部失效,并且開始一個新的request對象。
sendRedirect()方法的語法格式如下:
response.sendRedirect(String psth);
例子:新建一個index.jsp頁面在body中添加Java重定向語句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<%@ page language="java" contentType="text/html;charset="UTF-8" pageEncoding="UTF-8"%> < html > < head > < meta http-equiv = "Content-Type" content = "text/html;charset=UTF-8" > < title >首頁</ title > </ head > < body > <%response.sendRedirect("login.jsp");%> </ body > </ html > |
新建一個login.jsp 布局登錄界面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> < html > < head > < meta http-equiv = "Content-Type" content = "text/html;charset=UTF-8" > < title >用戶登錄頁面</ title > </ head > < body > < form name = "form1" method = "post" action = "" > 用戶名:< input name = "name" type = "text" id = "name" style = "width:120px" >< br > 密 碼:< input name = "pwd" type = "password" id = "pwd" style = "width:120px" >< br > < input type = "submit" name = "Submit" value = "提交" > </ form > </ body > </ html > |
以上這篇response對象的使用(實例講解)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/lihuibin/archive/2017/08/31/7460153.html