Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中将查询字符串值从servlet传递到jsp(文本框值)_Java_Jsp_Servlets_Jdbc - Fatal编程技术网

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

如何在java中将查询字符串值从servlet传递到jsp(文本框值),java,jsp,servlets,jdbc,Java,Jsp,Servlets,Jdbc,基于查询字符串值,我需要从数据库中获取值,并需要将值从servlet传递到jsp。我如何在这里传递该值?我尝试了这段代码,它显示在文本框nullvalue中 用于此: request.setAttribute("BatchID", value); 使用request.setAttribute()方法在servlet中设置BatchID Servlet try { conn = ds.getConnection(); if (request.getQueryString() !

基于查询字符串值,我需要从数据库中获取值,并需要将值从servlet传递到jsp。我如何在这里传递该值?我尝试了这段代码,它显示在文本框
null
value中

用于此:

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")%>">

您必须使用,还必须检查您的结果集是否为空,您必须使用此选项:

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);
}

我需要将查询字符串值从servlet显示到jsp@RobertE你能试试使用
instead@RobertE我不知道你在servelt或jsp中工作的地方,你能组织你的问题吗?嗨,我在我的servlet中也有插入查询。当我点击按钮时,它会工作,但在按钮点击之前,当显示页面时,我只需要点击display@RobertE您是否尝试过使用
if(res.next()){id=res.getInt(“BatchID”);request.setAttribute(“BatchID”,id);}
?您确定您的
id!=空
您可以在控制台中打印它并进行检查尝试并让我知道以这种方式传递BatchID并进行检查。setAttribute(“BatchID”、“hello”);没有它在文本框中显示仍然为空值我更新了我的图像,它是如何出现的?到目前为止,您尝试了什么?请提供您的代码和相应的错误以及!