[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2090
  • Last Modified:

Java Servlet - getParameter returns null

I am trying to get the string reportName but it is always null. The following code is inside my function that is called from doGet. What could be wrong? Please help. Thanks.

        String reportName = request.getParameter("ReportNames");
        out.println(reportName);
        if ( reportName == null) {
              toolbar.addElement(new Button().addElement("Select").addAttribute("onClick", "(confirm('Please select Report'))"));
        }
        else {
              toolbar.addElement(new Button().addElement("View").addAttribute("onClick", "window.open('/ne/report/reports.jsp', '_blank', 'width=1200, height=750, resizable=yes, scrollbars=yes, status=no, toolbar=no'); return false;"));
        }
0
yooniquely
Asked:
yooniquely
  • 12
  • 10
  • 2
1 Solution
 
sudhakar_koundinyaCommented:
>>What could be wrong?

1. ReportNames should be case sensitive. means you need to send the parameter name with this only
2. should be either text field or hiden element or url parameter. check how u r passing?
0
 
sudhakar_koundinyaCommented:
while using doget method

the value should be from url only. hidden or text field elements will not work

0
 
sudhakar_koundinyaCommented:
your code  should be some thing like this while working with doGet method of servlets

<script>
function loadpage()
{
   String url="yourUrl?ReportNames="+document.yourformname.ReportNames.value;
}
</script>

<body>
<form name=yourformname>
<input type=text name=.ReportNames>
<input type=button name=text value="Load Page" onClick=loadpage()>
</form>
</body>
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
sudhakar_koundinyaCommented:
your code  should be some thing like this while working with doGet method of servlets

<script>
function loadpage()
{
   String url="yourUrl?ReportNames="+document.yourformname.ReportNames.value;
    document.location.href=url;
}
</script>

<body>
<form name=yourformname>
<input type=text name=.ReportNames>
<input type=button name=text value="Load Page" onClick=loadpage()>
</form>
</body>
0
 
yooniquelyAuthor Commented:
sorry i am a beginner at java servlets and i dont understand how that'll work...(i'm working with someone else's code)

my doPost function:

        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        response.setHeader("pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        try
        {
            LEAPFramework lf = ServletGlue.getLeap(this, request);
           
            // STEP 1: Extract HTTP request parameters...
           
            // op
            /*
            String op = request.getParameter("op");
            if (op == null)
                op = "load";*/
            NetworkElementPK nePK = getNEPK(lf, request);
            String cat = request.getParameter("cat");
            String type = request.getParameter("type");

            // STEP 2a: HTML head...
            Head head = new Head();
            head.addElement(new Link().setRel("stylesheet").setType("text/css").setHref("/servlet/ThemeServlet?themeColor=9966cc"));

            // STEP 2b: HTML body...            
            Body body = new Body();
            body.setStyle("margin: 0;");

            TreeWidgetContentProvider callback = new MyCallback(lf, request);
            TreeWidget treeWidget = new TreeWidget(request, new String[] { "root", "nePK" }, callback);
            treeWidget.setIcons(new String[] {null, "/i/ne.gif" });
            Element leftSide = new StringElement(treeWidget.toString());

            Element rightSide = new StringElement("&nbsp;");

           
            {
                ReportNEServlet ns = new ReportNEServlet(); // this class contains the function above
                rightSide = ns.doOpNetwork(lf, request, response, out); // which is this one.
            }
            // inventory perspective ops...
               
            Table table = new Table().setBorder(0).setCellPadding("6pt").setCellSpacing(0).setWidth("100%").setHeight("100%");
            TR tr = new TR().setVAlign("top");
            tr.addElement(new TD(new Div().addElement(leftSide).setStyle("width: 240px")).setStyle("width: 240px; background-color: #eeeeee;"));
            tr.addElement(new TD(rightSide));
            table.addElement(tr);

            body.addElement(table);
           
            // final assembly...!
            Html html = new Html().addElement(head).addElement(body);
            ServletGlue.setPrettyPrint(html, true);
            html.output(out);            
        }
        catch (Exception e)
        {
            out.println("<pre>");
            e.printStackTrace(out);
            out.println("</pre>");
        }
0
 
sudhakar_koundinyaCommented:
you said it is problem with doGet

can u post doGet code??
0
 
yooniquelyAuthor Commented:
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException
    {
        PrintWriter out = response.getWriter();
        try
        {
            response.setContentType("text/html");
            response.setHeader("pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");

            LEAPFramework lf = ServletGlue.getLeap(this, request);
           
            // STEP 1: Extract HTTP request parameters...
            // op
            String op = request.getParameter("op");
           

            NetworkElementPK nePK = getNEPK(lf, request);
            String type = request.getParameter("type");

            // STEP 2: Render HTML...
           
            // STEP 2a: HTML head...
            Head head = new Head();
            head.addElement(new Link().setRel("stylesheet").setType("text/css").setHref("/servlet/ThemeServlet?themeColor=9966cc"));

            // STEP 2b: HTML body...            
            Body body = new Body();
            body.setStyle("margin: 0;");
           
            TreeWidgetContentProvider callback = new MyCallback(lf, request);
            TreeWidget treeWidget = new TreeWidget(request, new String[] { "root", "nePK" }, callback);
            Element leftSide = new StringElement(treeWidget.toString());

            Element rightSide = new StringElement("op: " + op);


            Table table = new Table().setBorder(0).setCellPadding("6pt").setCellSpacing(0).setWidth("100%").setHeight("100%");
            TR tr = new TR().setVAlign("top");
            tr.addElement(new TD(new Div().addElement(leftSide).setStyle("width: 200px")).setStyle("width: 200px; background-color: #eeeeee;"));
            tr.addElement(new TD(rightSide));
            table.addElement(tr);
           
            body.addElement(table);
           
            // final assembly...!
            Html html = new Html().addElement(head).addElement(body);
            ServletGlue.setPrettyPrint(html, true);
            html.output(out);            
        }
        catch (Exception e)
        {
            out.println("<pre>");
            e.printStackTrace(out);
            out.println("</pre>");
        }
0
 
sudhakar_koundinyaCommented:
whare is

request.getParameter("ReportNames");  related code??
0
 
yooniquelyAuthor Commented:
oh, sorry that code is inside this function:

    static Element doOpNetwork(LEAPFramework lf, HttpServletRequest request, HttpServletResponse response, PrintWriter out)
        throws Exception
    {
        // http request parameters...
        List list = new ArrayList();
        String[] pks = request.getParameterValues("nePK");
        if (pks != null)
            list = Arrays.asList(pks);

        // these are the checkboxes
        Script script = new Script();
        script.addElement("function toggle(checked) {");
        script.addElement("  var form = document.forms['form'];");
        script.addElement("  for (var i = 0; i < form.elements.length; ++i) {");
        script.addElement("    if (form.elements[i].type == 'checkbox')");
        script.addElement("      form.elements[i].checked = checked;");
        script.addElement("  }");
        script.addElement("}");
       
        Map theMap = new LinkedHashMap();
       
        Set detailsSet = lf.getNEManager().getAllNEs();
        for (Iterator neIter = detailsSet.iterator(); neIter.hasNext();)
        {
            NetworkElementDetails neDetails = (NetworkElementDetails) neIter.next();
               
            Map row = new LinkedHashMap();
           
            String neType = neDetails.getType();
            String neName = neDetails.getName();
           
            // neName
            StringElement se = new StringElement();
            se.addElement(new IMG("/default/images/tree/ne.gif"));
            se.addElement(new BR());
            se.addElement(neName);
            row.put("NE Name", se);

            // neType
            row.put("NE Type", neType);

            theMap.put(neDetails.getNEPK().toString(), row);
        }
       
        Div toolbar = new Div();

        toolbar.setClass("toolbar");
        toolbar.addElement(new Div().addElement("<SELECT NAME=\"ReportNames\"> <OPTION VALUE=\"none\">Choose Report <OPTION VALUE=\"Hardware\">Hardware <OPTION VALUE=\"License\">License</SELECT>"));
       
        String reportName = request.getParameter("ReportNames");
        out.println(reportName);
        if ( getReportName() == null) {
              toolbar.addElement(new Button().addElement("Select").addAttribute("onClick", "(confirm('Please select Report'))"));
        }
        else {
              toolbar.addElement(new Button().addElement("View").addAttribute("onClick", "window.open('/ne/report/reports.jsp', '_blank', 'width=1200, height=750, resizable=yes, scrollbars=yes, status=no, toolbar=no'); return false;"));
        }
        Form form = new Form().setAction("?").setMethod("get");
        form.setName("form");
        form.addElement(new Input("hidden", "perspective", "network"));
        form.addElement(new Input ("hidden", "ReportNames", request.getParameter("ReportNames")));
        form.addElement(new Input("hidden", "op", ""));

        form.addElement(new Div().addElement(toolbar));
        form.addElement(new Div("&nbsp;"));
               
        GridWidget gridWidget = new GridWidget(request, "nePK", theMap);
        form.addElement(gridWidget.toString());
       
        form.addElement(new Div("&nbsp;"));
        //form.addElement(new Div().addElement(toolbar));
       
        StringElement result = new StringElement();
        result.addElement(script);
        result.addElement(new Div("&nbsp;"));
        result.addElement(form);
        return result;
    }

could you also tell me how to get the values for checkboxes and working with the buttons? Thanks!!!
0
 
sudhakar_koundinyaCommented:
are u calling this method doOpNetwork in doPost also??
0
 
sudhakar_koundinyaCommented:
And actually my earlier style of program should work for doGet method
0
 
beermequikCommented:
Here is something I always do during the development stage, in my servlets to ensure I am really passing what I want to pass.

Enumeration enum = request.getParameterNames();
while (enum.hasMoreElements()){
    String name = (String)enum.nextElement();
    String val  = (String)request.getParameter(name);
    System.out.println("<insert servlet name here>:"+name+"="+val+"!");
}

That always tells me the name and values of the parameters passed.
0
 
yooniquelyAuthor Commented:
when i tested it, it returned null... how do i make sure that i pass in the parameter? thanks so much.
0
 
yooniquelyAuthor Commented:
also what should i put in the yourUrl? because it uses jsp... when i load the page it says index.jsp?tab=reports

   String url="yourUrl?ReportNames="+document.yourformname.ReportNames.value;
    document.location.href=url;
0
 
yooniquelyAuthor Commented:
nm it is not index.jsp.

but yeah, it still returns null....
0
 
sudhakar_koundinyaCommented:
String url="index.jsp?tab=reports&ReportNames="+document.yourformname.ReportNames.value;
    document.location.href=url;
0
 
sudhakar_koundinyaCommented:
can u post the modified code and also let me know  the names of page1 and page2,  what is page1 and what is page 2 in terms of JSP and/or servlet

0
 
beermequikCommented:
How is the servlet getting invoked?  Are you clicking a button on another web page?

It would help to see the code from the page that invokes the servlet.  That would tell us what parameters should be passed to the servlet, and hopefully find out why the one you are expecting is null.
0
 
yooniquelyAuthor Commented:
inside index.jsp

if (tab.equals("reports"))
{
      frameLeftWidth="0";
    leftFrame="about:blank";
    rightFrame="/servlet/ReportServlet";
}

index.jsp?tab=reports
0
 
sudhakar_koundinyaCommented:
re u calling this method doOpNetwork in doPost also??
0
 
yooniquelyAuthor Commented:
doPost in first servlet.
0
 
sudhakar_koundinyaCommented:
I am sorry to say that, I am really confused

I suggest you to place ur entire code of servlet and jsp in some public domain or here. Then I can try to help you

Regards
Sudhakar
0
 
yooniquelyAuthor Commented:
I am too...can i email you? thanks a lot.
0
 
yooniquelyAuthor Commented:
actually it was just an html error...i needed a <form> in front of the <select> stuff..thanks anyway. printline statements helped.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 12
  • 10
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now