• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Forms in servlet

 
len oke
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to retrieve the name and quantity and price, these need to be made form fields ,so I want to put 'hidden' types like this in the servlet.

<input type="hidden" name="price" value="4">
<input type="hidden" name="item" value="sock">

How do you code this in my servlet?

servlet part code....

public class MyBasket extends HttpServlet {


private String startWebPage(){
return "<HTML><HEAD><TITLE>My Basket</TITLE></HEAD><BODY>" +
"<HTML><HEAD><TITLE>My Basket</TITLE></HEAD><BODY>" +
"<FORM action='checkout' method='POST'>" +

"<TABLE ALIGN='center' BORDER='1' WIDTH='50%' CELLSPACING='3' CELLPADDING='3'>" +
"<TR><TH ALIGN='center' VALIGN='top'>Item</TH>" +
"<TH ALIGN='center' VALIGN='top'>Unit Price (�)</TH>" +
"<TH ALIGN='center' VALIGN='top'>Quantity</TH></TR>";
}

private String endWebPage(){
return "<TR><TD ALIGN='center' VALIGN='top' COLSPAN=3>" +
"<INPUT TYPE='submit' VALUE='Continue'>" +
"<INPUT TYPE='reset' VALUE='Clear'></TD></TR></TABLE></FORM></BODY></HTML>";
}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

// Get the value of an initialization parameter
// Get the price parameter value

String parm = getServletConfig().getInitParameter("price");
int price = Integer.parseInt(parm);
String parm1 = getServletConfig().getInitParameter("price1");
int price1 = Integer.parseInt(parm1);
String parm2 =getServletConfig() .getInitParameter("price2");
int price2 = Integer.parseInt(parm2);
String parm3 = getServletConfig().getInitParameter("price3");
int price3 = Integer.parseInt(parm3);
String parm4 = getServletConfig().getInitParameter("price4");
int price4 = Integer.parseInt(parm4);

// Get the item parameter value

String item = getServletConfig().getInitParameter("item");
String item1 = getServletConfig().getInitParameter("item1");
String item2 = getServletConfig().getInitParameter("item2");
String item3 = getServletConfig().getInitParameter("item3");
String item4 = getServletConfig().getInitParameter("item4");



// TODO output your page here

out.println( this.startWebPage() );

out.println("<TR><TD>" + item + "</TD><TD>" + price + "</TD>");
out.println("<TD ALIGN='left' VALIGN='top'>" +
"<SELECT NAME='choices'>" +
"<OPTION>0" +
"<OPTION>1" +
"<OPTION>2" +
"<OPTION>3" +
"<OPTION>4" +
"</SELECT></TD></TR>");
out.println("<TR><TD>" + item1 + "</TD><TD>" + price1 + "</TD>");
out.println("<TD ALIGN='left' VALIGN='top'>" +
"<SELECT NAME='choices1'>" +
"<OPTION>0" +
"<OPTION>1" +
"<OPTION>2" +
"<OPTION>0" +
"<OPTION>0" +
"</SELECT></TD></TR>");
out.println("<TR><TD>" + item2 + "</TD><TD>" + price2 + "</TD>");
out.println("<TD ALIGN='left' VALIGN='top'>" +
"<SELECT NAME='choices2'>" +
"<OPTION>0" +
"<OPTION>1" +
"<OPTION>0" +
"<OPTION>0" +
"<OPTION>0" +
"</SELECT></TD></TR>");
out.println("<TR><TD>" + item3 + "</TD><TD>" + price3 + "</TD>");
out.println("<TD ALIGN='left' VALIGN='top'>" +
"<SELECT NAME='choices3'>" +
"<OPTION>0" +
"<OPTION>1" +
"<OPTION>2" +
"<OPTION>0" +
"<OPTION>0" +
"</SELECT></TD></TR>");
out.println("<TR><TD>" + item4 + "</TD><TD>" + price4 + "</TD>");
out.println("<TD ALIGN='left' VALIGN='top'>" +
"<SELECT NAME='choices4'>" +
"<OPTION>0" +
"<OPTION>1" +
"<OPTION>0" +
"<OPTION>0" +
"<OPTION>0" +
"</SELECT></TD></TR>");
out.println(endWebPage());
out.close();
}



/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you generating all that markup in a servlet rather than employing a JSP? That's insane.
 
len oke
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The paramters are coming from web.xml ,and I want to pass the values into my basket servlet.
So I need to know how to code the hidden types into the servlet, like you can do in html?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"gpo",

It looks like you've changed your screen name from something valid to something invalid.
Please go back and make sure it conforms to our JavaRanch Naming Policy
You can do this here
 
dhanush karma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a JSP and get rid of this clutter ...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic