File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes My servlet doesn't get params Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "My servlet doesn Watch "My servlet doesn New topic
Author

My servlet doesn't get params

moshi cochem
Ranch Hand

Joined: Nov 10, 2009
Posts: 91
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.
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50691

Please UseCodeTags.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 32768
You're missing the "name" attributes of the <input> tags - a "label" is something different.


Android appsImageJ pluginsJava web charts
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50691

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.
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 462

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 ..


Thanks,
Sherif
moshi cochem
Ranch Hand

Joined: Nov 10, 2009
Posts: 91
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!
Ravishanker kumar
Ranch Hand

Joined: Jul 20, 2006
Posts: 48
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...

Maximus Moothiringus
Ranch Hand

Joined: Jun 07, 2008
Posts: 46

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


-- Maximus Moothiringus
Preparing for SCJA!!
Finally figured out that giving <br/> splits the profile biography but not the signature
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50691

Using tables for formatting is a pre-CSS dinosaur. Use CSS for layout, tables for tables.
 
 
subject: My servlet doesn't get params
 
Threads others viewed
Getting null values in table even after the input
unable to do validation for select in struts2
The max input fields for servlet can handle
Doubts in jsp
getting the parameters from multipart/form-data
developer file tools