当前位置:网站首页>Request forwarding and redirection
Request forwarding and redirection
2022-07-06 05:58:00 【The corner of fufu】
List of articles
There is login verification and display home page in the web page , How to jump and interact ?
Two Servlet(JSP) There are two ways to jump between :
- Request forwarding
- Response redirection
One 、 Request forwarding
It's a server jump , Only one request will be generated
// Realize the function of request forwarding
request.getRequestDispatcher("/direct/index").forward(request,response);
Request forwarding : The address is direct/check
Console output
Two 、 Response redirection
Redirection is browser side jump , There will be two requests
// Response redirection needs to be increased contextPath
response.sendRedirect("/servlet_advanced/direct/index");
Response redirection : The address is entered as direct/check
After resetting, the address changes to direct/index
Console output
3、 ... and 、 Set request custom properties
Request permission to create custom attributes
Set request properties
request.setAttribute("username", "admin");
Get request properties
String name = (String)request.getAttribute("username");
1、 Request forwarding
Request only once , Can get admin attribute
2、 Response redirection
Response redirection , Ask twice , First destroyed , Get the property value as null
Four 、 Program page
1、CheckLoginServlet.java
package com.ssyt.servlet.direct;
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 CheckLoginServlet */
@WebServlet("/direct/check")
public class CheckLoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/** * @see HttpServlet#HttpServlet() */
public CheckLoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(" User login successful ");
request.setAttribute("username", "admin");
// Realize the function of request forwarding
request.getRequestDispatcher("/direct/index").forward(request,response);
// Response redirection needs to be increased contextPath
//response.sendRedirect("/servlet_advanced/direct/index");
}
}
2、IndexServlet.java
package com.ssyt.servlet.direct;
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 IndexServlet */
@WebServlet("/direct/index")
public class IndexServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/** * @see HttpServlet#HttpServlet() */
public IndexServlet() {
super();
// TODO Auto-generated constructor stub
}
/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = (String)request.getAttribute("username");
response.getWriter().println("This is index page!current username is " + name + ".");
}
}
边栏推荐
- Report on the competition status and investment decision recommendations of Guangxi hospital industry in China from 2022 to 2028
- 【LeetCode】Day96-第一个唯一字符&赎金信&字母异位词
- As3013 fire endurance test of cable distribution system
- [email protected]树莓派
- [SQL Server fast track] - authentication and establishment and management of user accounts
- 清除浮动的方式
- The difference and usage between continue and break
- 【SQL server速成之路】——身份驗證及建立和管理用戶賬戶
- B站刘二大人-线性回归 Pytorch
- Mysql database master-slave cluster construction
猜你喜欢
B站刘二大人-Softmx分类器及MNIST实现-Lecture 9
Construction of yolox based on paste framework
ArcGIS application foundation 4 thematic map making
Station B Liu Erden softmx classifier and MNIST implementation -structure 9
(column 22) typical column questions of C language: delete the specified letters in the string.
进程和线程
P2802 回家
清除浮动的方式
IDEA 新UI使用
Station B Liu Erden linear regression pytoch
随机推荐
【论文阅读】NFlowJS:基于鲁棒学习的合成负数据密集异常检测
H3C防火墙RBM+VRRP 组网配置
My 2021
Station B, Master Liu Er - back propagation
Some easy-to-use tools make your essay style more elegant
As3013 fire endurance test of cable distribution system
Sequoiadb Lake warehouse integrated distributed database, June 2022 issue
Node 之 nvm 下载、安装、使用,以及node 、nrm 的相关使用
(column 22) typical column questions of C language: delete the specified letters in the string.
Embedded interview questions (I: process and thread)
嵌入式面试题(四、常见算法)
High quality coding tool clion
IPv6 comprehensive experiment
Garbage collector with serial, throughput priority and response time priority
Introduction to promql of # yyds dry goods inventory # Prometheus
Rustdesk builds its own remote desktop relay server
H3C S5820V2_ Upgrade method after stacking IRF2 of 5830v2 switch
Title 1093: character reverse order
Novice entry SCM must understand those things
[Thesis code] SML part code reading