我正在尝试创建社交网站,并且面临接受和拒绝朋友请求的问题。这是我用来接受好友请求的代码:
<body>
<%!
Connection conn;
int user_id;
int send_id;
PreparedStatement pstmt;
%>
<%
HttpSession session2 = request.getSession(false);
user_id = (Integer)session2.getAttribute("id");
send_id = Integer.parseInt(request.getParameter("snd_id"));
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException e) {
out.print(e);
}
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","vishal");
pstmt = conn.prepareStatement("create table if not exists Friends(requested int, accepted int)");
pstmt.executeUpdate();
pstmt = conn.prepareStatement("insert into Friends values(?,?)");
pstmt.setInt(1,send_id);
pstmt.setInt(2,user_id);
pstmt.executeUpdate();
pstmt = conn.prepareStatement("delete from PendingRequest where sender_id=? and receiver_id = ?");
pstmt.setInt(1,send_id);
pstmt.setInt(2,user_id);
pstmt.executeUpdate();
}
catch(SQLException e) {
out.print(e);
}
RequestDispatcher rd = request.getRequestDispatcher("/home.jsp");
rd.include(request,response);
%>
现在代码将用户重定向到&#34; home.jsp&#34;页面,“朋友请求”列显示0个请求,但在数据库中有2到3个待处理请求。
有人可以解决这个问题吗?
以下是打印好友请求数量的代码:
<%
try
{
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/project","root","vishal");
PreparedStatement pstmt = conn.prepareStatement("create table if not exists PendingRequest(sender_id int, receiver_id int)");
pstmt.executeUpdate();
PreparedStatement pstmt2 = conn.prepareStatement("select count(sender_id) from PendingRequest where receiver_id=?");
pstmt2.setInt(1,user_id);
ResultSet rst = pstmt2.executeQuery();
if(rst.next())
cnt=rst.getInt(1);
pstmt = conn.prepareStatement("select sender_id from PendingRequest where receiver_id=?");
pstmt.setInt(1,user_id);
rst = pstmt.executeQuery();
while(rst.next())
{
send[i] = rst.getInt(1);
i++;
}
}
catch(SQLException e)
{
out.print(e);
}
%>
Friend Requests (<%=cnt%>)
答案 0 :(得分:0)
我正在重定向到&#34; home.jsp&#34;重定向到网页&#34; home.jsp&#34;直接没有任何人登录。所以,我必须将登录ID和密码作为查询字符串传递,如下所示:
String lnk = "/home.jsp?login="+email+"&pass="+password;
pageContext.forward(lnk);
我已经将登录ID和密码检索为:
String email = (String)session2.getAttribute("email");
String password = (String)session2.getAttribute("password");
我已经放置在Sriplet内。