使用 servlet 通过 2 个表单发送变量

发布于 2025-01-08 20:47:27 字数 4049 浏览 1 评论 0原文

我遇到了 2 个 servlet 的问题。基本上我有1个带有表单的jsp页面,当我点击提交时,我需要从这个jps中获取所有变量,以便将它们注入第二个页面(输入类型=“隐藏”),最后一起提交。

我的 servlet 1(添加客户端)

    @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    System.out.println("salut 1  ");
    request.getRequestDispatcher("/JSP/Template/BankAdvisor/AddClient.jsp").forward(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String firstname = request.getParameter("firstname");
    String lastname = request.getParameter("lastname");
    String email = request.getParameter("email");
    String address = request.getParameter("address");
    String city = request.getParameter("city");
    String zipcode = request.getParameter("zipcode");
    String phone = request.getParameter("phone");

    if (firstname != null && lastname != null && email != null && address != null && city != null && zipcode != null && phone != null) {

        if (!lastname.isEmpty() && !firstname.isEmpty() && !email.isEmpty() && !address.isEmpty() && !city.isEmpty() && !zipcode.isEmpty() && !phone.isEmpty()) {

            request.setAttribute("firstname", firstname);
            request.setAttribute("lastname", lastname);
            request.setAttribute("email", email);
            request.setAttribute("address", address);
            request.setAttribute("city", city);
            request.setAttribute("zipcode", zipcode);
            request.setAttribute("phone", phone);
            request.getRequestDispatcher("/BankAdvisor/AddAccount").forward(request, response);

        } else {

            response.sendRedirect(getServletContext().getContextPath() + "/BankAdvisor/AddClient");
        }

    } else {

        response.sendRedirect(getServletContext().getContextPath() + "/BankAdvisor/AddClient");
    }

}

我的 servlet 2(添加帐户)

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.getRequestDispatcher("/JSP/Template/BankAdvisor/AddAccount.jsp").forward(request, response);
    String firstname = request.getParameter("firstname");
    String lastname = request.getParameter("lastname");
    String email = request.getParameter("email");
    String address = request.getParameter("address");
    String city = request.getParameter("city");
    String zipcode = request.getParameter("zipcode");
    String phone = request.getParameter("phone");
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String firstname = request.getParameter("firstname");
    String lastname = request.getParameter("lastname");
    String email = request.getParameter("email");
    String address = request.getParameter("address");
    String city = request.getParameter("city");
    String zipcode = request.getParameter("zipcode");
    String phone = request.getParameter("phone");

    //The two news variables
    String accountName = request.getParameter("accountname");
    String accountType = request.getParameter("accounttype");

    if (firstname != null && lastname != null && email != null && address != null && city != null && zipcode != null && phone != null) {

        if (!lastname.isEmpty() && !firstname.isEmpty() && !email.isEmpty() && !address.isEmpty() && !city.isEmpty() && !zipcode.isEmpty() && !phone.isEmpty()) {

         // ACTION 
        } else {
            response.sendRedirect(getServletContext().getContextPath() + "/BankAdvisor/AddClient");
        }

   } else {
 response.sendRedirect(getServletContext().getContextPath() + "/BankAdvisor/AddClient");
   }
}

谢谢

I got a problem with 2 servlets. Basically I got 1 jsp page with form, when I click on submit, i need to get all variables from this jps, in order to inject them into the second on (input type="hidden"), for finally submit all together.

My servlet 1 (Add client)

    @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    System.out.println("salut 1  ");
    request.getRequestDispatcher("/JSP/Template/BankAdvisor/AddClient.jsp").forward(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String firstname = request.getParameter("firstname");
    String lastname = request.getParameter("lastname");
    String email = request.getParameter("email");
    String address = request.getParameter("address");
    String city = request.getParameter("city");
    String zipcode = request.getParameter("zipcode");
    String phone = request.getParameter("phone");

    if (firstname != null && lastname != null && email != null && address != null && city != null && zipcode != null && phone != null) {

        if (!lastname.isEmpty() && !firstname.isEmpty() && !email.isEmpty() && !address.isEmpty() && !city.isEmpty() && !zipcode.isEmpty() && !phone.isEmpty()) {

            request.setAttribute("firstname", firstname);
            request.setAttribute("lastname", lastname);
            request.setAttribute("email", email);
            request.setAttribute("address", address);
            request.setAttribute("city", city);
            request.setAttribute("zipcode", zipcode);
            request.setAttribute("phone", phone);
            request.getRequestDispatcher("/BankAdvisor/AddAccount").forward(request, response);

        } else {

            response.sendRedirect(getServletContext().getContextPath() + "/BankAdvisor/AddClient");
        }

    } else {

        response.sendRedirect(getServletContext().getContextPath() + "/BankAdvisor/AddClient");
    }

}

My servlet 2 (Add account)

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.getRequestDispatcher("/JSP/Template/BankAdvisor/AddAccount.jsp").forward(request, response);
    String firstname = request.getParameter("firstname");
    String lastname = request.getParameter("lastname");
    String email = request.getParameter("email");
    String address = request.getParameter("address");
    String city = request.getParameter("city");
    String zipcode = request.getParameter("zipcode");
    String phone = request.getParameter("phone");
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String firstname = request.getParameter("firstname");
    String lastname = request.getParameter("lastname");
    String email = request.getParameter("email");
    String address = request.getParameter("address");
    String city = request.getParameter("city");
    String zipcode = request.getParameter("zipcode");
    String phone = request.getParameter("phone");

    //The two news variables
    String accountName = request.getParameter("accountname");
    String accountType = request.getParameter("accounttype");

    if (firstname != null && lastname != null && email != null && address != null && city != null && zipcode != null && phone != null) {

        if (!lastname.isEmpty() && !firstname.isEmpty() && !email.isEmpty() && !address.isEmpty() && !city.isEmpty() && !zipcode.isEmpty() && !phone.isEmpty()) {

         // ACTION 
        } else {
            response.sendRedirect(getServletContext().getContextPath() + "/BankAdvisor/AddClient");
        }

   } else {
 response.sendRedirect(getServletContext().getContextPath() + "/BankAdvisor/AddClient");
   }
}

Thanks

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

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

发布评论

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

评论(1

飘然心甜 2025-01-15 20:47:27

在 servlet 1 中,您应该使用 :

getServletContext().getRequestDispatcher("/BankAdvisor/AddAccount").forward(request, response);

而不是 :

request.getRequestDispatcher("/BankAdvisor/AddAccount").forward(request, response);

因为 ServletRequest#getRequestDispatcher(java.lang.String),如果路径以“/”开头,则被解释为相对于当前路径上下文根(即当前请求的上下文路径)。

  • 示例:如果您在 http://test/myapp/toto 上收到请求,它将转发到 http://test/myapp/toto/BankAdvisor/AddAccount

ServletContext#getRequestDispatcher(java.lang.String) 路径被解释为相对于当前上下文根(即当前 Web 应用程序的上下文路径)。

  • 示例:如果您在 http://test/myapp/toto 上收到请求,如果 web 应用程序上下文,它将在 http://test/myapp/BankAdvisor/AddAccount 上转发根目录是http://test/myapp

In servlet 1 you should use :

getServletContext().getRequestDispatcher("/BankAdvisor/AddAccount").forward(request, response);

instead of :

request.getRequestDispatcher("/BankAdvisor/AddAccount").forward(request, response);

because with ServletRequest#getRequestDispatcher(java.lang.String), if the path begins with a "/" it is interpreted as relative to the current context root (i.e. context path of the current request).

  • Example : if you received a request on http://test/myapp/toto it will forward on http://test/myapp/toto/BankAdvisor/AddAccount

Whereas with ServletContext#getRequestDispatcher(java.lang.String) path is interpreted as relative to the current context root (i.e. context path of the current web application).

  • Example : if you received a request on http://test/myapp/toto it will forward on http://test/myapp/BankAdvisor/AddAccount if the webapp context root is http://test/myapp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文