0

I am trying to pass two values from my JSP page to servlet using request dispatcher.

Here is the JSP code:

<%
            String companyName = (String) request
                    .getAttribute("adminCompnayName");
            String tenantId = (String) request.getAttribute("adminTenantId");

            request.setAttribute("adminCompnayName", companyName);

            request.setAttribute("adminTenantId", tenantId);

request.setAttribute("adminTenantid","my compnay");
            request.setAttribute("adminCompanyname"," my tenant id");
            String servletName="../StoreUserDetails";
            RequestDispatcher dispatcher = request.getRequestDispatcher(servletName);
            if (dispatcher != null){
            dispatcher.forward(request, response);
            } 

These two values are received from another servlet.

Here is the servlet code where I am receiving values.

String tenantId = (String) request.getAttribute("adminTenantid");
        String companyName = (String) request
                .getAttribute("adminCompanyname");




out.println("--------------------------"
                + request.getAttribute("adminTenantid"));
        out.println("--------------------------"
                + request.getAttribute("adminCompanyname"));

But I am getting null values.

--------------------------null 

--------------------------null

How to fix it?

4

1 回答 1

3

您将属性存储为adminTenantIdand ,并将它们作为andadminCompnayName获取到 servlet 中。检查属性的拼写,或者更好的是,定义常量并在任何地方使用它们,以避免此类拼写错误。adminTenantidadminCompanyname

也就是说,设计良好的应用程序应该从 servlet 分派到 JSP,而不是从 JSP 分派到 servlet。JSP 不应使用 scriptlet,而应仅用于使用 EL、JSTL 和其他自定义标记生成标记。

于 2012-10-21T09:26:48.147 回答