0

I am using query string for sending parameter to other page in form action tag but when I am fetching the value on the next page, it is showing null and null is stored in the database.

My code is as follows:

out.println("<html>");
out.println("<form action=SubmitTimesheet?manager="+manager+">");

and on the other page I am trying to fetch the value of parameter in following manner:

String manager=request.getParameter("manager");
out.println(manager);

But it is printing null and further if I am trying to store the fetched value in database it is storing null.

Please help.

Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
cr.7
  • 85
  • 1
  • 8
  • 1
    first you need to check `out.println("
    ");'. Did you check manager having value before send to servlet?
    – androidGenX Feb 12 '16 at 05:13
  • Can you please check in the first page whether manager is null or not? – prasad Feb 12 '16 at 05:13
  • yes i have already checked that on the first page it is showing the correct value for manager but on the second page it is showing null. – cr.7 Feb 12 '16 at 05:18
  • how two pages are connected? can u post tat code? it may be the issue mapping from 1st page to 2nd – arch Feb 12 '16 at 05:20
  • my first page code is like this : out.println(""); out.print("
    "); out.println(""); and second page code is like this String manager=request.getParameter("manager"); out.println(manager); on the first page it is printing the required value but on the second page it is printing null
    – cr.7 Feb 12 '16 at 05:45
  • @cr.7 check this link- http://stackoverflow.com/questions/19946277/how-to-pass-a-string-value-from-one-servlet-to-another-servlet – arch Feb 12 '16 at 05:55
  • @cr.7 is your problem fixed? – androidGenX Feb 12 '16 at 06:06
  • yes my problem got solved i have use session object and passed the value of manager in session variable instead of query string. – cr.7 Feb 12 '16 at 07:02

2 Answers2

0

Don't add the manager query parameter if the manager value is null.

Also remember to encode query parameters.

And finally, the value of the action attribute should be quoted.

out.print("<form action=\"SubmitTimesheet");
if (manager != null)
    out.print("?manager=" + URLEncoder.encode(manager, "UTF-8"));
out.println("\">");

Instead of the "UTF-8" string literal, you can also use StandardCharsets.UTF_8.


UPDATE

In reality, you shouldn't add query parameters to the action of a <form>. The form will post all the values of <input> and other form related elements, so what you should do, is add the manager value as a hidden input field:

out.println("<html>");
out.println("<form action=\"SubmitTimesheet\">");
if (manager != null)
    out.print("<input name=\"manager\" type=\"hidden\" value=\"" +
              StringEscapeUtils.escapeHtml4(manager) + "\">");

The above uses the library suggested by answer to question Recommended method for escaping HTML in Java.

Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • thanks for the suggestion . i applied your code in my page. but it is still storing null in database.It means the value of manager on the second page is still null. – cr.7 Feb 12 '16 at 06:00
  • @cr.7 I've never tried query parameters on a form action, so that may not even work. See update for correct way to do this. – Andreas Feb 12 '16 at 17:20
0

i have use session object and passed the value of manager in session variable instead of query string. i have used the following code:

ses.putValue("manager", manager);

on the first servlet page and then on the second servlet page i fetched the value like this :

String manager=ses.getValue("manager").toString();

out.println(manager);

now it is printing the correct value instead of null

cr.7
  • 85
  • 1
  • 8