当前位置:网站首页>请求转发与重定向
请求转发与重定向
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 + ".");
}
}
边栏推荐
- 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
- What preparations should be made for website server migration?
- Embedded interview questions (IV. common algorithms)
- Closure, decorator
- 应用安全系列之三十七:日志注入
- Web Security (VI) the use of session and the difference between session and cookie
- HCIA复习
- B站刘二大人-数据集及数据加载 Lecture 8
- ArcGIS application foundation 4 thematic map making
- 华为路由器如何配置静态路由
猜你喜欢
授予渔,从0开始搭建一个自己想要的网页
初识数据库
Analysis report on development trends and investment planning of China's methanol industry from 2022 to 2028
What impact will frequent job hopping have on your career?
Yygh-11-timing statistics
Li Chuang EDA learning notes 12: common PCB board layout constraint principles
B站刘二大人-反向传播
C language learning notes (mind map)
巨杉数据库再次亮相金交会,共建数字经济新时代
Classes and objects (I) detailed explanation of this pointer
随机推荐
Yunxiaoduo software internal test distribution test platform description document
Redistemplate common collection instructions opsforvalue (II)
What impact will frequent job hopping have on your career?
如何在业务代码中使用 ThinkPHP5.1 封装的容器内反射方法
[SQL Server fast track] - authentication and establishment and management of user accounts
Cannot build artifact 'test Web: War expanded' because it is included into a circular depend solution
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
Selective parameters in MATLAB functions
Yygh-11-timing statistics
B站刘二大人-数据集及数据加载 Lecture 8
Report on market depth analysis and future trend prediction of China's arsenic trioxide industry from 2022 to 2028
Report on the competition status and investment decision recommendations of Guangxi hospital industry in China from 2022 to 2028
局域网同一个网段通信过程
Redis消息队列
Station B, Master Liu Er - back propagation
[experience] install Visio on win11
HCIA复习
华为路由器如何配置静态路由
Migrate Infones to stm32
What is independent IP and how about independent IP host?