当前位置:网站首页>请求转发与重定向
请求转发与重定向
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 + ".");
}
}
边栏推荐
- [JVM] [Chapter 17] [garbage collector]
- Memory and stack related concepts
- 华为路由器忘记密码怎么恢复
- B站刘二大人-线性回归及梯度下降
- B站刘二大人-反向传播
- What impact will frequent job hopping have on your career?
- Migrate Infones to stm32
- How to use PHP string query function
- MIT6.s081-2020 Lab2 System Calls
- Station B, Master Liu Er - back propagation
猜你喜欢

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

Hongliao Technology: Liu qiangdong's "heavy hand"

养了只小猫咪

Memory and stack related concepts

LAN communication process in the same network segment

Web service connector: Servlet

Station B Liu Erden softmx classifier and MNIST implementation -structure 9
![[Baiwen smart home] first day of the course_ Learn Embedded and understand the development mode of bare metal and RTOS](/img/ed/8d112054f31bd7e593050d1278b9f1.jpg)
[Baiwen smart home] first day of the course_ Learn Embedded and understand the development mode of bare metal and RTOS

授予渔,从0开始搭建一个自己想要的网页

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
随机推荐
网站进行服务器迁移前应做好哪些准备?
[SQL Server fast track] - authentication and establishment and management of user accounts
[experience] install Visio on win11
H3C S5820V2_5830V2交换机IRF2堆叠后升级方法
P2802 go home
[email protected] raspberry pie
LeetCode_ String inversion_ Simple_ 557. Reverse word III in string
After the project is released, index Html is cached
Migrate Infones to stm32
ArcGIS应用基础4 专题图的制作
How to download GB files from Google cloud hard disk
[experience] when ultralso makes a startup disk, there is an error: the disk / image capacity is too small
【经验】win11上安装visio
MIT6.s081-2020 Lab2 System Calls
H3C firewall rbm+vrrp networking configuration
Node 之 nvm 下载、安装、使用,以及node 、nrm 的相关使用
清除浮动的方式
LAN communication process in the same network segment
ArcGIS application foundation 4 thematic map making
[paper reading] nflowjs: synthetic negative data intensive anomaly detection based on robust learning







