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?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52
  • 1
    You seem to completely misunderstand how to properly use JSP/Servlets. Your JSP is been used as a controller and your servlet is been used as a view. It should be the other way round. Put your mouse above the `[servlets]` tag which you placed on the question until a black box shows up and then click therein the *info* link. Read this carefully and thoroughly. – BalusC Oct 21 '12 at 12:11

1 Answers1

3

You store the attributes as adminTenantId and adminCompnayName, and get them in the servlet as adminTenantid and adminCompanyname. Check the spelling of your attributes, or even better, define constants and use them everywhere, to avoid such typos.

That said, a well designed application should dispatch from a servlet to a JSP, and not from a JSP to a servlet. JSPs should not use scriptlets and should only be used to generate markup using EL, the JSTL and other custom tags.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255