Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%
- String promo = request.getParameter("promoid");
- if (promo==null)
- promo="";
- %>
- <form action="PromotionDisplayServlet" method="post">
- Enter promotion id: <input type="text" name="promoid" size="50"
- value="<%=promo %>"/> <input type="submit" value="Submit"/>
- </form>
- package servlet;
- import java.io.IOException;
- import java.util.ArrayList;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import model.*;
- /**
- * Servlet implementation class PromotionDisplayServlet
- */
- @WebServlet("/PromotionDisplayServlet")
- public class PromotionDisplayServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- /**
- * @see HttpServlet#HttpServlet()
- */
- public PromotionDisplayServlet() {
- super();
- // TODO Auto-generated constructor stub
- }
- /**
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- response.getWriter().append("Served at: ").append(request.getContextPath());
- String promoid=request.getParameter("promoid");
- PromotionDB db=new PromotionDB();
- ArrayList<PromotionDisplay>p1=db.promo(promoid);
- request.setAttribute("promodetails",p1);
- RequestDispatcher rd=request.getRequestDispatcher("displayPromotion.jsp");
- rd.forward(request, response);
- }
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- doGet(request, response);
- }
- }
- package model;
- public class PromotionDisplay {
- private String promoid;
- private String flightid;
- private String discountrate;
- private String ecost;
- private String bcost;
- private String fcost;
- private String start;
- private String end;
- public PromotionDisplay() {
- super();
- }
- public PromotionDisplay(String promoid, String flightid, String discountrate, String ecost, String bcost,
- String fcost, String start, String end) {
- super();
- this.promoid = promoid;
- this.flightid = flightid;
- this.discountrate = discountrate;
- this.ecost = ecost;
- this.bcost = bcost;
- this.fcost = fcost;
- this.start = start;
- this.end = end;
- }
- public String getPromoid() {
- return promoid;
- }
- public void setPromoid(String promoid) {
- this.promoid = promoid;
- }
- public String getFlightid() {
- return flightid;
- }
- public void setFlightid(String flightid) {
- this.flightid = flightid;
- }
- public String getDiscountrate() {
- return discountrate;
- }
- public void setDiscountrate(String discountrate) {
- this.discountrate = discountrate;
- }
- public String getEcost() {
- return ecost;
- }
- public void setEcost(String ecost) {
- this.ecost = ecost;
- }
- public String getBcost() {
- return bcost;
- }
- public void setBcost(String bcost) {
- this.bcost = bcost;
- }
- public String getFcost() {
- return fcost;
- }
- public void setFcost(String fcost) {
- this.fcost = fcost;
- }
- public String getStart() {
- return start;
- }
- public void setStart(String start) {
- this.start = start;
- }
- public String getEnd() {
- return end;
- }
- public void setEnd(String end) {
- this.end = end;
- }
- }
- package model;
- import java.sql.*;
- import java.util.ArrayList;
- public ArrayList<PromotionDisplay> promo(String promoid){
- try{
- //Step1: Load JDBC Driver
- Class.forName("com.mysql.jdbc.Driver");
- //Step 2: Define Connection URL
- String connURL ="jdbc:mysql://localhost/assignment?user=root&password=Mousehunt1";
- //Step 3: Establish connection to URL
- Connection conn = DriverManager.getConnection(connURL);
- 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=?";
- PreparedStatement pstmt = conn.prepareStatement(sqlStr);
- pstmt.setString(1,"%"+promoid+"%");
- ResultSet rs = pstmt.executeQuery();
- ArrayList <PromotionDisplay> p1= new ArrayList <PromotionDisplay>();
- while (rs.next()){
- promoid=rs.getString("PromoID");
- String flightid=rs.getString("FlightID");
- String discountrate=rs.getString("DiscountRate");
- String ecost=rs.getString("Ecost");
- String bcost=rs.getString("Bcost");
- String fcost=rs.getString("FCcost");
- String start=rs.getString("ValidityStartDate");
- String end=rs.getString("ValidityEndDate");
- PromotionDisplay prodip= new PromotionDisplay(promoid,flightid,discountrate,ecost,bcost,fcost,start,end);
- p1.add(prodip);
- }
- conn.close();
- return p1;
- }catch (Exception e){
- System.out.println(e);
- return null;
- }
- }
- <%
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <%@page import="java.sql.*,java.util.*,model.*"%>
- ArrayList<PromotionDisplay>p1=(ArrayList<PromotionDisplay>)request.getAttribute("promodetails");
- if (p1!=null){
- %>
- <table border = '2'>
- <tr bgcolor='white'>
- <td width='300'>Promotion ID</td>
- <td width='300'>Flight ID</td>
- <td width='500'>Discount Rate</td>
- <td width='700'>Discounted Economic Cost</td>
- <td width='700'>Discounted Business Cost</td>
- <td width='700'>Discounted First Class Cost</td>
- <td width='600'>Validity Start Date</td>
- <td width='600'>Validity End Date</td>
- </tr>
- <%
- for (PromotionDisplay prodip:p1) {
- String promoid=prodip.getPromoid();
- String flightid=prodip.getFlightid();
- String discountrate=prodip.getDiscountrate();
- String ecost=prodip.getEcost();
- String bcost=prodip.getBcost();
- String fcost=prodip.getFcost();
- String start=prodip.getStart();
- String end=prodip.getEnd();
- out.println("<tr>");
- out.println("<td width='100'>"+promoid + "</td>");
- out.println("<td width='100'>"+flightid + "</td>");
- out.println("<td width='100'>"+discountrate + "</td>");
- out.println("<td width='100'>"+(double)Integer.parseInt(ecost)* (100-Integer.parseInt(discountrate)) /100+"0"+" SGD" + "</td>");
- out.println("<td width='100'>"+(double)Integer.parseInt(bcost)* (100-Integer.parseInt(discountrate)) /100+"0"+" SGD" + "</td>");
- out.println("<td width='100'>"+(double)Integer.parseInt(fcost)* (100-Integer.parseInt(discountrate)) /100+"0"+" SGD" + "</td>");
- out.println("<td width='100'>"+start + "</td>");
- out.println("<td width='100'>"+end + "</td>");
- out.println("<td width='50'>");
- out.println("</form>");
- out.println("</td>");
- %>
- <tr>
- <%
- }
- %>
- </table>
- <%
- }
- %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement