可以从Java Servlet API发送到相对路径的重定向吗? [英] Can a redirect to relative path be sent from Java Servlet API?

查看:91
本文介绍了可以从Java Servlet API发送到相对路径的重定向吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向浏览器发送相对重定向(到原始请求域和端口).

I would like to send relative redirect (to the original request domain & port) to the browser.

赞:

Location: /app

但是Jetty自动发送完整的绝对路径

But Jetty send automatically the full absolute path

Location:http://10.0.2.17:9080/app

文档还提到必须通过servlet容器将相对URL转换为绝对URL.

The documentation also mentions that the relative URL must be converted to the absolute URL by the servlet container.

甚至可以通过重定向发送相对网址吗?

Is it even possible to send a relative URL with redirect?

推荐答案

只需手动设置重定向状态和标头.将response.sendRedirect(url)替换为

Just manually set the redirect status and header. Replace response.sendRedirect(url) by

response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
response.setHeader("Location", url);

这篇关于可以从Java Servlet API发送到相对路径的重定向吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆