This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

My servlet doesn't get params

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
I try to call a servlet and to pass parameters to it. My form is very simple:
<form action="HelloWorld" method="get">
<table>
<tr>
<td><label>name</label></td>
<td><input type="text" id="txtName"></input></td>
</tr>
<tr>
<td><label>family</label></td>
<td><input type="text" id="txtfamily"></input></td>
</tr>
</table>

<input type="submit"></input>
</form>

And in my servlet I have inside the doGet method:
String name = request.getParameter("name");
That gets null.

I tried also to add a method of doPost, and to write the same code in it, but I still get NULL.

Any idea ?
Thanks.
 
Sheriff
Posts: 67752
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're missing the "name" attributes of the <input> tags - a "label" is something different.
 
Bear Bibeault
Sheriff
Posts: 67752
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 results should be no surprise because you don't have an input element with the name name. When you ask for the value of a request parameter that doesn't exist, you get null.

P.S. You ought to avoid using tables for formatting.
 
Ranch Hand
Posts: 493
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

moshi cochem wrote:Hi.
I try to call a servlet and to pass parameters to it. My form is very simple:
<form action="HelloWorld" method="get">
<table>
<tr>
<td><label>name</label></td>
<td><input type="text" id="txtName"></input></td>
</tr>
<tr>
<td><label>family</label></td>
<td><input type="text" id="txtfamily"></input></td>
</tr>
</table>

<input type="submit"></input>
</form>

And in my servlet I have inside the doGet method:
String name = request.getParameter("name");
That gets null.

I tried also to add a method of doPost, and to write the same code in it, but I still get NULL.

Any idea ?
Thanks.



Ya As Bear and Ulf said , there is nothing named "name" as you mentioned in your code to get the value of it , your code must looks like this :

Hope this is helpful ..
 
moshi cochem
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
thanks for all the replies.
So I understand that the request object works with name and not with id, right ?
I'll try it.
And another thing, why should't I use tables ? Should I use DIV instead ? I'll be glad to hear why.
Thanks a lot!
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

moshi cochem wrote:Hi,
thanks for all the replies.
So I understand that the request object works with name and not with id, right ?
I'll try it.
And another thing, why should't I use tables ? Should I use DIV instead ? I'll be glad to hear why.
Thanks a lot!


There is no relation with table/DIV with servlet. You can use DIV also...

 
Ranch Hand
Posts: 46
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The choice between the table and div would come as part of a choice for the design of the web.

div would call for some use of styling on your part where as table can be used to layout as effectively. Also, for form parameters, name would be the obvious, and I think the only, choice. The id attribute would be used for identifying it as part of the DOM, if i am not wrong.

Max
 
Bear Bibeault
Sheriff
Posts: 67752
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
Using tables for formatting is a pre-CSS dinosaur. Use CSS for layout, tables for tables.
 
The only thing that kept the leeches off of me was this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic