文本框可以';t获取从数据库检索到的jsp中的局部变量值

文本框可以';t获取从数据库检索到的jsp中的局部变量值,jsp,jdbc,Jsp,Jdbc,我感兴趣的是从数据库中取出值并将其放入textbox value属性中,以下是我的代码: <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional

我感兴趣的是从数据库中取出值并将其放入textbox value属性中,以下是我的代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Update Your Data</title>
</head>
<body>
    <%! ResultSet rs; %>
    <%! Connection con;%>
    <%! PreparedStatement pstmt;%>
    <%! String sql = "SELECT * FROM USERS WHERE USERNAME = ?"; %>
    **<%! String unm,pwd; %>**
<h3>Update Your Details</h3>
<hr/>

<form action="UpdateSrvlt" method="post">
    <%
        try{
        Class.forName("com.mysql.jdbc.Driver");
        }catch(ClassNotFoundException cnfe){
            System.out.println("Error :"+cnfe.getMessage());
        }
        try{
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
        pstmt = con.prepareStatement(sql);
        pstmt.setString(1,request.getParameter("username"));
        rs = pstmt.executeQuery();
        while(rs.next()){
            **unm = rs.getString("username");**
            System.out.println("Username at Update.jsp: "+unm);
            **pwd = rs.getString("password");**
            System.out.println("Password at Update.jsp: "+pwd);

        }
        }catch(SQLException se){
            System.out.println("Error :"+ se.getMessage());
        }       
    %>
    <table>
        <tr>
            <td align="left">
                Username :
            </td>
            <td align="left">

                <input type="text" name="username" value="${unm}"/>
            </td>
        </tr>
        <tr>

            <td align="left">
                Password :
            </td>
            <td>
                <input type="text" name="password" value="${pwd}"  />
            </td>
        </tr>

        <tr>
            <td><input type="submit" value="Update" /></td>
        </tr>
        <%
            pstmt.close();
            con.close();
        %>
    </table>
    </form>
</body>
</html>

更新您的数据
****
更新您的详细信息

用户名: 密码:
任何人都知道获取null的原因。尽管获取变量值。

尝试以下方法:

<body>
<%! 
    String unm="", pwd="";
%>
<form>
<%
    //your code to get userName and password here
    pageContext.setAttribute("unm", unm);
    pageContext.setAttribute("pwd", pwd);
%>
<input type="text" name="username" value="${unm}"/>
<input type="text" name="password" value="${pwd}"/>
</form>
</body>



我在页面上获取查询字符串值。。当我在服务器端打印它们时,我甚至得到了这个值,但是我怎么不能在标记值中得到这个值。看到这个答案,我得到了空值。在这两个文本框中。通过任何一种方法。早些时候,我试用了第二个,我在文本框中打印了空值。@Zeelous请看一下更新,我已经测试了它,它工作正常。我在文本框中打印了空值。是的,谢谢你的回复。。!由于
pstmt.setString(1,request.getParameter(“用户名”)),您将获得NULL
<input type="text" name="username" value="<%=unm%>"/>
<input type="text" name="password" value="<%=pwd%>"/>