繁体   English   中英

如何在Java中将查询字符串值从servlet传递到jsp(文本框值)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM