wood burning stoves 2.0*
The moose likes Servlets and the fly likes Forms in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Forms in servlet" Watch "Forms in servlet" New topic
Author

Forms in servlet

len oke
Greenhorn

Joined: Mar 19, 2007
Posts: 4
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

Joined: Jan 10, 2002
Posts: 60752
    
  65

Why are you generating all that markup in a servlet rather than employing a JSP? That's insane.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
len oke
Greenhorn

Joined: Mar 19, 2007
Posts: 4
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

Joined: Dec 11, 2004
Posts: 13410

You can do that in a JSP.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"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

Joined: Mar 22, 2007
Posts: 3
Use a JSP and get rid of this clutter ...


<b><a href="http://redhotjava.com/index.php?option=com_linkdirectory&task=viewcat&catid=10&Itemid=48" target="_blank" rel="nofollow">90 SCJP Tests</a></b> <b><a href="http://redhotjava.com/content/view/88/35/index.php?option=com_content&task=category�ionid=4&id=20&Itemid=36" target="_blank" rel="nofollow">Servlet Interview Questions</a></b>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Forms in servlet
 
Similar Threads
Connection Pooling
Servlet Initialization Parameters
My bean doesn't quite work
Passing web.xml info to servlets
inserting data to a table