java 重定向到某个 servlet 的空白 index.jsp

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6049824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:08:25  来源:igfitidea点击:

blank index.jsp redirecting to some servlet

javajspservlets

提问by dantuch

Is it possible to have some kind of empty JSP (index.jsp) with ~only autoredirecting to servlet?

是否有可能有某种空的 JSP (index.jsp) 和 ~only自动重定向到 servlet?

Or can I start my web app not from page (jsp/html) but from servlet? (web.xml says no)

或者我可以不从页面(jsp/html)而是从 servlet 启动我的 Web 应用程序?(web.xml中说没有

I have logic needed in (for example) index.jsppage inside my Logic.javaservlet - that's why I need to use servlet before any useful JSP (don't want to mix logic with UI using scriptlets)

我的servletindex.jsp内的(例如)页面中需要逻辑Logic.java- 这就是为什么我需要在任何有用的 JSP 之前使用 servlet(不想使用 scriptlet 将逻辑与 UI 混合)

Does it can be done?

可以做到吗?

采纳答案by BalusC

That's possible if you provide an empty index.jspand map the servlet on

如果您提供一个空的index.jsp并将 servlet 映射到

<url-pattern>/index.jsp</url-pattern>

The servlet should in turn however forward to a JSP to present the results. The very same index.jspmaybe? :)

然而,servlet 应该反过来转发到 JSP 以呈现结果。index.jsp可能是一样的?:)

回答by Amir Raminfar

I think what you want is

我想你想要的是

<jsp:forward page="/controller-name" />

or you can redirect using

或者您可以使用重定向

<c:redirect page="..."/>

The difference is first will forward which means the user's url won't change and the latter will change the user's url.

区别在于首先会转发,这意味着用户的 url 不会改变,而后者会改变用户的 url。