[英]How to Pass Query String value from servlet to jsp(text box value) in java
基於查詢字符串值,我需要從數據庫獲取值,並且需要將值從servlet傳遞到jsp,我如何在這里傳遞該值,我嘗試了此代碼在文本框中顯示為null
值。
您必須使用void setAttribute(java.lang.String name, java.lang.Object o)
並且還必須檢查ResultSet是否不為空,而必須使用以下方法:
ResultSet res = st.executeQuery(s);
int id = 0;
if(res.next()){
id = res.getInt("BatchID");
}
request.setAttribute("BatchID", id);
請注意,為避免任何語法錯誤或SQL注入,您必須改用PreparedStatement
String s = "select BatchID from CPWorkDetails where BatchId = ?";
st = conn.createStatement();
ResultSet res = st.executeQuery(s);
int Id = res.getInt("BatchID");
try (PreparedStatement st = connection.prepareStatement(s)) {
st.setString(1, BatchId1[1]);
ResultSet res = st.executeQuery(s);
int id = 0;
if(res.next()){
id = res.getInt("BatchID");
}
request.setAttribute("BatchID", id);
}
用於此:
request.setAttribute("BatchID", value);
使用request.setAttribute()方法在servlet中設置BatchID
Servlet
try {
conn = ds.getConnection();
if (request.getQueryString() != null) {
String Batch = request.getQueryString();
String[] BatchId1 = Batch.split("=");
String s = "select BatchID from CPWorkDetails where BatchId='" + BatchId1[1] + "'";
st = conn.createStatement();
ResultSet res = st.executeQuery(s);
int Id = res.getInt("BatchID");
request.setAttribute("BatchID",Id );
}
}
Jsp
<input type="text" id="txtBatchName" class="form-control"
name="txtBatchName" value=" <%=request.getAttribute("BatchID")%>">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.