This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help!!! Trying to Run Simple Servlet Example.....  RSS feed

 
sam patel
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I kind of new to this....
I am using JBuilder 7 to do this project...
I am displaying four textboxes and a submit button on the browser...
When i click the submit, i want to call the POST method in my class and print out the Parameter Name and Parameter Value....
I don't know if i am doing it right or not, but if someone can see how can i get the Parameter name and Value when i submit the button...
I am attaching the code...
Any help would be greatly appreciated...
============================================================
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author unascribed
* @version 1.0
*/
public class Servlet1 extends HttpServlet {
static final private String CONTENT_TYPE = "text/html";
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println(createSearchForm());
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse res)
throws IOException, ServletException
{
String xParameterName = null;
String xParameterValue = null;
Enumeration e = request.getParameterNames();
while (e.hasMoreElements()) {
xParameterName = (String) e.nextElement();
if((xParameterName.compareTo("firstName")==0)){
xParameterValue = request.getParameterValues(xParameterName)[0];
PrintWriter out = res.getWriter();
System.out.println(xParameterName);
System.out.println(xParameterValue);
}
}
}
//Clean up resources
public void destroy() {
}
public String createSearchForm(){
StringBuffer sb = new StringBuffer();
sb.append("<form action=\"Servlet1\" method=POST >");
sb.append("\n");
sb.append("<p align=\"center\">");
sb.append("\n");
sb.append("First Name:");
sb.append("\n");
sb.append("<input name=\"firstnameTextfield\" type=\"text\" size=\"20\" maxlength=\"20\">");
sb.append("\n");
sb.append("Last Name:");
sb.append("\n");
sb.append("<input name=\"textfield\" type=\"text\" size=\"20\" maxlength=\"20\">");
sb.append("\n");
sb.append("</p>");
sb.append("\n");
sb.append("\n");
sb.append("<br>");
sb.append("\n");
sb.append("<br>");
sb.append("\n");
sb.append("<p align=\"center\">");
sb.append("\n");
sb.append("Policy Number:");
sb.append("\n");
sb.append("<input name=\"textfield2\" type=\"text\" size=\"20\" maxlength=\"20\">");
sb.append("\n");
sb.append("SSN:");
sb.append("\n");
sb.append("<input name=\"textfield3\" type=\"text\" size=\"20\" maxlength=\"9\">");
sb.append("\n");
sb.append("</p>");
sb.append("\n");
sb.append("<br>");
sb.append("\n");
sb.append("<p align=\"center\">");
sb.append("\n");
sb.append("<input type=\"submit\" >");
sb.append("\n");
sb.append("</p>");
sb.append("\n");
sb.append("</form>");
return sb.toString();
}
}
 
Craig Jackson
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing, based on your html input element shouldn't your if condition compare
"firstnameTextfield" instead of "firstname".
What do you think?
craig
 
Geeta Ramasami
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
For displaying the ParamterName and ParamaterValues u can use the following code Snippet instead of checking some conditions.
Enumeration e = request.getParamaterNames();
while(e.hasMoreElements())
{
xParameterName = (String)e.NextElement();
xParameterValue = request.getParameter(xParameterName);
PrintWriter out = res.getWriter();
out.println(xParameterName);
out.println(xParameterValue);
}
Cheers
Geeta
[ March 11, 2003: Message edited by: Geeta Ramasami ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!