• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
please buy my thing and then I'll have more money:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic