java 没有脚本的 JSP 加载调用 servlet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14606245/
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-31 16:48:12  来源:igfitidea点击:

JSP onload call servlet without scriptlets

javahtmljspservlets

提问by Noah Martin

I want that when my JSP is loaded to call a servlet. I have this link:

我希望在我的 JSP 加载时调用 servlet。我有这个链接:

<a href="ContentServlet?action=userContents">Homepage</a>

But this way I have to click the link, I want to perform the calling automatically when the jsp is loaded.

但是这样我必须点击链接,我想在加载jsp时自动执行调用。

On the other side I need to use no scriptlets. Does anyone have any idea how to do this?

另一方面,我不需要使用脚本。有谁知道如何做到这一点?

采纳答案by Piyush Joshi

Although its marked as resolved but I am editing my answer for future reference: Apart from the javascript solution you can accomplish this with 2 more options using jsp tags:

虽然它被标记为已解决,但我正在编辑我的答案以供将来参考:除了 javascript 解决方案之外,您还可以使用 jsp 标签通过另外 2 个选项来完成此操作:

Option1:You can forward the request to the corresponding servlet. Use jsp standard action jsp:forward, e.g.:

Option1:可以将请求转发给对应的servlet。使用 jsp 标准动作 jsp:forward,例如:

<jsp:forward page="ContentServlet?action=userContents" >
</jsp:forward>

You can replace your link with the above tag and the servlet will be called.

你可以用上面的标签替换你的链接,servlet 将被调用。

Option2:you can redirect the request to your servlet using JSTL tags:

选项 2:您可以使用 JSTL 标签将请求重定向到您的 servlet:

<c:redirect url="ContentServlet?action=userContents" />

Again you can replace your link with the above tag.

同样,您可以用上面的标签替换您的链接。

In Option1 browser's url will not change. In Option 2 browser's url will change to "ContentServlet?action=userContents"

在 Option1 浏览器的 url 不会改变。在选项 2 浏览器的 url 将更改为“ContentServlet?action=userContents”

Hope it solves your problem.

希望它能解决你的问题。

回答by azraelAT

Why don't you use JavaScript?

为什么不使用 JavaScript?

 <script type="text/javascript">
        function redirect(){
        window.location = "/ContentServlet?action=userContents"
        }
</script>

    ...

    <body onLoad="redirect()">