我想从html到jsp页面获取UserName,以便基于UserName它通过jsp页面显示特定的用户数据,我尝试了很多但没有得到所需的输出。如果我在jsp页面中对UserName进行硬编码,那么即时从数据库中获取数据。 请忽略评论的部分。 我的HTML代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Student Login Page</title>
<h1 align="center">Student Login Page</h1>
</head>
<body>
<form name="Servlet_LoginPage" action="Servlet_LoginPage" method="post">
<table align="center">
<!--
<tr>
<td>
<select name="UserSelect" >
<option label="Select User"> </option>
<option value="student">Student</option>
<option value="Admin">Admin</option>
</select>
</td>
</tr>
-->
<tr>
<td>UserName </td><td><input type="text" name=UserName></td>
</tr>
<tr>
<td>Password</td><td><input type="password" name=pwd></td>
</tr>
<tr>
<td></td>
<td><input type="submit">
<input type="reset" ></td>
</tr>
</table>
</form>
</body>
</html>
我的Jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Student Details</title>
</head>
<body>
<%
response.setContentType("text/html");
String uname=request.getParameter("UserName");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/vikas","root","rass");
PreparedStatement ps=con.prepareStatement("select * from Student_Data where UserName="+uname);
ResultSet rs = ps.executeQuery();
%>
<table>
<%
while (rs.next()) {
%>
<tr>
<td height="25" >FirstName: </td><td colspan="2"><%out.println(rs.getString(1));%></td>
<td height="25" >LastName: </td><td colspan="3"><%out.println(rs.getString(2));%></td></tr>
<tr><td height="25" >UserName: </td><td colspan="5"><%out.println(rs.getString(3));%></td></tr>
<tr><td height="25" >Password: </td><td colspan="5"><%out.println(rs.getString(4));%></td></tr>
<tr><td height="25" >DateOfBirth: </td><td colspan="5"><%out.println(rs.getString(5));%></td></tr>
<tr><td height="25" >E-Mail</td><td colspan="5"><%out.println(rs.getString(6));%></td></tr>
<tr><td height="25" >Mobile.No</td><td colspan="5"><%out.println(rs.getString(7));%></td></tr>
<tr><td height="25" >Gender</td><td colspan="5"><%out.println(rs.getString(8));%></td></tr>
<tr><td height="25" >Address</td colspan="5"><td colspan="5"><%out.println(rs.getString(9));%></td></tr>
<tr><td height="25" >City</td><td colspan="5"><%out.println(rs.getString(10));%></td></tr>
<tr><td height="25" >PinCode</td><td colspan="5"><%out.println(rs.getString(11));%></td></tr>
<tr><td height="25" >State</td><td colspan="5"><%out.println(rs.getString(12));%></td></tr>
<tr><td height="25" >Country</td><td colspan="5"><%out.println(rs.getString(13));%></td></tr>
<tr>
<td height="25" >Hobbies</td>
<td colspan="5"><%out.println(rs.getString(14));%> </td>
</tr>
<tr>
<td rowspan="4">Qualification</td><td height="25" >Sl.No</td><td>Examination</td><td>Board</td><td>Percentage</td><td>Year of Passing</td>
</tr>
<tr>
<td height="25" >1</td><td>Class X</td><td><%out.println(rs.getString(15));%></td><td><%out.println(rs.getString(16));%></td><td><%out.println(rs.getString(17));%></td>
</tr>
<tr>
<td height="25" >2</td><td>Class XII</td><td><%out.println(rs.getString(18));%></td><td><%out.println(rs.getString(19));%></td><td><%out.println(rs.getString(20));%></td>
</tr>
<tr>
<td height="25" >3</td><td>Graduation</td><td><%out.println(rs.getString(21));%></td><td><%out.println(rs.getString(22));%></td><td><%out.println(rs.getString(23));%></td>
</tr>
<tr>
<td height="25" >Course Applied</td><td colspan="5"><%out.println(rs.getString(24));%>
</td>
</tr>
</table>
<%
}
}
catch(Exception e)
{
System.out.println(e);
}
%>
</body>
</html>
答案 0 :(得分:0)
您没有正确编写HTML。
<td>UserName </td><td><input type="text" name=UserName></td>
您需要名称值
的引号<td>UserName </td><td><input type="text" name="UserName"></td>
我用
进行了测试<html>
<body>
<form method="GET" action="#">
<input name=test/>
</form>
</body>
</html>
查询以“foobar”值接收:
文件:/// C:?/tmp/test.html试验%2F = foobar的#
为什么,因为这是在旧版本的Firefox上解释HTML的方式(暂时无法检查更新):
<input name="test/">
因此参数名称不是您期望的名称。在HTML中使用正确的语法,它应该解决问题。
然后我需要告诉你这是PreparedStatement
的错误用法,你需要使用参数:
PreparedStatement ps=con.prepareStatement("select * from Student_Data where UserName=?");
ps.setString(uname);
这样,请求将有一个参数UserName
仅供参考:使用<table>
进行设计会被网页开发人员折旧至少十年;)