当前位置:网站首页>请求转发与重定向
请求转发与重定向
2022-07-06 05:48:00 【緈福的街口】
在网页中有登录校验与显示首页,如何进行跳转和交互?
两个Servlet(JSP)之间跳转有两种方式:
- 请求转发
- 响应重定向
一、请求转发
是服务器跳转,只会产生一次请求
// 实现了请求转发的功能
request.getRequestDispatcher("/direct/index").forward(request,response);
请求转发:地址为direct/check
控制台输出
二、响应重定向
重定向是浏览器端跳转,会产生两次请求
// 响应重定向需要增加contextPath
response.sendRedirect("/servlet_advanced/direct/index");
响应重定向:地址输入为direct/check
重定向后地址变为direct/index
控制台输出
三、设置请求自定义属性
请求允许创建自定义属性
设置请求属性
request.setAttribute("username", "admin");
获取请求属性
String name = (String)request.getAttribute("username");
1、请求转发
只请求一次,可以获取admin属性
2、响应重定向
响应重定向,请求两次,第一次已销毁,获取属性值为null
四、程序页面
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("用户登录成功");
request.setAttribute("username", "admin");
// 实现了请求转发的功能
request.getRequestDispatcher("/direct/index").forward(request,response);
// 响应重定向需要增加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 + ".");
}
}
边栏推荐
- Selective parameters in MATLAB functions
- Memory and stack related concepts
- Winter 2021 pat class B problem solution (C language)
- [Jiudu OJ 07] folding basket
- Network protocol model
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- B站刘二大人-线性回归及梯度下降
- [JVM] [Chapter 17] [garbage collector]
- 类和对象(一)this指针详解
- J'ai un chaton.
猜你喜欢
A master in the field of software architecture -- Reading Notes of the beauty of Architecture
B站刘二大人-反向传播
实践分享:如何安全快速地从 Centos迁移到openEuler
Station B Liu Erden linear regression pytoch
Download, install and use NVM of node, and related use of node and NRM
【经验】win11上安装visio
Report on the competition status and investment decision recommendations of Guangxi hospital industry in China from 2022 to 2028
- [email protected]树莓派"/>
[email protected]树莓派
Text classification still stays at Bert? The dual contrast learning framework is too strong
P2802 回家
随机推荐
嵌入式面试题(四、常见算法)
[imgui] unity MenuItem shortcut key
LeetCode_ String inversion_ Simple_ 557. Reverse word III in string
Redis消息队列
如何在业务代码中使用 ThinkPHP5.1 封装的容器内反射方法
Download, install and use NVM of node, and related use of node and NRM
Demander le Code de texte standard correspondant à un centre de travail dans l'ordre de production
[JVM] [Chapter 17] [garbage collector]
26file filter anonymous inner class and lambda optimization
LAN communication process in the same network segment
[experience] when ultralso makes a startup disk, there is an error: the disk / image capacity is too small
H3C firewall rbm+vrrp networking configuration
Report on market depth analysis and future trend prediction of China's arsenic trioxide industry from 2022 to 2028
【论文代码】SML部分代码阅读
Pytorch代码注意的细节,容易敲错的地方
Some easy-to-use tools make your essay style more elegant
How to use PHP string query function
Classes and objects (I) detailed explanation of this pointer
大型网站如何选择比较好的云主机服务商?
PDK工藝庫安裝-CSMC