基于查询字符串值,我需要从数据库获取值,并且需要将值从servlet传递到jsp,我如何在这里传递该值,我尝试了此代码在文本框中显示为null
值。
You have to use void setAttribute(java.lang.String name, java.lang.Object o)
also you have to check if your ResultSet is not empty, you have to use this instead :
ResultSet res = st.executeQuery(s);
int id = 0;
if(res.next()){
id = res.getInt("BatchID");
}
request.setAttribute("BatchID", id);
Note to avoid any syntax error or sql injection you have to use PreparedStatement instead
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);
set BatchID in servlet using request.setAttribute() method
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")%>">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.