2017-01-01 81 views
1

我的網站代碼出現問題。我正在編寫一個網站,當用戶可以在jsp頁面上搜索活動時。數據庫根據其類型,城市,位置和日期包括一些活動。用戶將填寫此信息並按提交按鈕。新的jsp頁面將會打開,名爲search.jsp。用戶只能通過填寫一個字段來搜索,例如只填寫類型並寫'音樂'。他還可以通過填寫「音樂」,「伊斯坦布爾」和「本週」來填充2或3或全部字段進行搜索。按下提交按鈕後。數據將顯示在搜索頁面上。但我不能這樣做。我能怎麼做 ? (PS:在sql中的表名是activities。屬性是id(int主鍵),類型(varchar),描述(varchar),城市(varchar),位置(varchar),日期(varchar),價格(varchar)。 enter image description here 的success.jsp在動態網頁上從數據庫搜索數據

<%-- 
Document : success 
Created on : 19.Ara.2016, 12:41:49 
Author  : BURAK NURÇİÇEK 
--%> 

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
<body background="http://www.teamarking.com/barcode/bar_background.jpg"> 

<marquee><h2 style="color:RED">WELCOME TO ONLINE RESERVATION SYSTEM   </marquee></h2> 

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>JSP Page</title> 

</head> 
<form method = "get" action = "search.jsp"> 
<table align="right"> 
    <tr><td><b><font color='#D18603'>Type :</b></td><td><input type="text" name="type" /></td></tr> 
    <tr><td><b><font color='#D18603'>City :</b></td><td><input type="text" name="city" /></td></tr> 
    <tr><td><b><font color='#D18603'>Location :</b></td><td><input type="text" name="location" /></td></tr> 
    <tr><td><b><font color='#D18603'>Date(YYYY-MM-DD) :</b></td><td><select name='search'><option value='1'>Today</option><option value='2'>Tomorrow</option><option value='3'>This Weekend</option><option value='4'>Next Weekend</option><option value='5'>This week</option><<option value='6'>Next 14 Days</option><option value='7'>Next 30 Days</option></select></b>; 
    <tr><td colspan="2" align="center"><input type="submit" value="submit" /> <input type="reset" value="reset" /></td></tr></td></tr> 
    <% 
     String x = (String) application.getAttribute("id"); 
    //out.println(x); 
    %> 

</table> 
<table> 

</table> 
</form> 
<table align='left'> 
    <ul> 
    <li><% 
     out.println("<a href='music.jsp'>Music</a>"); 
     %> 
    </li> 
    <li><% 
     out.println("<a href='sport.jsp'>Sport</a>"); 
     %></li> 
    <li> <% 
     out.println("<a href='stage.jsp'>Stage</a>"); 
     %></li> 
    <li> <% 
     out.println("<a href='family.jsp'>Family</a>"); 
     %></li> 
    <li> <% 
     out.println("<a href='education.jsp'>Education</a>"); 
     %></li> 
    </ul> 
    <div class="landscape"> 


    <script language="javascript" src="http://is.sitekodlari.com/takvim5.js"></script> 
    <script language="javascript" src="http://is.sitekodlari.com/paylas17.js"></script> 



    <% 
     String username = (String) request.getSession().getAttribute("username"); 
     out.println("<br> Welcome " + username); 

     out.println(
       "<br><a href='logout.jsp'>Log out</a>"); 

     out.println(
       "<br><a href='myreservations.jsp'>My Reservations</a>"); 
    %> 

</table> 
</div> 
</body> 
</html> 

search.jsp的

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<%@ page import ="java.sql.*" %> 




<!DOCTYPE html> 
<html> 
<body background="http://www.teamarking.com/barcode/bar_background.jpg"> 
    <form method="post" action="reservations.jsp"> 

     <head> 
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>Book Ticket</title> 
    </head> 

    <center>  
     <table border="1" width="30%" height="30%"> 
      <th><font color='#D18603'>id</font> 
      <th><font color='#D18603'>Type</font></th> 
      <th><font color='#D18603'>Description</font></th> 
      <th><font color='#D18603'>City</font></th> 
      <th><font color='#D18603'>Location</font></th> 
      <th><font color='#D18603'>Date</font></th> 
      <th><font color='#D18603'>Price</font></th> 
      <th><font color='#D18603'>Buy</font> 





       <% 
        Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance(); 
        Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/users", "users", "123"); 

        Statement st = con.createStatement(); 
        ResultSet rs; 
        /* ERROR IN PARAMETERS */   rs = st.executeQuery("select * from activities where type='"+request.getParameter(type)+"' AND city='"+request.getParameter(city)+"' AND location='"+request.getParameter(location)+"' AND date='"+request.getParameter(date)+"'"); 
        while (rs.next()) { 

         out.println("<tr>"); 
         out.println("<form action='reservations.jsp'>"); 
         out.println("<td>" + rs.getString("id") + "<input type='hidden' name='id' value='" + rs.getString("id") + "'></td>"); 
         out.println("<td>" + rs.getString("type") +"<input type='hidden' name='type' value='" + rs.getString("type") + "'></td>"); 
         out.println("<td>" + rs.getString("description") + "<input type='hidden' name='description' value='" + rs.getString("description") + "'></td>"); 
         out.println("<td>" + rs.getString("city") + "<input type='hidden' name='city' value='" + rs.getString("city") + "'></td>"); 
         out.println("<td>" + rs.getString("location") + "<input type='hidden' name='location' value='" + rs.getString("location") + "'></td>"); 
         out.println("<td>" + rs.getString("date") +"<input type='hidden' name='date' value='" + rs.getString("date") + "'></td>"); 
         out.println("<td>" + rs.getString("price") + "<input type='hidden' name='price' value='" + rs.getString("price") + "'></td>"); 

         out.println("<td><b><form action='reservations.jsp'><select name='buy'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select><input type='submit' value='Submit'></form></b>"); 

         out.println("</tr>"); 

        } 
        st.close(); 

       %> 


       </center> 
     </table> 

     <br> <br><a href='success.jsp'>Back</a> 
     <br><br><a href='logout.jsp'>Log out</a> 
    </form> 
    </body> 
    </html> 

我給這個錯誤。因爲我的SQL代碼是錯誤的,我不能讓這個值enter image description here

+0

將參數的名稱放在雙引號中。 'request.getParameter(「city」)''而不是'request.getParameter(city)' – msk

+0

我做了這個。但它返回search.jsp上的空表 – tripley

+0

在ID參數的search.jsp頁面中使用'rs.getInt'而不是'rs.getString'。 – msk

回答

1

你想使用字符串:

rs = st.executeQuery("select * from activities where type='"+request.getParameter("type")+"' AND city='"+request.getParameter("city")+"' AND location='"+request.getParameter("location")+"' AND date='"+request.getParameter("date")+"'"); 

此外,請使用PreparedStatement(一個理由刪除等SQL注入漏洞)的習慣,在那裏你可以這樣定義:

PreparedStatement ps = con.prepareStatement("select * from activities where type=? AND city=? AND location=? AND date=?"); 

ps.setString(1, request.getParameter("type")); 
ps.setString(2, request.getParameter("city")); 
ps.setString(3, request.getParameter("location")); 
ps.setString(4, request.getParameter("date")); 

rs = ps.executeQuery(); 

最後,內部JSP的Java代碼,不推薦。您應該嘗試將您的項目建模爲MVC表單。

+0

我必須把這個代碼? – tripley

+0

它在搜索jsp中返回空表,當我搜索 – tripley

+0

時,我可以帶上您的社交媒體帳號以便快速通話嗎?我認爲問題在部分日期。 ı不知道如何修復我 – tripley

0

錯誤似乎是JSP頁面本身沒有編譯。你有沒有想過你寫的代碼出了什麼問題。

錯誤說,無法識別變量類型,城市,位置。建議您檢查對象HttpServletRequest採用的方法getParameter作爲參數。

其次,最好的做法是將數據庫查詢移動到Java類中,而不是在JSP頁面中進行查詢。