Solved

Java Servlet - getParameter returns null

Posted on 2004-08-17
24
2,012 Views
Last Modified: 2013-11-24
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
Comment
Question by:yooniquely
  • 12
  • 10
  • 2
24 Comments
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11823568
>>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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11823582
while using doget method

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

0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11823627
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11823823
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
 

Author Comment

by:yooniquely
ID: 11823890
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11823913
you said it is problem with doGet

can u post doGet code??
0
 

Author Comment

by:yooniquely
ID: 11823958
   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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11823990
whare is

request.getParameter("ReportNames");  related code??
0
 

Author Comment

by:yooniquely
ID: 11824059
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11824258
are u calling this method doOpNetwork in doPost also??
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11824449
And actually my earlier style of program should work for doGet method
0
 
LVL 2

Accepted Solution

by:
beermequik earned 125 total points
ID: 11824744
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
 

Author Comment

by:yooniquely
ID: 11824870
when i tested it, it returned null... how do i make sure that i pass in the parameter? thanks so much.
0
 

Author Comment

by:yooniquely
ID: 11824941
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
 

Author Comment

by:yooniquely
ID: 11824997
nm it is not index.jsp.

but yeah, it still returns null....
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11825001
String url="index.jsp?tab=reports&ReportNames="+document.yourformname.ReportNames.value;
    document.location.href=url;
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11825050
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
 
LVL 2

Expert Comment

by:beermequik
ID: 11825065
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
 

Author Comment

by:yooniquely
ID: 11825152
inside index.jsp

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

index.jsp?tab=reports
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11825163
re u calling this method doOpNetwork in doPost also??
0
 

Author Comment

by:yooniquely
ID: 11825236
doPost in first servlet.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 11825277
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
 

Author Comment

by:yooniquely
ID: 11825532
I am too...can i email you? thanks a lot.
0
 

Author Comment

by:yooniquely
ID: 11826205
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question