This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Forms in servlet

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
}
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do that in a JSP.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a JSP and get rid of this clutter ...
 
Beware the other head of science - it bites! Nibble on this message:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic