当前位置:网站首页>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 + ".");
}
}
边栏推荐
- A master in the field of software architecture -- Reading Notes of the beauty of Architecture
- B站刘二大人-多元逻辑回归 Lecture 7
- 如何在业务代码中使用 ThinkPHP5.1 封装的容器内反射方法
- [Baiwen smart home] first day of the course_ Learn Embedded and understand the development mode of bare metal and RTOS
- 公司視頻加速播放
- How to recover Huawei router's forgotten password
- 什么是独立IP,独立IP主机怎么样?
- 关于 PHP 启动 MongoDb 找不到指定模块问题
- 【SQL server速成之路】——身份验证及建立和管理用户账户
- 【无标题】
猜你喜欢
局域网同一个网段通信过程
【论文阅读】NFlowJS:基于鲁棒学习的合成负数据密集异常检测
(column 22) typical column questions of C language: delete the specified letters in the string.
Rustdesk builds its own remote desktop relay server
【SQL server速成之路】——身份验证及建立和管理用户账户
[experience] install Visio on win11
Arrays and collections
Is it difficult for an information system project manager?
数字经济破浪而来 ,LTD是权益独立的Web3.0网站?
The difference and usage between continue and break
随机推荐
C language bubble sort
H3C S5820V2_5830V2交换机IRF2堆叠后升级方法
Station B, Mr. Liu Er - multiple logistic regression, structure 7
ContentType的作用
Embedded interview questions (I: process and thread)
CoDeSys note 2: set coil and reset coil
LAN communication process in the same network segment
类和对象(一)this指针详解
How Huawei routers configure static routes
[C language syntax] the difference between typedef struct and struct
Construction of yolox based on paste framework
Node 之 nvm 下载、安装、使用,以及node 、nrm 的相关使用
B站刘二大人-反向传播
养了只小猫咪
nodejs实现微博第三方登录
【SQL server速成之路】——身份驗證及建立和管理用戶賬戶
c语言——冒泡排序
【论文阅读】NFlowJS:基于鲁棒学习的合成负数据密集异常检测
Query the standard text code corresponding to a work center (s) in the production order
Configuring OSPF GR features for Huawei devices