当前位置:网站首页>请求转发与重定向
请求转发与重定向
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 + ".");
}
}
边栏推荐
- Baidu online AI competition - image processing challenge: the 8th program of handwriting erasure
- Station B, Master Liu Er - dataset and data loading
- 数字经济破浪而来 ,LTD是权益独立的Web3.0网站?
- What impact will frequent job hopping have on your career?
- 【论文阅读】NFlowJS:基于鲁棒学习的合成负数据密集异常检测
- 養了只小猫咪
- PDK process library installation -csmc
- 【经验】UltralSO制作启动盘时报错:磁盘/映像容量太小
- [experience] when ultralso makes a startup disk, there is an error: the disk / image capacity is too small
- 网站进行服务器迁移前应做好哪些准备?
猜你喜欢
随机推荐
Is it difficult for an information system project manager?
Some easy-to-use tools make your essay style more elegant
CoDeSys note 2: set coil and reset coil
Market development prospect and investment risk assessment report of China's humidity sensor industry from 2022 to 2028
B站刘二大人-反向传播
C language learning notes (mind map)
After the project is released, index Html is cached
PDK process library installation -csmc
Leetcode 701 insertion operation in binary search tree -- recursive method and iterative method
实践分享:如何安全快速地从 Centos迁移到openEuler
How to download GB files from Google cloud hard disk
Quantitative description of ANC noise reduction
应用安全系列之三十七:日志注入
LTE CSFB process
Web service connector: Servlet
[SQL Server fast track] - authentication and establishment and management of user accounts
Summary of data sets in intrusion detection field
[Jiudu OJ 07] folding basket
B站刘二大人-数据集及数据加载 Lecture 8
Game push: image / table /cv/nlp, multi-threaded start!