2017-03-01 71 views
0

這是我第一次嘗試這種應用我有從數據庫中發送數據使用javaservlet

<html> 
<head> 
<title>Flight booking</title> 
<meta charset="utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" 
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 

<link rel="stylesheet" href="css/style.css" type="text/css" 
    media="screen">`enter code here` 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Welcome <%=session.getAttribute("fname")%> ! 
</title> 
</head> 
<body> 
    <div class="container"> 
     <div class="panel panel-default"> 
      <div class="panel body" id="inner_panel_1"> 
       <h4> 
        Welcome , 
        <%=session.getAttribute("fname")%></h4> 
      </div> 
      <!--end panel 1 --> 

      <div class="panel body" id="inner_panel_2"> 
      <form action="CustomerServlet" method="post"> 
       <label for="viewClientInfo">View/Change 
        Client Information</label> 
       <input type="submit" name="viewClientInfo" id="viewClientInfo" class="btn btn-success" 
        data-toggle="collapse" data-target="#clientPanel"> 
      </form> 

      <!-- end panel body --> 
      <div id="clientPanel" class="collapse"> 

       <div 
        class="col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xs-offset-0 col-sm-offset-0 col-md-offset-3 col-lg-offset-3 toppad"> 


        <div class="panel panel-info"> 
         <div class="panel-heading"> 
          <h3 class="panel-title"></h3> 

           <td> <%=session.getAttribute("fname")%> </td> 
           <td> <%=session.getAttribute("lname")%> </td> 

         </div> 
         <div class="panel-body"> 
          <div class="row"> 
           <div class="col-md-3 col-lg-3 " align="center"> 
            <img alt="User Pic" 
             src="http://icons.iconarchive.com/icons/icons8/windows-8/128/Transport-Airplane-Takeoff-icon.png" 
             class="img-circle img-responsive"> 
           </div> 

           <div class=" col-md-9 col-lg-9 "> 
            <table class="table table-user-information"> 
             <tbody> 
              <tr> 
               <td>First Name :</td> 
               <td><%=session.getAttribute("firstname")%></td> 
              </tr> 
              <tr> 
               <td>Last Name :</td> 
               <td><%=session.getAttribute("lastname")%></td> 
              </tr> 
              <tr> 
               <td>Apt Number:</td> 
               <td><%=session.getAttribute("aptNumber")%></td> 
              </tr> 
              <tr> 
               <td>Address</td> 
               <td><%=session.getAttribute("streetNum")%> <%=session.getAttribute("streetName")%> 
               </td> 
              </tr> 
              <tr> 
               <td>City, Country</td> 
               <td><%=session.getAttribute("city")%>, <%=session.getAttribute("country")%> 
               </td> 
              </tr> 
              <tr> 
               <td>Email</td> 
               <td><%=session.getAttribute("userEmail")%></td> 
              </tr> 
              <tr> 
              <td>Phone Number</td> 
              <td><%=session.getAttribute("phoneNum")%><br></td> 

              </tr> 

             </tbody> 
            </table> 

            <button type="button" class="btn btn-success" id="changePayment" data-toggle="collapse" data-target="#payment-panel">Change Payment Information</button> 
           </div> 
          </div> 
         </div> 
        </div> 
       </div> 
       <!--end panel 2 --> 
     </div> 

      <div id="payment-panel" class="collapse"> 
       <div class="col-xs-12 col-md-4"> 
      <div class="panel panel-default"> 
       <div class="panel-heading"> 
        <h3 class="panel-title"> 
         Payment Details 
        </h3> 
       </div> 
       <div class="panel-body"> 
        <form role="form" class="creditCard" action="CreditCardServlet" method="addCreditCard"> 
        <div class="form-group"> 
         <label for="cardNumber"> 
          CARD NUMBER</label> 
         <div class="input-group"> 
          <input type="text" class="form-control" id="cardNumber" placeholder="Valid Card Number" 
           required autofocus /> 
          <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span> 
         </div> 
        </div> 
        <div class="row"> 
         <div class="col-xs-7 col-md-7"> 
          <div class="form-group"> 
           <label for="expiry"> 
            EXPIRY DATE</label> 
           <div class="col-xs-6 col-lg-6 pl-ziro"> 
            <input type="text" class="form-control" id="expiry" placeholder="MM/YYYY" required /> 
           </div> 
          </div> 
         </div> 
         <div class="col-xs-5 col-md-5 pull-right"> 
          <div class="form-group"> 
           <label for="cvv"> 
            CV CODE</label> 
           <input type="password" class="form-control" id="cvv" placeholder="CV" required /> 
          </div> 
         </div> 
        </div> 
        </form> 
        <div class="col-md-4 text-center"> 
        <button type="button" class="btn btn-success" id="submitPaymentInfo" >Submit</button> 

        </div> 

       </div> 
      </div> 
      </div> 
      </div> 
      </div> 
      <!-- end inner panel --> 
      <div class="panel body" id="inner_panel_3"> 
       <button type="button" class="btn btn-success" id="viewBookings" data-toggle="collapse" data-target="#client-bookings-panel">View 
        Bookings</button> 

      </div> 

      <div id="client-bookings-panel" class="collapse"> 
       <p>Client Bookings Dump for next Version</p> 
      </div> 
      <!--end panel 3 --> 

      <div class="panel body" id="inner_panel_4"> 
       <button type="button" class="btn btn-success" id="bookFlightButton" data-toggle="collapse" data-target="#flight-booker-panel">Book 
        Flight for Next Version</button> 

      </div> 

      <div id="flight-booker-panel" class="collapse">Link to Flight Booker</div> 
      <!--end panel 4 --> 

     </div> 
    </div> 
    <!--end container --> 

</body> 
<script 
    src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
<script 
    src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
</html> 

我試圖讓剛剛從DB登錄的用戶的信息的JSP文件中的jsp通過使用上述顯示的JSP給他們

public class UserInfoServlet { 

    public void doGet(HttpServletRequest request , HttpServletResponse response)throws ServletException{ 


    } 
} 

我知道如何從JSP到Java獲取信息,並將其保存到數據庫,但我不知道如何去oposite方向

+0

所以你知道如何連接到數據庫並從數據庫中檢索信息? –

+0

我知道如何連接到數據庫我只是不知道從數據庫獲得信息後,我怎麼可以將這些信息分配給正確的jsp的地方持有人 – Amin

回答

0

呦ü將需要連接到數據庫,然後設置在這樣的會議信息:

public class DatabaseAccess extends HttpServlet{ 

public void doGet(HttpServletRequest request, 
       HttpServletResponse response) 
     throws ServletException, IOException 
{ 
    //you can use your own connection thing in you don't like the below code 
    static final String JDBC_DRIVER="com.mysql.jdbc.Driver"; 
    static final String DB_URL="jdbc:mysql://localhost/TEST"; 


    static final String USER = "root"; 
    static final String PASS = "password"; 

    try{ 

    Class.forName("com.mysql.jdbc.Driver"); 


    Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); 

    // Execute SQL query 
    Statement stmt = conn.createStatement(); 
    String sql; 
    sql = "YOUR QUERY. YOU SAID YOU ALREADY KNOW THIS"; 
    ResultSet rs = stmt.executeQuery(sql); 

    // Extract data from result set 
    while(rs.next()){ 
     //Retrieve by column name 
     String fname = rs.getString("first"); 
     String flast = rs.getString("last"); 
    } 

    rs.close(); 
    stmt.close(); 
    conn.close(); 
    }catch(SQLException se){   
    se.printStackTrace(); 
    }catch(Exception e){ 
    e.printStackTrace(); 
    }finally{   
    try{ 
     if(stmt!=null) 
      stmt.close(); 
    }catch(SQLException se2){ 
    } 
    try{ 
     if(conn!=null) 
     conn.close(); 
    }catch(SQLException se){ 
     se.printStackTrace(); 
    } 
    } 

//get the session and set the values 
HttpSession session = request.getSession();  
session.setAttribute("fname", fname); 
RequestDispatcher rd=req.getRequestDispatcher("/whateveristhejspname.jsp"); 
rd.forward(req, resp); 

} 
} 

確保正確有您的映射設置在web.xml文件

<servlet> 
<servlet-name>DatabaseAccess</servlet-name> 
<servlet-class>DatabaseAccess</servlet-class> 
</servlet> 

<servlet-mapping> 
<servlet-name>DatabaseAccess</servlet-name> 
<url-pattern>/DatabaseAccess</url-pattern> 
</servlet-mapping> 
+0

謝謝大家在尋求幫助後的幫助例如我能夠解決一些變化的問題 – Amin

0
public void doGet(HttpServletRequest request , HttpServletResponse response)throws ServletException { 
    HttpSession session = request.getSession(); 
    //todo getSomeThing from DB 
    session.setAttribute("userInfo","yourMessage"); 
    response.sendRedirect("your Page Location"); 
} 

也許你想要這個