我有一個數據庫和房子,而HTML頁面使用<SELECT>,
,用戶需要選擇房屋所在的地區。對客戶端有jdbc響應的servlet
的Servlet:
@WebServlet("/post")
public class HosesBaseServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
//choice from html form
String choice = request.getParameter("district");
//Database parameters
final String DB_CONNECTION = "jdbc:mysql://localhost:3306/mydb2";
final String DB_USER = "root";
final String DB_PASSWORD = "root";
Connection conn;
try {
conn = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
System.out.println("Connection available");
PreparedStatement ps = conn.prepareStatement("SELECT Square, RoomNumbers, Price FROM houses WHERE District = " + choice);
}catch (SQLException ex) {
System.out.println("Fail to connect with base");
}
}
}
我怎樣才能把SQL SELECT結果到HTML頁面,並給它回客戶端?
我創建的類衆議院
public class Hosue implements Serializable {
private String price;
private String square;
private String RoomNumbers;
public String getPrice() {
return price;
}
public String getSquare() {
return square;}
public String getRoomNumbers() {
return RoomNumbers;}
public void setPrice(String price) {
this.price = price;
}
public void setSquare(String square) {
this.square = square;
}
public void setRoomNumbers(String roomNumbers) {
RoomNumbers = roomNumbers;
}
}
和房屋
public class Houses {
public List<House> getList() {
}
}
,並添加腳本到我的HTML。下一步,如何將select中的信息添加到此列表中?
你更喜歡前端jsp或純html的技術? –
喜歡純html,因爲我不太明白jsp是如何工作的,當servlet發送給客戶端時,jsp會轉換爲html嗎?也許jsp會更好? – user202822
好的,等我給你解決。如何將房屋列表轉換爲html。 –