如果 JSP 中验证失败则重定向

发布于 2024-10-20 05:05:27 字数 273 浏览 1 评论 0原文

我使用下面的代码从页面中获取一些数据:

int clientId = Integer.parseInt(request.getParameter("clientId"));

我尝试用 try/catch 包围它,然后使用 response.sendRedirect("page.jsp"); 在 catch 块中重定向,但是页面不重定向,它似乎想要完成整个脚本(这反过来会引发更多错误,因为初始变量无效!)。我在这里缺少一些基本的东西吗?

I am grabbing some data from a page using the code below:

int clientId = Integer.parseInt(request.getParameter("clientId"));

I have tried surrounding it with try/catch and then redirecting in the catch block using response.sendRedirect("page.jsp"); but the page does not re-direct, it seems to want to finish the whole script (which in turn throws more errors because the initial variables aren't valid!). Am I missing something basic here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丶情人眼里出诗心の 2024-10-27 05:05:27

阅读您的服务器日志。我的美分是它充满了 IllegalStateException 。另请参阅此答案了解更多详情。

不要滥用 JSP 作为页面控制器。由于 JSP 本身就是响应的一部分,因此您不能再从 JSP 内部更改它(好吧,有 hacks/workarounds,但您不想使用 hacks/workarounds)。要控制请求/响应,您应该使用 servlet。创建一个servlet,让它监听某个URL,让表单提交到该URL,让servlet执行JSP中的所有Java代码,最后控制响应目的地。

您可以在我们的 Servlet wiki 页面 中找到 Hello World 示例。它还涵盖了基本验证。

Read your server logs. My cents on that it's cluttered with IllegalStateExceptions. See also this answer for more detail.

Don't abuse the JSP as a page controller. Since JSP is by itself part of the response, you cannot change it from inside the JSP anymore (OK, there are hacks/workarounds, but you don't want to use hacks/workarounds). To control the request/response, you should be using a servlet. Create a servlet, let it listen on a certain URL, let the form submit to that URL, let the servlet execute all the Java code which you have had in the JSP and finally control the response destination.

You can find a Hello World example in our Servlets wiki page. It also covers basic validation.

许一世地老天荒 2024-10-27 05:05:27

您可能缺少 return 语句(尽管我没有看到您的代码):

response.sendRedirect(..);
return;

如果您只是调用 sendRedirect(..) 这将设置 Location< /code> http header 和 http status,但不会影响当前方法的流程,并且会继续。

You are perhaps missing a return statement (although I don't see your code):

response.sendRedirect(..);
return;

If you simply call sendRedirect(..) this will set the Location http header, and an http status, but will not affect the flow of the current method, and it will continue.

少跟Wǒ拽 2024-10-27 05:05:27

您需要确保 JSP 页面上的所有 scriptlet 逻辑都是有条件的,具体取决于“clientId”的存在,因此您的 catch 块可能需要深入到 JSP 页面的底部。请记住,JSP 页面被编译为 servlet,因此您仍然需要确保代码可以编译。一般来说,我强烈建议您将此逻辑放入一个单独的类(例如,控制器类)中,并最大限度地减少 JSP 页面上的 scriptlet 代码量。否则很难维护。

如果这不能帮助您解决问题,请发布您的整个 JSP 代码,以便我们可以确定在哪里提供建议。

You need to make sure all of your scriptlet logic on the JSP page is conditional depending on the existence of "clientId", so your catch block will probably need to go down to the bottom of the JSP page. Keep in mind that the JSP page is compiled into a servlet, so you still need to make sure the code can compile. In general, I would strongly recommend you put this logic inside a separate class (e.g., a controller kind of class) and minimize the amount of scriptlet code on your JSP page. Just hard to maintain otherwise.

If this doesn't clear things up for you, pls post your entire JSP code so that we can pinpoint where to advise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文