Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet getParameter

 
Antonis Papadakis
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
 
Prabhu Venkatachalam
Ranch Hand
Posts: 502
Java jQuery Spring
  • 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>
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
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
Java jQuery Spring
  • 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.
 
Rahul Bhattacharjee
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.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic