当前位置:网站首页>请求转发与重定向
请求转发与重定向
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 + ".");
}
}
边栏推荐
- 嵌入式面试题(四、常见算法)
- PDK process library installation -csmc
- Hongliao Technology: Liu qiangdong's "heavy hand"
- Raised a kitten
- Redis6 cluster setup
- 【课程笔记】编译原理
- 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
- 入侵检测领域数据集总结
- 【无标题】
- 通讯录管理系统链表实现
猜你喜欢

网站进行服务器迁移前应做好哪些准备?

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

华为路由器如何配置静态路由

The usage and difference between strlen and sizeof
[SQL Server fast track] - authentication and establishment and management of user accounts

Wib3.0 leapfrogging, in leapfrogging (ง • ̀_•́) ง

LTE CSFB process

RustDesk 搭建一个自己的远程桌面中继服务器

数字经济破浪而来 ,LTD是权益独立的Web3.0网站?

H3C防火墙RBM+VRRP 组网配置
随机推荐
华为路由器如何配置静态路由
[Jiudu OJ 07] folding basket
网络协议模型
Raised a kitten
Is it difficult for an information system project manager?
P2802 go home
wib3.0 跨越,在跨越(ง •̀_•́)ง
[email protected]树莓派
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
Redistemplate common collection instructions opsforvalue (II)
[happy Spring Festival] if you feel happy, dance
AUTOSAR from getting started to becoming proficient (10) - embedded S19 file analysis
PDK工藝庫安裝-CSMC
59. Spiral matrix
Database: ODBC remote access SQL Server2008 in oracel
查詢生產訂單中某個(些)工作中心對應的標准文本碼
B站刘二大人-线性回归及梯度下降
Analysis of grammar elements in turtle Library
如何在业务代码中使用 ThinkPHP5.1 封装的容器内反射方法
Leetcode 701 insertion operation in binary search tree -- recursive method and iterative method







