当前位置:网站首页>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 + ".");
}
}
边栏推荐
- Winter 2021 pat class B problem solution (C language)
- B站刘二大人-多元逻辑回归 Lecture 7
- 公司视频加速播放
- [Baiwen smart home] first day of the course_ Learn Embedded and understand the development mode of bare metal and RTOS
- Clear floating mode
- Wib3.0 leapfrogging, in leapfrogging (ง • ̀_•́) ง
- Memory and stack related concepts
- (5) Explanation of yolo-v3 core source code (3)
- [course notes] Compilation Principle
- [string] palindrome string of codeup
猜你喜欢

Analysis of grammar elements in turtle Library

Memory and stack related concepts

LAN communication process in the same network segment

The difference and usage between continue and break

请求转发与重定向
[SQL Server Express Way] - authentification et création et gestion de comptes utilisateurs

What is independent IP and how about independent IP host?

如何在业务代码中使用 ThinkPHP5.1 封装的容器内反射方法

continue和break的区别与用法

Clear floating mode
随机推荐
Hongliao Technology: Liu qiangdong's "heavy hand"
Yygh-11-timing statistics
实践分享:如何安全快速地从 Centos迁移到openEuler
Jushan database appears again in the gold fair to jointly build a new era of digital economy
B站刘二大人-数据集及数据加载 Lecture 8
How to use the container reflection method encapsulated by thinkphp5.1 in business code
请求转发与重定向
Node 之 nvm 下载、安装、使用,以及node 、nrm 的相关使用
My 2021
wib3.0 跨越,在跨越(ง •̀_•́)ง
c语言——冒泡排序
B站刘二大人-Softmx分类器及MNIST实现-Lecture 9
ArcGIS应用基础4 专题图的制作
A complete collection of necessary learning websites for office programmers
Redis消息队列
B站刘二大人-线性回归及梯度下降
Construction of yolox based on paste framework
[C language syntax] the difference between typedef struct and struct
ContentType的作用
IDEA 新UI使用






