what is wrong with <strong>if (request.getParameterValues("Array_List") == null) </strong>
4 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   jie_huang
Posted On:   Wednesday, August 21, 2002 08:14 AM

Hi, I used the following code to get the value passed from a checkbox

String[] strArray =
request.getParameterValues("Checkbox_List");




Then I tried to use the following code to prevent someone from going to this page directly. but I got a nullPointerException.

if (strArray==null)

response.sendRedirect("AdminTasks.jsp");




Can someone tell me what is wrong here?

Thanks



Jie

Re: what is wrong with <strong>if (request.getParameterValues("Array_List") == null) </strong>

Posted By:   jie_huang  
Posted On:   Wednesday, August 21, 2002 10:48 AM

Hi, all:



thanks for your reply. I just found that the problem is that I need to write something like:

if (request.getParameterValues("Array_List")==null) {
response.sendRedirect("AdminTasks.jsp")

}else{

then do something else

}


I omitted the "}else{...}" part previously.

Jie

Re: what is wrong with <strong>if (request.getParameterValues("Array_List") == null) </strong>

Posted By:   michael_dean  
Posted On:   Wednesday, August 21, 2002 09:44 AM

There is absolutely nothing wrong with:




String[] strArray = request.getParameterValues("Checkbox_List");
if (strArray==null)
response.sendRedirect("AdminTasks.jsp");


However, shortly after the response.sendRedirect(...) method call, you use the strArray object reference to access values in the array. That is the problem.



After the call to response.sendRedirect(...) returns, the rest of the code in your doXXX(...) method (including the access of strArray) is processed--yes, it really is!--resulting in a NullPointerException.



That means, you can either create an empty array within the if block, enclose the "else" code in an else block, or forward the request (instead of redirecting it). To see for yourself, test the following servlet:




import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
The RedirectVsForward servlet accepts requests and will either redirect
or forward the request depending upon the processing instructions received.
It allows the user to see that after a response.sendRedirect(...)
method call returns, processing continues in the doGet(...)
method. This means that when sending a redirect due to missing parameter
data, null values must still be handled appropriately.

Use the following URL's to test the servlet:

  • Forwards:


    • No values for "checkbox_list", does not access the array
      (proves forward works):

      http://localhost:8080/servlet/RedirectVsForward?processing_instructions=forward

    • No values for "checkbox_list", does access the array
      (proves forward will work without checking for null):

      http://localhost:8080/servlet/RedirectVsForward?processing_instructions=forward&access_array=true

    • Values for "checkbox_list", does not access the array
      (does not forward because it receives valid values):

      http://localhost:8080/servlet/RedirectVsForward?processing_instructions=forward&checkbox_list=1&checkbox_list=2

    • Values for "checkbox_list", does access the array
      (does not forward because it receives valid values--the values will be displayed):

      http://localhost:8080/servlet/RedirectVsForward?processing_instructions=forward&checkbox_list=1&checkbox_list=2&access_array=true


  • Redirects:


    • No values for "checkbox_list", does not access the array
      (proves redirect works):

      http://localhost:8080/servlet/RedirectVsForward?processing_instructions=redirect

    • No values for "checkbox_list", does access the array
      (proves redirect requires checking for null):

      http://localhost:8080/servlet/RedirectVsForward?processing_instructions=redirect&access_array=true

    • Values for "checkbox_list", does not access the array
      (does not redirect because it receives valid values):

      http://localhost:8080/servlet/RedirectVsForward?processing_instructions=redirect&checkbox_list=1&checkbox_list=2

    • Values for "checkbox_list", does access the array
      (does not forward because it receives valid values--the values will be displayed):

      http://localhost:8080/servlet/RedirectVsForward?processing_instructions=redirect&checkbox_list=1&checkbox_list=2&access_array=true



*/
public class RedirectVsForward extends HttpServlet {

public void doGet(HttpServletRequest request,
HttpServletResponse response) throws IOException,
ServletException {
String processingInstructions = request.getParameter("processing_instructions");
String accessArray = request.getParameter("access_array");
String[] strArray = request.getParameterValues("checkbox_list");
if (null == strArray) {
if ("forward".equalsIgnoreCase(processingInstructions)) {
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.html");
dispatcher.forward(request, response);
} else {
response.sendRedirect("/index.html");
}
}
PrintWriter out = response.getWriter();
out.println("Not Redirected.");
if (null != accessArray) {
for (int i=strArray.length-1; i>=0; i--) {
out.println(strArray[i]);
}
}
}

}

Re: what is wrong with <strong>if (request.getParameterValues("Array_List") == null) </strong>

Posted By:   Adrian_K  
Posted On:   Wednesday, August 21, 2002 09:11 AM

Does AdminTasks.jsp exists (in the correct dir)?

Re: what is wrong with <strong>if (request.getParameterValues("Array_List") == null) </strong>

Posted By:   zhaohua_meng  
Posted On:   Wednesday, August 21, 2002 09:04 AM

Can you post the entire stack trace here?
About | Sitemap | Contact