Servlet如何调用多个提交按钮

时间:2012-10-17 08:27:24

标签: java hibernate jsp servlets

我有一个关于我的简单应用程序的问题我有2个单独的.JSP文件让我们说注册和login.jsp都有提交动作,这两个jsp共享同一个servlet让我们说usercontrol.servlet如何调用动作一旦登录按钮也与register.jsp相同,如果他们共享公共servlet?

这是我的servlet代码:

if(request.getParameter("register").equals("Register")){
            if(request.getParameter("first") == "" || request.getParameter("middle") == "" || request.getParameter("last") == "" || 
                    request.getParameter("loginid") == "" || request.getParameter("pass") == "" || request.getParameter("pass2")== ""){
                JOptionPane.showMessageDialog(null, "Cannot leave null value from one of the textfields");
                response.sendRedirect("AdminPage/Register.jsp");

            }
            //else if(request.getParameter("pass") != request.getParameter("pass2")){
                //JOptionPane.showMessageDialog(null, "Password Mismatch, Check you password and retype it correctly");
                //response.sendRedirect("AdminPage/Register.jsp");
            //}
            else{
                RegisterUser(request, response);
            }
    }

    else if(request.getParameter("login").equals("Login")){
        if(request.getParameter("username") == ""){
            JOptionPane.showMessageDialog(null, "Username is required");
            response.sendRedirect("AdminPage/Login.jsp");
        }
        else if(request.getParameter("password") == ""){
            JOptionPane.showMessageDialog(null, "Password is required");
            response.sendRedirect("AdminPage/Login.jsp");
        }
        else if(request.getParameter("username") == "" || request.getParameter("password") == ""){
            JOptionPane.showMessageDialog(null, "Password/Username is required");
            response.sendRedirect("AdminPage/Login.jsp");
        }
        else{
            LoginUser(response, request);
        }   
    }

我正在将我的应用程序与hibernate集成。

这是我的jsp提交按钮我没有包含整个表单我只是包含提交按钮。

的Login.jsp

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

Register.jsp

<input type="submit" name="register" value="Register"/>

我的问题是,每次运行应用程序时,它总是会在if..else语句中执行所有操作,还有其他方法可以解决此问题吗?

1 个答案:

答案 0 :(得分:0)

在你的jsp中你应该有提交按钮

用于login.jsp

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

表示register.jsp

<input type="submit" name="register" value="Register"/>

现在在servlet中你可以检查

if(request.getParameter("login") != null) {
  // request is comming from login jsp
  // do validation, perform actual transaction 
} else if(request.getParameter("register") != null) {
  // request is comming from register jsp
  // do validation, perform actual transaction 
}