当前位置:网站首页>Request forwarding and redirection
Request forwarding and redirection
2022-07-06 05:58:00 【The corner of fufu】
List of articles
There is login verification and display home page in the web page , How to jump and interact ?
Two Servlet(JSP) There are two ways to jump between :
- Request forwarding
- Response redirection
One 、 Request forwarding
It's a server jump , Only one request will be generated
// Realize the function of request forwarding
request.getRequestDispatcher("/direct/index").forward(request,response);
Request forwarding : The address is direct/check
Console output
Two 、 Response redirection
Redirection is browser side jump , There will be two requests
// Response redirection needs to be increased contextPath
response.sendRedirect("/servlet_advanced/direct/index");
Response redirection : The address is entered as direct/check
After resetting, the address changes to direct/index
Console output
3、 ... and 、 Set request custom properties
Request permission to create custom attributes
Set request properties
request.setAttribute("username", "admin");Get request properties
String name = (String)request.getAttribute("username");
1、 Request forwarding
Request only once , Can get admin attribute
2、 Response redirection
Response redirection , Ask twice , First destroyed , Get the property value as null
Four 、 Program page
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(" User login successful ");
request.setAttribute("username", "admin");
// Realize the function of request forwarding
request.getRequestDispatcher("/direct/index").forward(request,response);
// Response redirection needs to be increased 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 + ".");
}
}
边栏推荐
- 实践分享:如何安全快速地从 Centos迁移到openEuler
- 数字经济破浪而来 ,LTD是权益独立的Web3.0网站?
- Arrays and collections
- Accélération de la lecture vidéo de l'entreprise
- Auto.js学习笔记17:基础监听事件和UI简单的点击事件操作
- Redis message queue
- Embedded interview questions (I: process and thread)
- Network protocol model
- Download, install and use NVM of node, and related use of node and NRM
- 进程和线程
猜你喜欢

First knowledge database

Jushan database appears again in the gold fair to jointly build a new era of digital economy

Analysis report on development trends and investment planning of China's methanol industry from 2022 to 2028

(5) Explanation of yolo-v3 core source code (3)

Redis消息队列

Processes and threads

ArcGIS application foundation 4 thematic map making

How to use the container reflection method encapsulated by thinkphp5.1 in business code

ContentType的作用

MPLS test report
随机推荐
养了只小猫咪
wib3.0 跨越,在跨越(ง •̀_•́)ง
[happy Spring Festival] if you feel happy, dance
Station B Liu Erden linear regression pytoch
Redistemplate common collection instructions opsforvalue (II)
嵌入式面试题(一:进程与线程)
Bit operation rules
nodejs实现微博第三方登录
J'ai un chaton.
[experience] when ultralso makes a startup disk, there is an error: the disk / image capacity is too small
H3C V7 switch configuration IRF
Market development prospect and investment risk assessment report of China's humidity sensor industry from 2022 to 2028
Is it difficult for an information system project manager?
网络协议模型
初识数据库
The digital economy has broken through the waves. Is Ltd a Web3.0 website with independent rights and interests?
[Thesis code] SML part code reading
Web service connector: Servlet
[Baiwen smart home] first day of the course_ Learn Embedded and understand the development mode of bare metal and RTOS
c语言——冒泡排序







