我有兴趣从DATABASE中获取值并将其转换为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的原因......尽管获得变量值。
答案 0 :(得分:0)
试试这个:
<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>
答案 1 :(得分:0)
<input type="text" name="username" value="<%=unm%>"/>
<input type="text" name="password" value="<%=pwd%>"/>