• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting null value from radio button

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote programm using jsp , servlet that choose one value from the radio button and go to another page according to my choose.

my program is : jsp

<form action=TestUser3 METHOD="post">


<input type="radio" name="kaspomat" value="a1" CHECKED>

radio button 1




<input type="radio" name="kaspomat" value="a10" CHECKED>

radio button 2




<input type="radio" name="kaspomat" value="a20" CHECKED>

radio button 3




<input type="submit" VALUE="Submit" >






<td> what your choise </td>


and the servlet is:

@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request1, HttpServletResponse response1) throws ServletException, IOException {{
// TODO Auto-generated method stub

// try {
// super.doPost(request1, response1);
// } catch (ServletException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
// catch (IOException e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}

// TODO Auto-generated catch block







doPost1(request1, response1, null, null);

// TODO Auto-generated catch block

}




public String[] Kaspomat(HttpServletRequest request1, HttpServletResponse response1,String kaspomat){
String[] kaspomat1=request1.getParameterValues("radios");
return kaspomat1;
}



void doPost1 (HttpServletRequest request1, HttpServletResponse response1, Object object,String kaspomat)throws ServletException,IOException {
Kaspomat(request1, response1,kaspomat);
try {
if((kaspomat) != null) {

if(kaspomat.equals("a1")) {

javax.servlet.RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/indexa1.jsp");
rd.forward(request1, response1);



}

if(kaspomat.equals("a10")) {
response1.sendRedirect("http://www.ynet.il");
}

if(kaspomat.equals("a20")) {

response1.sendRedirect("http://www.nrg.il");
}

}
else {
javax.servlet.RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/indexwrong.jsp");
rd.forward(request1, response1);

}
}
catch (Exception e) {
e.printStackTrace();
}

}


}

my problem is why I get null value from the button?

please someone can help me?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags. As it stands, your code is unreadable.
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



and the servlet is:

Like Bear Bibeault says, please use code tags. I added them for you so you could see what it looks like.
I would change a few things to make this more readable. For example, don't have an upper case method name. The naming standards are that your method name start with a lower case letter.
then, you are passing around this variable "kaspomat" that is always null. as you call
That further confuses things.
then, the return value from the "Kaspomat" method is never read.
then, the parameter you are looking in "radios" is not the name of any control from the form. On the form, the radio buttons are named "kaspomat"
also, avoid using "l" (lower case L) and "1" (the number "one") in names like you do. In a lot of fonts, these look the same and when looking back over your code that can confuse.
this:


looks like:


but in first one, I used a "one" and in second one I used a lower case L
 
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




Why did you CHECKED all the three check-boxes by default?
 
alona ariel
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I WILL TRY AGAIN TO WRITE MY CODE , HOPLY IT WILL BE MORE CLEARLY.

The jsp code is:

1..<%@ page language="java" contentType="text/html; charset=windows-1255"
2.. pageEncoding="windows-1255"%>
3..<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4..<html>
5..<head>
6..<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
7.. <title>Insert title here</title>
8--</head>
9--<body>
10--

welcome to our bank



11--<form action=TestUser3 METHOD="post">


12--<input type="radio" name="kaspomat" value="a1" CHECKED>

13--radio button 1
14--

15--

16--<input type="radio" name="kaspomat" value="a10" >

17--radio button 2
18--

19--

20--<input type="radio" name="kaspomat" value="a20" >

21--radio button 3
22--

23--

24--<input type="submit" VALUE="Submit" >

25--

26--


27--td> what your choise </td>


and the servlet is:

1.package alona;

2.import java.io.IOException;

3.import valueObjects.User;


4.* Servlet implementation class TestUser3

5.public class TestUser3 extends HttpServlet {
6.


7. private static final long serialVersionUID = 1L;
8. private Object user;



9. // TODO Auto-generated constructor stub



10. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
11. public void doGet(HttpServletRequest request, HttpServletResponse response)throws javax.servlet.ServletException,
12. java.io.IOException {
13. // TODO Auto-generated method stub
14. // try {
15. // super.doGet(request, response);
16. // } catch (ServletException e) {
// TODO Auto-generated catch block
17. // e.printStackTrace();
18. // } catch (IOException e) {
// TODO Auto-generated catch block
19. // e.printStackTrace();
20. // doGet( request, response);
}
21.


22.// }

/**
23. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
24. */
25. protected void doPost(HttpServletRequest request1, HttpServletResponse response1) throws ServletException, IOException {{
26. // TODO Auto-generated method stub

27. // try {
28. // super.doPost(request1, response1);
29. // } catch (ServletException e) {
// TODO Auto-generated catch block
30. // e.printStackTrace();
31. // catch (IOException e) {
// TODO Auto-generated catch block
32. // e.printStackTrace();
33. }

34. // TODO Auto-generated catch block







35. doPost1(request1, response1, null, null);

36. // TODO Auto-generated catch block

37. }






38.
void doPost1 (HttpServletRequest request1, HttpServletResponse response1,String username)throws
39.

ServletException,IOException {
40. String kaspomat1=(String)request1.getParameter("radios");




41.
42. try {
if((request1.getParameterValues("radios")) != null) {
43.
44. if(request1.getParameterValues("radios").equals("a1")) {

45. javax.servlet.RequestDispatcher rd = this.getServletContext().getRequestDispatcher
46. ("/indexa1.jsp");

47. rd.forward(request1, response1);



48. }

49. if(request1.getParameterValues("radios").equals("a10")) {
50. response1.sendRedirect("http://www.ynet.il");
51. }
52.
53. if(request1.getParameterValues("radios").equals("a20")) {

54. response1.sendRedirect("http://www.nrg.il");
55. }

56. }
else {
57. javax.servlet.RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/indexwrong.jsp");
58. rd.forward(request1, response1);

59. }
}
60. catch (Exception e) {
61. e.printStackTrace();
62. }
63.
64. }


65.}
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code is even more unreadable than before!

Please read this: UseCodeTags <-- this is a link, click it!

Then read it again!
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


See the problem is not with the button but the way you are calling doPost1 here you are comparing with kaspomat but can you just see that in doPost1 you are passing null so it will always be null , actually you have to loop through String[] kaspomat1=request1.getParameterValues("radios"); and then pass the individual array elements so that correct output is processed..
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

alona ariel wrote:I WILL TRY AGAIN TO WRITE MY CODE , HOPLY IT WILL BE MORE CLEAR.




You are missing the idea of code tags. Bear posted a link (http://faq.javaranch.com/java/UseCodeTags). Perhaps you didn't see it as a link, but you must read it.
 
I am displeased. You are no longer allowed to read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic