Advertisement
Guest User

Untitled

a guest
Aug 3rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. <%
  2. String promo = request.getParameter("promoid");
  3.  
  4. if (promo==null)
  5. promo="";
  6.  
  7. %>
  8. <form action="PromotionDisplayServlet" method="post">
  9. Enter promotion id: <input type="text" name="promoid" size="50"
  10. value="<%=promo %>"/> <input type="submit" value="Submit"/>
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. </form>
  18.  
  19. package servlet;
  20.  
  21. import java.io.IOException;
  22. import java.util.ArrayList;
  23.  
  24. import javax.servlet.RequestDispatcher;
  25. import javax.servlet.ServletException;
  26. import javax.servlet.annotation.WebServlet;
  27. import javax.servlet.http.HttpServlet;
  28. import javax.servlet.http.HttpServletRequest;
  29. import javax.servlet.http.HttpServletResponse;
  30.  
  31. import model.*;
  32.  
  33. /**
  34. * Servlet implementation class PromotionDisplayServlet
  35. */
  36. @WebServlet("/PromotionDisplayServlet")
  37. public class PromotionDisplayServlet extends HttpServlet {
  38. private static final long serialVersionUID = 1L;
  39.  
  40. /**
  41. * @see HttpServlet#HttpServlet()
  42. */
  43. public PromotionDisplayServlet() {
  44. super();
  45. // TODO Auto-generated constructor stub
  46. }
  47.  
  48. /**
  49. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  50. */
  51. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  52. // TODO Auto-generated method stub
  53. response.getWriter().append("Served at: ").append(request.getContextPath());
  54.  
  55. String promoid=request.getParameter("promoid");
  56.  
  57. PromotionDB db=new PromotionDB();
  58.  
  59. ArrayList<PromotionDisplay>p1=db.promo(promoid);
  60.  
  61. request.setAttribute("promodetails",p1);
  62.  
  63. RequestDispatcher rd=request.getRequestDispatcher("displayPromotion.jsp");
  64. rd.forward(request, response);
  65. }
  66.  
  67. /**
  68. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  69. */
  70. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  71. // TODO Auto-generated method stub
  72. doGet(request, response);
  73. }
  74.  
  75. }
  76.  
  77. package model;
  78.  
  79. public class PromotionDisplay {
  80. private String promoid;
  81. private String flightid;
  82. private String discountrate;
  83. private String ecost;
  84. private String bcost;
  85. private String fcost;
  86. private String start;
  87. private String end;
  88. public PromotionDisplay() {
  89. super();
  90. }
  91. public PromotionDisplay(String promoid, String flightid, String discountrate, String ecost, String bcost,
  92. String fcost, String start, String end) {
  93. super();
  94. this.promoid = promoid;
  95. this.flightid = flightid;
  96. this.discountrate = discountrate;
  97. this.ecost = ecost;
  98. this.bcost = bcost;
  99. this.fcost = fcost;
  100. this.start = start;
  101. this.end = end;
  102. }
  103. public String getPromoid() {
  104. return promoid;
  105. }
  106. public void setPromoid(String promoid) {
  107. this.promoid = promoid;
  108. }
  109. public String getFlightid() {
  110. return flightid;
  111. }
  112. public void setFlightid(String flightid) {
  113. this.flightid = flightid;
  114. }
  115. public String getDiscountrate() {
  116. return discountrate;
  117. }
  118. public void setDiscountrate(String discountrate) {
  119. this.discountrate = discountrate;
  120. }
  121. public String getEcost() {
  122. return ecost;
  123. }
  124. public void setEcost(String ecost) {
  125. this.ecost = ecost;
  126. }
  127. public String getBcost() {
  128. return bcost;
  129. }
  130. public void setBcost(String bcost) {
  131. this.bcost = bcost;
  132. }
  133. public String getFcost() {
  134. return fcost;
  135. }
  136. public void setFcost(String fcost) {
  137. this.fcost = fcost;
  138. }
  139. public String getStart() {
  140. return start;
  141. }
  142. public void setStart(String start) {
  143. this.start = start;
  144. }
  145. public String getEnd() {
  146. return end;
  147. }
  148. public void setEnd(String end) {
  149. this.end = end;
  150. }
  151.  
  152.  
  153. }
  154.  
  155. package model;
  156.  
  157. import java.sql.*;
  158. import java.util.ArrayList;
  159.  
  160. public ArrayList<PromotionDisplay> promo(String promoid){
  161. try{
  162. //Step1: Load JDBC Driver
  163. Class.forName("com.mysql.jdbc.Driver");
  164. //Step 2: Define Connection URL
  165. String connURL ="jdbc:mysql://localhost/assignment?user=root&password=Mousehunt1";
  166. //Step 3: Establish connection to URL
  167. Connection conn = DriverManager.getConnection(connURL);
  168. String sqlStr= "Select PromoID,P.FlightID,DiscountRate,Ecost,Bcost,FCcost,ValidityStartDate,ValidityEndDate from flightschedule F,promotion P where P.flightid=F.flightid and P.promoid=?";
  169. PreparedStatement pstmt = conn.prepareStatement(sqlStr);
  170.  
  171. pstmt.setString(1,"%"+promoid+"%");
  172.  
  173.  
  174. ResultSet rs = pstmt.executeQuery();
  175.  
  176. ArrayList <PromotionDisplay> p1= new ArrayList <PromotionDisplay>();
  177.  
  178.  
  179.  
  180. while (rs.next()){
  181.  
  182. promoid=rs.getString("PromoID");
  183. String flightid=rs.getString("FlightID");
  184. String discountrate=rs.getString("DiscountRate");
  185. String ecost=rs.getString("Ecost");
  186. String bcost=rs.getString("Bcost");
  187. String fcost=rs.getString("FCcost");
  188. String start=rs.getString("ValidityStartDate");
  189. String end=rs.getString("ValidityEndDate");
  190.  
  191.  
  192. PromotionDisplay prodip= new PromotionDisplay(promoid,flightid,discountrate,ecost,bcost,fcost,start,end);
  193.  
  194. p1.add(prodip);
  195. }
  196. conn.close();
  197. return p1;
  198. }catch (Exception e){
  199.  
  200. System.out.println(e);
  201. return null;
  202. }
  203.  
  204. }
  205.  
  206. <%
  207. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  208. pageEncoding="ISO-8859-1"%>
  209. <%@page import="java.sql.*,java.util.*,model.*"%>
  210.  
  211. ArrayList<PromotionDisplay>p1=(ArrayList<PromotionDisplay>)request.getAttribute("promodetails");
  212.  
  213. if (p1!=null){
  214. %>
  215. <table border = '2'>
  216. <tr bgcolor='white'>
  217. <td width='300'>Promotion ID</td>
  218. <td width='300'>Flight ID</td>
  219. <td width='500'>Discount Rate</td>
  220. <td width='700'>Discounted Economic Cost</td>
  221. <td width='700'>Discounted Business Cost</td>
  222. <td width='700'>Discounted First Class Cost</td>
  223. <td width='600'>Validity Start Date</td>
  224. <td width='600'>Validity End Date</td>
  225.  
  226. </tr>
  227. <%
  228. for (PromotionDisplay prodip:p1) {
  229. String promoid=prodip.getPromoid();
  230. String flightid=prodip.getFlightid();
  231. String discountrate=prodip.getDiscountrate();
  232. String ecost=prodip.getEcost();
  233. String bcost=prodip.getBcost();
  234. String fcost=prodip.getFcost();
  235. String start=prodip.getStart();
  236. String end=prodip.getEnd();
  237.  
  238. out.println("<tr>");
  239. out.println("<td width='100'>"+promoid + "</td>");
  240. out.println("<td width='100'>"+flightid + "</td>");
  241. out.println("<td width='100'>"+discountrate + "</td>");
  242. out.println("<td width='100'>"+(double)Integer.parseInt(ecost)* (100-Integer.parseInt(discountrate)) /100+"0"+" SGD" + "</td>");
  243. out.println("<td width='100'>"+(double)Integer.parseInt(bcost)* (100-Integer.parseInt(discountrate)) /100+"0"+" SGD" + "</td>");
  244. out.println("<td width='100'>"+(double)Integer.parseInt(fcost)* (100-Integer.parseInt(discountrate)) /100+"0"+" SGD" + "</td>");
  245. out.println("<td width='100'>"+start + "</td>");
  246. out.println("<td width='100'>"+end + "</td>");
  247.  
  248. out.println("<td width='50'>");
  249.  
  250.  
  251.  
  252. out.println("</form>");
  253. out.println("</td>");
  254. %>
  255. <tr>
  256.  
  257. <%
  258. }
  259. %>
  260. </table>
  261. <%
  262.  
  263. }
  264.  
  265. %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement