如何确定已提交的表单以及在servlet中验证它们 [英] How to determine which form has been submited and to validate them in a servlet

查看:85
本文介绍了如何确定已提交的表单以及在servlet中验证它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个由表单组成的jsp页面。一个是signup.jsp页面,另一个是login.jsp。两个表单操作都被发送到验证servlet。 servlet将如何确定已提交的表单,因此将根据用户提交的内容进行验证。

I have two jsp pages both consisting of forms. One is a "signup.jsp" page the other "login.jsp". Both form action is sent to "Validate" servlet. How would the servlet determine which form has been submitted, so it will be validated corresponding to which the user submits.

推荐答案

给提交按钮一个唯一的名称。

Give the submit button an unique name.

<form ...>
    ...
    <input type="submit" name="signup" value="Signup" />
</form>



<form ...>
    ...
    <input type="submit" name="login" value="Login" />
</form>

与其他所有输入元素一样,它们的name = value对也将作为请求参数发送。所以,你需要在servlet中做的只是

Like with every other input element, their name=value pair will also be sent as request parameter. So, all you need to do in the servlet is just

if (request.getParameter("signup") != null) {
    // Signup form is submitted.
}
else if (request.getParameter("login") != null) {
    // Login form is submitted.
}






不相关对于具体问题,这种设计有点奇怪。我希望每个表单都有一个单独的servlet。无论你在验证servlet中的代码是什么,都应该重构为一个独立的类,你可以在两个servlet中导入/使用它。


Unrelated to the concrete problem, this design is however somewhat strange. I'd expect a separate servlet for each form. Whatever code you've in "Validate" servlet should be refactored to a standalone class which you could just import/use in the both servlets.

  • doGet and doPost in Servlets
  • Design Patterns web based applications

这篇关于如何确定已提交的表单以及在servlet中验证它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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