Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Servlet getParameter

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator

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



Yeah its works but not completely.
and finally...
 
Yes, my master! Here is the tiny ad you asked for:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic