使用javaservlet将数据从数据库发送到jsp

时间:2017-03-01 01:46:46

标签: java database jsp servlets

这是我第一次尝试这种应用程序时我有一个jsp文件

<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>

我正在尝试获取刚刚从数据库登录的用户的信息,并使用上面的jsp将其显示给他们

public class UserInfoServlet {

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


    }
}

我知道如何从jsp获取信息到java并将其保存到db但我不知道如何进入对开方向

2 个答案:

答案 0 :(得分:0)

您需要连接到db,然后在会话中设置信息,如下所示:

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>

答案 1 :(得分:0)

select distinct bb.pk1 as LearnSourceKey,
       sc.pk1 as SISSourceKey,
       row_number () over (partition by bb.pk1 order by sc.SISCourseKey) as RowRank
from Source.SISCourse sc left join
     source.SISTerm st
     on sc.TermSourceKey = st.SISTermKey inner join
     Source.COURSE_MAIN bb
     on bb.batch_uid = (left(CatalogNumber, len(CatalogNumber) - 1) + 
                        (case when st.TermDescription = 'Semester 1' then 'SEM-1'  + '_' + st.AcademicYear end) 
                       );

也许你想要这个