Servlet跳转

在Servlet内实现跳转

常见的Servlet内跳转有两种:
(1)重定向:对应JSP内置对象中的sendRedirect
respose.sendRedirect(“URL地址”);
(2)服务器内跳转:对应JSP中的forward标签
ServletContext application = this.getServletContext();
ServletContext application = this.getServletContext();
RequestDispatcher rd = application.getRequestDispatcher(“URL地址”);
rd.forward(request, response);

编写跳转Servlet

创建一个名为JumpServlet的Servlet
代码如下:

package cn.lystery.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class JumpServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String user = "Lystery";//模拟用户
        req.setAttribute("user", user);
        ServletContext application = this.getServletContext();
        RequestDispatcher rd = application.getRequestDispatcher("/index.jsp");
        rd.forward(req, resp);
    }

}

配置Servlet

配置代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
                    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <servlet>
        <servlet-name>JumpServlet</servlet-name>
        <servlet-class>cn.lystery.servlet.JumpServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>JumpServlet</servlet-name>
        <url-pattern>/JumpServlet</url-pattern>
    </servlet-mapping>

</web-app>

编写index.jsp界面

index.jsp页面存在于WebContent目录下
这里写图片描述
编写index.jsp界面

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>欢迎界面</title>
</head>
<body>
欢迎<br>
<%=request.getAttribute("user") %>
</body>
</html>

部署并测试JumpServlet

在地址栏输入http://localhost:8080/MyServlet/JumpServlet
这里写图片描述
发现已经跳转到index.jsp界面,显示出对应内容

使用重定向进行跳转

对JumpServlet进行如下更改

package cn.lystery.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class JumpServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String user = "Lystery";//模拟用户
        req.setAttribute("user", user);
        resp.sendRedirect("index.jsp");
    }

}

测试发现 当运行是 地址路径变成index.jsp但是传递的参数不见了 这就是两者的区别
这里写图片描述

servlet中,可以使用RequestDispatcher的forward方法来跳转页面。例如,可以使用以下代码将请求转发到名为"EShop.jsp"的JSP页面: ```java String url = "/EShop.jsp"; ServletContext sc = getServletContext(); RequestDispatcher rd = sc.getRequestDispatcher(url); rd.forward(req, res); ``` 这将把请求和响应对象传递给目标页面,使目标页面能够处理请求并生成响应。注意,使用forward方法跳转页面后,地址栏不会发生变化。\[1\] 另外,需要注意的是,forward方式只能跳转到本web应用中的页面上,无法跳转到其他应用或外部网页。如果想要在跳转后传递参数,可以使用url中带parameter、session或request.setAttribute等方法。\[3\] #### 引用[.reference_title] - *1* *2* [Servlet跳转到jsp页面的几种方法](https://blog.csdn.net/weixin_30713953/article/details/96238984)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Servlet跳转页面](https://blog.csdn.net/weixin_40912987/article/details/116167119)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值