//All Rights Reserved
1. import java.io.*; 2. import javax.servlet.*; 3. import javax.servlet.http.*; 4. import java.util.*;
5. import java.sql.*;
6. public class ShopingCart extends HttpServlet 7. { 8. String[] cuno; 9. Connection con; 10. String url,mg=""; 11. String cname=""; 12. String ccellno=""; 13. String cadd1="",cadd2="",cadd3="",ccity=""; 14. String cmail=""; 15. String custidstr=""; 16. String cnew=""; 17. String csend="",temp=""; 18. String s1,s2,s3,s4,s5,ino1,max1,s6,s7,s8,ii,s9; 19. ResultSet rs,rs1,rs2,rs3,rs4,rs5,rs6,rs7,rs8,rs9,rs10,rs11; 20. Statement stmt,stmt1,stmt2,stmt3,stmt4,stmt5,stmt6,stmt8; 21. int c1=0,c11,cl1,ino=0,ino2,custidint=0;
22. public void init(ServletConfig sc) throws ServletException{ 23. /* CONNECTING TO THE DATABASE */
24. try{ 25. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 26. con = DriverManager.getConnection("jdbc:odbc:shop"); 27. stmt = con.createStatement(); 28. stmt1= con.createStatement(); 29. }catch(Exception e){System.out.println(e);} 30. }
31. public void doGet(HttpServletRequest request,HttpServletResponse response) throws
32. ServletException,IOException 33. {
34. /* DESIGN A FORM FOR DISPLAYING DATA */ 35. PrintWriter out = response.getWriter();
36. /* Get Identity of the Customer and Parse it */ 37. custidstr = request.getParameter("CustId");
38. custidint = Integer.parseInt(custidstr); 39. /* Set Content-type Header */ 40. response.setContentType("text/vnd.wap.wml"); 41. /* Write the Response */ 42. out.println("<?xml version=\"1.0\"?>");
43. out.println("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML 1.1//EN\" 44. \"http://www.wapforum.org/DTD/wml_1.1.xml\">");
45. out.println("<wml>"); 46. out.println("<template>");
47. out.println("<do type='prev' label='Back'>"); 48. out.println("<prev/>");
49. out.println("</do>"); 50. out.println("</template>"); 51. try{
52. /* Information on the Customer From the Database */ 53. rs=stmt1.executeQuery("select CustomerID from CustomerDetails here 54. CustomerID="+custidint); 55. while(rs.next()) 56. { 57. ino =rs.getInt(1); 58. } 59. rs.close(); 60. if(custidint == ino) 61. { 62. try{
63. /* Based on the CustId Retrieve the Details from Database */ 64. rs1 = stmt.executeQuery("select
65. Groceries,Acessories,Cosmetics,FashionWear,NewProduct from CustomerDetails where 66. CustomerId="+custidint); 67. if(rs1.next()) 68. { 69. s1 = rs1.getString(1); 70. s2 = rs1.getString(2); 71. s3 = rs1.getString(3); 72. s4 = rs1.getString(4); 73. s9 = rs1.getString(5); 74. System.out.println("S1 :"+s1+" s2 :"+s2+" S3 :"+s3+" S4:"+s4+"S9 :"+s9); 75. } 76. rs1.close(); 77. } 78. catch(Exception e1){System.out.println(e1.toString());} 79. /* Print out Shopping Cart info for the User to Order */ 80. out.println("<card id='card1' title='ShoppingCart'>"); 81. out.println("<p>");
82. //if starts
83. if(s1.equals("Yes")) 84. {
85. System.out.println("S1 ");
86. /* Anchor for Navigation to Groceries Card */ 87. out.println("<a href='#groc'>Groceries</a><br/>"); 88. }
89. if(s2.equals("Yes")) 90. {
91. System.out.println("S2");
92. /* Anchor for Navigation to Accessories Card */ 93. out.println("<a href='#aces'>Accessories</a><br/>"); 94. } 95. if(s3.equals("Yes")) 96. { 97. System.out.println("S3"); 98. out.println("<a href='#cosm'>Cosmetics</a><br/>"); 99. } 100. if(s4.equals("Yes")) 101. { 102. System.out.println("S4"); 103. out.println("<a href='#fash'>FashionWear</a><br/>"); 104. } 105. if(s9.equals("Yes")) 106. {
107. System.out.println("S5");
108. out.println("<a href='#new'>New Products</a><br/>"); 109. }
110. out.println("<anchor title='Link'>Order");
111. /* Link for Navigation to Order Servlet */
112. out.println("<go href='http://localhost:8080/Shopping/servlet/Order' method='get'
113. >");
114. /* Posting Parameters to Order Servlet */
115. out.println("<postfield name='CustId' value='"+custidint+"' />"); 116. /* Check If User has selected the Item in the Registration Form */ 117. if(s1.equals("Yes"))
118. { 119. try 120. {
121. rs6 = stmt.executeQuery("select * from Groceries"); 122. while(rs6.next())
123. {
124. /*Retrieving First Column From the Database */ 125. s8 = rs6.getString(1);
126. /* Set the Parameter Name and Value */
127. out.println("<postfield name='"+s8+"' value='$("+s8+")'/>"); 128. } 129. rs6.close(); 130. } 131. catch(Exception e11){System.out.println(e11.toString());} 132. } 133. if(s2.equals("Yes")) 134. { 135. try 136. {
137. rs7 = stmt.executeQuery("select * from Acessories"); 138. while(rs7.next())
139. {
140. s8 = rs7.getString(1);
141. out.println("<postfield name='"+s8+"' value='$("+s8+")'/>"); 142. } 143. rs7.close(); 144. } 145. catch(Exception e12){System.out.println(e12.toString());} 146. } 147. if(s3.equals("Yes")) 148. { 149. try 150. {
151. rs8 = stmt.executeQuery("select * from Cosmetics"); 152. while(rs8.next())
153. {
154. s8 = rs8.getString(1);
155. out.println("<postfield name='"+s8+"' value='$("+s8+")'/>"); 156. } 157. rs8.close(); 158. } 159. catch(Exception e13){System.out.println(e13.toString());} 160. } 161. if(s4.equals("Yes"))
162. { 163. try 164. {
165. rs9 = stmt.executeQuery("select * from FashionWear"); 166. while(rs9.next())
167. {
168. s8 = rs9.getString(1);
169. out.println("<postfield name='"+s8+"' value='$("+s8+")'/>"); 170. } 171. rs9.close(); 172. } 173. catch(Exception e14){System.out.println(e14.toString());} 174. } 175. if(s9.equals("Yes")) 176. { 177. try 178. {
179. rs11 = stmt.executeQuery("select * from newproduct"); 180. while(rs11.next())
181. {
182. s8 = rs11.getString(2);
183. out.println("<postfield name='"+s8+"' value='$("+s8+")'/>"); 184. } 185. rs11.close(); 186. } 187. catch(Exception e14){System.out.println(e14.toString());} 188. } 189. out.println("</go></anchor>"); 190. out.println("</p>"); 191. out.println("</card>");
192. /* Write the Response to the Groceries Card if User Registers for item */
193. if(s1.equals("Yes")) 194. {
195. out.println("<card id='groc' title='Groceries'>"); 196. out.println("<p>");
197. //out.println("Groceries Here"); 198. try
199. {
200. rs2 = stmt.executeQuery("select * from Groceries"); 201. out.println("Item Name Rate Qty");
202. while(rs2.next()) 203. { 204. s7 = rs2.getString(1); 205. s5 = rs2.getString(2); 206. s6 = rs2.getString(3); 207. out.print(s5+" "+s6);
208. out.println("<input name='"+s7+"' type='text' value='0' />"); 209. out.println("<br/>"); 210. } 211. rs2.close(); 212. } 213. catch(Exception e3){System.out.println(e3.toString());} 214. out.println("</p>"); 215. out.println("</card>"); 216. }
217. /* Write the Response to the Accessories Card if User Registers for item */
218. if(s2.equals("Yes")) 219. {
220. out.println("<card id='aces' title='Accessories'>"); 221. out.println("<p>");
222. //out.println("Acessories Here"); 223. try
224. {
225. rs3 = stmt.executeQuery("select * from Acessories"); 226. out.println("Item Name Rate Qty");
227. while(rs3.next()) 228. { 229. s7 = rs3.getString(1); 230. s5 = rs3.getString(2); 231. s6 = rs3.getString(3); 232. out.print(s5+" "+s6);
233. out.println("<input name='"+s7+"' type='text' value='0' />"); 234. out.println("<br/>"); 235. } 236. rs3.close(); 237. } 238. catch(Exception e4){System.out.println(e4.toString());} 239. out.println("</p>"); 240. out.println("</card>"); 241. }
242. /* Write the Response to the Cosmetics Card if User Registers for item */
243. if(s3.equals("Yes")) 244. {
245. out.println("<card id='cosm' title='Cosmetics'>"); 246. out.println("<p>");
247. //out.println("Cosmetics Here"); 248. try
249. {
250. rs4 = stmt.executeQuery("select * from Cosmetics"); 251. out.println("Item Name Rate Qty");
252. while(rs4.next()) 253. { 254. s7 = rs4.getString(1); 255. s5 = rs4.getString(2); 256. s6 = rs4.getString(3); 257. out.print(s5+" "+s6);
258. out.println("<input name='"+s7+"' type='text' value='0' />"); 259. out.println("<br/>"); 260. } 261. rs4.close(); 262. } 263. catch(Exception e5){System.out.println(e5.toString());} 264. out.println("</p>"); 265. out.println("</card>"); 266. }
267. /* Write the Response to the FashionWear Card if User Registers for item */ if(s4.equals("Yes"))
268. {
270. out.println("<p>");
271. //out.println("FashionWear Here"); 272. try
273. {
274. rs5 = stmt.executeQuery("select * from FashionWear"); 275. out.println("Item Name Rate Qty");
276. while(rs5.next()) 277. { 278. s7 = rs5.getString(1); 279. s5 = rs5.getString(2); 280. s6 = rs5.getString(3); 281. out.print(s5+" "+s6);
282. out.println("<input name='"+s7+"' type='text' value='0' />"); 283. out.println("<br/>"); 284. } 285. rs5.close(); 286. } 287. catch(Exception e6){System.out.println(e6.toString());} 288. out.println("</p>"); 289. out.println("</card>"); 290. }
291. /* Write the Response to the New Products Card if User Registers for item */
292. if(s9.equals("Yes")) 293. {
294. out.println("<card id='new' title='New Products'>"); 295. out.println("<p>");
296. //out.println("FashionWear Here"); 297. try
298. {
299. rs10 = stmt.executeQuery("select * from newproduct order by category"); 301. //out.println("Item Name Rate Qty");
302 String temp1 = "",temp=""; 303. boolean flag=false; 304. int i=0; 305. while(rs10.next()) 306. { 307. temp = rs10.getString(1); 308. if(flag) 309. { 310. if(!temp.equals(temp1)) 311. { 312. i=0; 313. } 314. } 315. if(i == 0) 316. { 317. temp1=temp; 318. out.println(temp); 319. out.println("<br/>");
320. out.println("Item Name Rate Qty"); 321. out.println("<br/>");
322. i++; 323. }
324. s7 = rs10.getString(2); 325. s5 = rs10.getString(3);
326. s6 = rs10.getString(4); 327. out.print(s5+" "+s6);
328. out.println("<input name='"+s7+"' type='text' value='0' />"); 329. out.println("<br/>"); 330. flag = true; 331. } 332. rs10.close(); 333. } 334. catch(Exception e6){System.out.println(e6.toString());} 335. out.println("</p>"); 336. out.println("</card>"); 337. } 338. out.println("</wml>"); 339. } 340. //if ends 341. else 342. {
343. out.println("<card id='card1' title='Sorry'>"); 344. out.println("<p>");
345. out.println("Your ID Is Not Valid."); 346. out.println("</p>"); 347. out.println("</card>"); 348. out.println("</wml>"); 349. } 350. }catch(Exception e1){System.out.println(e1.toString());} 351. }
352. public void doPost(HttpServletRequest request,
353. HttpServletResponse response) throws ServletException,IOException 354. { doGet(request,response); }
355. }
Code Description
♦ Lines 1–5: As usual, we import the necessary class libraries.
♦ Lines 6–21: This code is for declaration the class and initialization of variables. All the fields in the registration form are given variable names, and also some temporary variables are declared.
♦ Lines 22–30: This code connects to the database using JDBC-ODBC connectivity. For exception handling, the code is kept in a try-catch block.
♦ Lines 31–40: A WML card is created in this code to get the customer ID from the handset. At line 40, the content type is set to WML.
♦ Lines 42–50: The customer ID obtained from the user is verified in the database. If the customer ID is valid, only the following code is executed. Otherwise, a message saying that the ID is not valid is sent to the handset as in the code in Lines 321–329.
♦ Lines 51–78: Based on customer ID, this code retrieves the details (items selected) from the database.
♦ Lines 80–104: Based on the items selected, a WML card containing the shopping cart will be generated. Line 71 generates the card title. Lines 74–79 generated the code for groceries only if groceries are selected by the user. Lines 80–85 are for accessories; Lines 86–90 are for cosmetics; Lines 91–95 are for fashion wear; Lines 96–100 are for new products.
♦ Lines 105–115: If the user selects groceries when the code given in Lines 80-104 is executed in the handset, the shopping cart has to display the item name, rate, and quantity fields that can be input by the user. When the user inputs the data, it has to be stored in the database through another servlet
called Order. The items selected in the shopping cart selected by the user will be posted in the database using this code.
♦ Lines 117–132: Here, the process of displaying the item name, rate, and quantity fields that can be input by the user is performed for groceries.
♦ Lines 133–146: The above process (displaying the item name, rate and quantity fields) is repeated for accessories.
♦ Lines 147–160: The above process is repeated for cosmetics.
♦ Lines 161–174: This code corresponds to the above process for fashionwear.
♦ Lines 175–191: This code corresponds to the same process for new products.
♦ Lines 193–216: If user selects groceries, the WML card is generated along with fields for item name, rate, and quantity.
♦ Lines 218–241: If the user selects accessories in the shopping cart, this card is generated with fields for item name, rate, and quantity.
♦ Lines 243–266: The process of card generation is repeated for cosmetics.
♦ Lines 268–290: The card generation process is repeated for fashionwear.
♦ Lines 292–339: The card generation process is repeated for new products.
♦ Lines 341–351: If the customer ID is not valid, the message “Sorry, Your ID Is Not Valid” is generated using this code.
♦ Lines 352–355: All the inputs will be posted in the database, and the recordset will be closed. The above program obtains the information from the user — the information is the items of interest and the quantity the user would like to procure. This has to be posted in the database for later processing of the order. Listing 6-7 gives the Java program for saving the customer order in the database.