我遇到了一个问题,我必须根据登录的用户显示 MYSQL 的 Ratings 表中存在的某些数据。
层次结构如下:
login.jsp --> check.jsp --> 欢迎.jsp
我必须根据登录的用户在 Welcome.jsp 上显示数据。我无法将用户参数作为显示用户特定数据的 sql 查询的输入获取到服务器。这是我的文件。
登录.jsp
<%--
Document : login
Created on : May 15, 2012, 10:36:24 AM
Author : Diaa
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<script>
function validate(){
var username=document.form.user.value;
var password=document.form.pass.value;
if(username==""){
alert("Enter Username!");
return false;
}
if(password==""){
alert("Enter Password!");
return false;
}
return true;
}
</script>
<div align="center">
<div class='cssmenu'>
<ul>
<li class='active '><a href='index.jsp'><span>Home</span></a></li>
<li><a href='login.jsp'><span>Login</span></a></li>
<li><a href='index.jsp'><span>About</span></a></li>
<li><a href='#'><span>Contact</span></a></li>
</ul>
</div>
<form name="form" method="post" action="check.jsp" onsubmit="javascript:return validate();">
<div align="center">
<h1>USER LOGIN</h1>
<table>
<tr><td bgcolor="#FF9900">Username:</td><td bgcolor="#33CCCC"><input type="text" name="user"></td></tr>
<tr><td bgcolor="#FF9900">Password:</td><td bgcolor="#33CCCC"><input type="password" name="pass"></td></tr>
<tr><td></td><td><input type="submit" value="Submit"></td></tr>
</table>
</div>
检查.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@page import="java.sql.*"%>
<%
String user=request.getParameter("user");
String pass=request.getParameter("pass");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/recommend","root","root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select UserID,password from Users where UserID='"+user+"' and password='"+pass+"'");
int count=0;
while(rs.next())
{
count++;
}
if(count>0)
{
out.println("welcome "+user);
response.sendRedirect("welcome.jsp?msg=welcome "+user +" <a href=\"index.jsp\">logout</a>");
session.removeAttribute("user");
}
else
{
response.sendRedirect("login.jsp?msg=Invalid Username or Password");
}
%>
欢迎.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%-- Database Interaction Queries Start --%>
<% String user=(String)session.getAttribute(user); %>
<sql:setDataSource var="genretype" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/recommend"
user="root" password="root"/>
<sql:query dataSource="${genretype}" var="result">
SELECT * from Genres;
</sql:query>
<sql:query dataSource="${genretype}" var="result1">
SELECT * from Ratings where UserID = ?
<sql:param value="${user}"/>
</sql:query>
<%-- Database Interaction Queries End --%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Heading</title>
</head>
<body>
<div align="center">
<div class='cssmenu'>
<ul>
<li class='active '><a href='index.jsp'><span>Home</span></a></li>
<li><a href='login.jsp'><span>Login</span></a></li>
<li><a href='index.jsp'><span>About</span></a></li>
<li><a href='#'><span>Contact</span></a></li>
</ul>
</div>
<%String msg=request.getParameter("msg");
if(msg!=null){
%>
<label><font color="red"><%=msg%></font></label>
<%
}
%>
<h1>Sample Title</h1>
<%--For Displaying Genre List Start --%>
<div>
<div align="left" position:absolute left:1200px>
<c:forEach var="row" items="${result.rows}">
<ul id="tab_nav">
<li><c:out value="${row.GenreName}"/></li>
</ul>
</c:forEach>
</div>
<div id="box" >
<table border="1">
<c:forEach var="row" items="${result1.rows}">
<tr>
<td><c:out value="${row.Rating}"/></td>
<td><c:out value="${row.UserID}"/></td>
<td><c:out value="${row.GenreName}"/></td>
<td><c:out value="${row.MovieName}"/></td>
</tr>
</c:forEach>
</table>
</div>
</div>
</body>
</html>
任何人都可以看看这个,让我知道我在这里缺少什么:(除了发布的代码之外,请随时询问任何其他信息。
手指交叉..等待帮助!
干杯!