• 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

Servlet getParameter

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,

I am learing java by myself and I am reading about servlets and I have stuck. I have written the following html

<html>
<head>
<title>Select a Stock</title>
</head>
<body>
<form action="http://localhost:8080/servlet/ServLet1" method="GET">
Enter your name: 
<input type=text value=Name>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Also I have written the java servlet

public class ServLet1 extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
String name = request.getParameter("Name");

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello world!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello world!</h1>");
out.println("<br>");
out.println("<h2>");
out.println("hi 1");
out.println(name);
out.println("</h2>");
out.println("</body>");
out.println("</html>");
}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}

I have install tomcat on windows XP Professional

I call the html with firefox, I type in a name and press the submit button. The html of the servlet is displayed but the getParameter returns NULL. I have checked that the name is correctly written.

Does anybody has an idea why?
Thanks in advance.

Regards

Antonis
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your HTML file,

Change this,

<input type=text value=Name>

to this,

<input type=text name="Name" value=Name>
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Change this,

<input type=text value=Name>

to this,

<input type=text name="Name" value=Name>



Actually, all attributes should be quoted for conformance to the HTML standard and general best results.

<input type="text" name="Name" value="Name">

Bill
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
without quotes also it will work but yes as per standard of mark up language all the attribute's value should be inside quote.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try out :

<input type="text" name="Name"/>

try out getParameter("Name")
at the servlet point.
Why do you need to give a value.Well with value also it should work.
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Prabhu venkatachalam:
without quotes also it will work



Yeah its works but not completely.
and finally...
 
Yeah, but is it art? What do you think 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