我們常常會用到通過圖書的名稱來查詢圖書那么這種話我們也就會使用到從數據庫中搜索出數據而且載入到自己的Jsp頁面中
這種話我們須要將從數據庫中獲取到的數據放進響應中然后通過%=request.getAttribute("bookId1") %獲取對應的值當然僅僅有servlet和jsp頁面是不夠的還須要的是將你的servlet配置到你的wed.xml中。這樣你的servlet才干夠使用。
下面為個人淺淺的想法:事實上我個人認為動態的載入網頁更加的簡單比如:你想要在你的站點每天公布一張站點動態圖片那么你須要做的就是最簡單的方式將你的圖片命名成之前的名稱用以替換然后單獨的一個文件存放文本文件 再將文本文件載入進來另一種更加安全的方法就是你須要將你的文件放進數據庫中然后在每次訪問頁面時候通過servlet載入進來(個人認為這樣的方式盡管安全可是載入的比較慢一般人都不願去等待)另一種就是我記得老師和我說過有一種專用的站點后台管理的假設創建自己的站點能夠考錄是用那個。
中間使用的book是定義的一個book類存儲的是book的基本信息為其設置setter和getter訪問器這種話符合封裝原則
package bookConnUtil;
import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class selectBook extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
String bookId=req.getParameter("bookId");
try {
//本句話調用的是等會數據庫進行增刪改查里的一個static方法可參考我寫的:http://blog.csdn.net/bluezhangfun/article/details/46617455
boolean flag=bookDAO.bookSelete(bookId);
//進行調試的語句
//System.out.println("bookSelect方法正常!!!。。!!
。!!
。!
");
if(flag==true){
//book.getBookID();在以下類中
String bookId1=bookDAO.book.getBookID();String bookName=bookDAO.book.getBookName();
String pianduanName=bookDAO.book.getPianduanName();
//System.out.println(bookId1+"+++++++++++++++++++++++++++");
//將從數據庫中查詢到的語句放入到response中
req.setAttribute("bookId1", bookId1);req.setAttribute("bookName", bookName);
req.setAttribute("pianduanName",pianduanName);
//進行頁面跳轉
req.getRequestDispatcher("selectBook.jsp").forward(req, resp);}else{
req.getRequestDispatcher("error.html").forward(req, resp);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/***************************************************************************************************************************
package bookConnUtil;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//使用rs結果集的時候由於查詢的是一條語句 假設是使用rs.next就是沒有后繼所以就不能運行while循環將會出現錯誤
//假設是查詢的多條語句結果是多條的話那么就能用while循環這樣就能實現反復的輸出
//使用prepareStatement時候必須先要對當中的?號進行賦值不然會出現錯誤的
public class bookDAO{
static books book = new books();
public static books getBook(){
return book;
}
public static boolean bookSelete(String bookId) throws SQLException{
String updateWithUid ="select bookId,bookName,pianduanName from books where bookId=?";
//String updateWithUname ="update users set UID=?
,password=?
,email=?,utype=?where Uname=?";
boolean flag =true;
Connection conn=null;
PreparedStatement stmt=null;
ResultSet rs=null;
try{
conn=DBUtil.getConnection(DBUtil.CONNECTION_SQL);
stmt=conn.prepareStatement(updateWithUid);
stmt.setString(1,bookId);
System.out.println("bookId是"+bookId+"!!!!!!!!!!!!");
rs=stmt.executeQuery();
if(rs.next()==false){flag=false; return flag; }
System.out.println("沒有進入到循環中+++++++++++++++++++++");
String bookId1 = rs.getString(1);
System.out.println(bookId+"++++++++++++++++++");
book.setBookID(bookId1);
String bookName = rs.getString(2);
book.setBookName(bookName);
System.out.println(bookName+"++++++++++++++++++");
String pianduanName = rs.getString(3);
book.setPianduanName(pianduanName);
return flag;
}catch(Exception e){
e.printStackTrace();
}finally{
conn.close();
stmt.close();
}
return false ;
}
}/
/**********************************************************************************************************************************************/
以下的代碼是對response中的數據提取並顯示在頁面中我僅僅做了簡單的處理
假設嫌自己的頁面過於簡單或者是運行的功能過於單一那么在jsp或者是你的servlet中進行控制
<div class="form_row">
<label class="contact"><strong>書籍ID:</strong></label>
<input type="text" class="contact_input" name="bookId" value='<%=request.getAttribute("bookId1") %>'/>
</div>
<div class="form_row">
<label class="contact"><strong>書籍名稱:</strong></label>
<input type="text" class="contact_input" name="bookName" value='<%=request.getAttribute("bookName") %>'/>
</div>
<div class="form_row">
<label class="contact"><strong>片段信息:</strong></label>
<input type="text" class="contact_input" name="pianduanName" value='<%=request.getAttribute("pianduanName") %>' />
</div>
