会员登录后,可以显示信息。类似与在csdn上面的,登录后,在【通知】里显示几条信息的功能,要源代码。 如题。。在线等啊。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <%@ page contentType="text/html; charset=gb2312" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Untitled Document</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><LINK href="hellking.css" type=text/css rel=stylesheet><body> <% boolean isLog=false; // out.println(session.getAttribute("isLog")); try { isLog=((String)session.getAttribute("isLog")).equals("1"); } catch(Exception e) { //out.println(e); }%><table width="842" align=center cellpadding=3 cellspacing=1 class=tableborder1> <tr> <td width="832" class=tablebody2>:: <a href="index.jsp" target="_top">首页</a> <a href="index.jsp" target="_top">邮件</a> <a href="shop/searchForm.html" target="mainFrame">商品搜索</a> <a href="/jspdev/servlet/forumservlet?jumpPage=1" target="mainFrame">论坛</a> <% if(isLog) { if(((Integer)session.getAttribute("userType")).equals(new Integer(1))) { %> <a href="index.jsp" target="_top">个人信息</a> <%} else { %> <a href="manage/index.jsp" target="_new">系统管理</a> <% } %> <a href="logout.jsp" target="_top">注销</a><%}%>:: </td> </tr> <tr> <td width="832" class=tablebody2> <% // out.println(isLog); if(isLog) { %> 欢迎光临!<%=session.getAttribute("name")%>,您是第<%=session.getAttribute("userLogCount")%>次登录,您上次登录的时间是:<%=session.getAttribute("userLastLogTime")%> <% }//if else { %> <form name="form1" method="post" action="login.jsp"> 用户名 <input type="text" name="userId" size="15" style="BORDER-RIGHT: #ffffff 1px groove; BORDER-TOP: #ffffff 1px groove; FONT: 12px Verdana,Geneva,sans-serif; BORDER-LEFT: #ffffff 1px groove; WIDTH: 100px; COLOR: #000000; BORDER-BOTTOM: #ffffff 1px groove; HEIGHT: 18px; BACKGROUND-COLOR: #DFF1F9"> 密码 <input type="password" name="password" size="15" style="BORDER-RIGHT: #ffffff 1px groove; BORDER-TOP: #ffffff 1px groove; FONT: 12px Verdana,Geneva,sans-serif; BORDER-LEFT: #ffffff 1px groove; WIDTH: 100px; COLOR: #000000; BORDER-BOTTOM: #ffffff 1px groove; HEIGHT: 18px; BACKGROUND-COLOR: #DFF1F9"> <input type="submit" name="Submit" value="确定" style="height:20; font:9pt; BORDER-BOTTOM: #cccccc 1px groove; BORDER-RIGHT: #cccccc 1px groove; BACKGROUND-COLOR: #eeeeee"> </form> <% }//else %> </td> </tr> </table></body></html> 代码这个没有具体的需求是不好写的,给你提供一个思路,在数据库中,存放消息的表中,增加一个字段,flag,0为未读,1为已读,在添加消息的时候,设置对应消息的flag为1,也就是未读。在dao里面写一个方法,public int getUnReadMsgNumByUserId(int userid){ ConnectionUtil util = new ConnectionUtil(); Connection conn = util.getConnection(); String sql = "select count(*) from MsgTbl where userid = ? and flag = 0"; try { PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, u.getUsername()); ResultSet rs = pstmt.executeQuery(sql); if(rs.next()){ return rs.getInt(0); } } catch (SQLException e) { e.printStackTrace(); } finally{ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); }} return 0;}当用户登陆时,调用这个方法,获取数目,int count = dao.getUnReadMsgNumByUserId(userid);userid通过登录的用户获得;并且保存在session里面,session.setAtribute("NewmsgCount",count)在jsp页面里面用写成:新消息:${NewmsgCount}用户读了对应的消息,就更新数据库,把对应消息的flag改成1int count = dao.getUnReadMsgNumByUserId(userid);并且更新session.setAtribute("NewmsgCount",count)这些可以通过Ajax的XmpHttpRequest对象来异步调用刷新,给你一个ajax的例子,如下:这个实现了在窗口关闭时,用ajax异步调用logout方法的。你可以参考一下window.onload=initAll; var httpReq=false; var url="userlist.jsp"; function initAll() { makeRequest(url); } function makeRequest(url) { if(window.XMLHttpRequest)// Mozilla, Safari,... { httpReq=new XMLHttpRequest(); } else { if(window.ActiveXObject)//IE { try { httpReq=new ActiveXObject("Microsoft.XMLHTTP"); } catch(e){} } } if(httpReq) { httpReq.onreadystatechange=tillRefresh; httpReq.open("GET",url,true);//异步 httpReq.send(null); } else { alert("不能创建一个XMLHTTPRequest实例"); } } function tillRefresh() { var tempDiv=document.createElement("div"); var userList=document.getElementById("userList"); if(httpReq.readyState==4) { if(httpReq.status==200) { tempDiv.innerHTML=httpReq.responseText; var required=tempDiv.getElementsByTagName("div"); userList.innerHTML=required[0].innerHTML; setTimeout("makeRequest(url)",1000); } else { alert("对不起,出现了一些问题 :"+httpReq.status); } } }function window.onbeforeunload() { if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey) { var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); xmlhttp.open("GET", "/ChatDemo/login.do?methodName=logout", false); xmlhttp.send(); } } PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, u.getUsername()); 这里写错了,不好意思啊,u.getUsername()应该改成,userid HI,我想的跟你的差不多。但是在用户读过一条数据时,再更新flag为已读时,需要再次链接数据库,使用update更新数据库? struts2显示图片问题 Java实现多页打印功能~ 大虾请进 求助!java上输出csv 0不出来的问题 dtree一个技术问题,急切!!! ocx 控件调用问题 花生壳+tomcat??? 如何通过页标签来控制表格中的数据动态的显示? 请教一个jsp查询速度慢的问题. 最新开发了图像监控JVM的内存使用的完整web application,需要清举手。 spring与struts整合问题,ServletResultDispatcherbean不能被创建 jsp 文件下载乱码问题。 是不是路径的问题?
<%@ page contentType="text/html; charset=gb2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head><LINK href="hellking.css" type=text/css rel=stylesheet><body>
<%
boolean isLog=false;
// out.println(session.getAttribute("isLog"));
try
{
isLog=((String)session.getAttribute("isLog")).equals("1");
}
catch(Exception e)
{
//out.println(e);
}
%>
<table width="842" align=center cellpadding=3 cellspacing=1 class=tableborder1>
<tr>
<td width="832" class=tablebody2>::
<a href="index.jsp" target="_top">首页</a>
<a href="index.jsp" target="_top">邮件</a>
<a href="shop/searchForm.html" target="mainFrame">商品搜索</a>
<a href="/jspdev/servlet/forumservlet?jumpPage=1" target="mainFrame">论坛</a>
<%
if(isLog)
{
if(((Integer)session.getAttribute("userType")).equals(new Integer(1)))
{
%>
<a href="index.jsp" target="_top">个人信息</a>
<%}
else
{
%>
<a href="manage/index.jsp" target="_new">系统管理</a>
<%
}
%>
<a href="logout.jsp" target="_top">注销</a><%}%>::
</td> </tr> <tr>
<td width="832" class=tablebody2>
<%
// out.println(isLog);
if(isLog)
{
%>
欢迎光临!<%=session.getAttribute("name")%>,您是第<%=session.getAttribute("userLogCount")%>次登录,您上次登录的时间是:<%=session.getAttribute("userLastLogTime")%>
<%
}//if
else
{
%>
<form name="form1" method="post" action="login.jsp">
用户名
<input type="text" name="userId" size="15" style="BORDER-RIGHT: #ffffff 1px groove; BORDER-TOP: #ffffff 1px groove; FONT: 12px Verdana,Geneva,sans-serif; BORDER-LEFT: #ffffff 1px groove; WIDTH: 100px; COLOR: #000000; BORDER-BOTTOM: #ffffff 1px groove; HEIGHT: 18px; BACKGROUND-COLOR: #DFF1F9">
密码
<input type="password" name="password" size="15" style="BORDER-RIGHT: #ffffff 1px groove; BORDER-TOP: #ffffff 1px groove; FONT: 12px Verdana,Geneva,sans-serif; BORDER-LEFT: #ffffff 1px groove; WIDTH: 100px; COLOR: #000000; BORDER-BOTTOM: #ffffff 1px groove; HEIGHT: 18px; BACKGROUND-COLOR: #DFF1F9">
<input type="submit" name="Submit" value="确定" style="height:20; font:9pt; BORDER-BOTTOM: #cccccc 1px groove; BORDER-RIGHT: #cccccc 1px groove; BACKGROUND-COLOR: #eeeeee">
</form>
<%
}//else
%>
</td>
</tr>
</table>
</body>
</html>
在添加消息的时候,设置对应消息的flag为1,也就是未读。在dao里面写一个方法,
public int getUnReadMsgNumByUserId(int userid){
ConnectionUtil util = new ConnectionUtil();
Connection conn = util.getConnection();
String sql = "select count(*) from MsgTbl where userid = ? and flag = 0";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, u.getUsername());
ResultSet rs = pstmt.executeQuery(sql);
if(rs.next()){
return rs.getInt(0);
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return 0;
}当用户登陆时,调用这个方法,获取数目,
int count = dao.getUnReadMsgNumByUserId(userid);
userid通过登录的用户获得;
并且保存在session里面,
session.setAtribute("NewmsgCount",count)
在jsp页面里面用写成:新消息:${NewmsgCount}用户读了对应的消息,就更新数据库,把对应消息的flag改成1
int count = dao.getUnReadMsgNumByUserId(userid);
并且更新session.setAtribute("NewmsgCount",count)这些可以通过Ajax的XmpHttpRequest对象来异步调用刷新,给你一个ajax的例子,如下:这个实现了在窗口关闭时,用ajax异步调用logout方法的。
你可以参考一下window.onload=initAll;
var httpReq=false;
var url="userlist.jsp";
function initAll()
{
makeRequest(url);
}
function makeRequest(url)
{
if(window.XMLHttpRequest)// Mozilla, Safari,...
{
httpReq=new XMLHttpRequest();
}
else
{
if(window.ActiveXObject)//IE
{
try
{
httpReq=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){}
}
}
if(httpReq)
{
httpReq.onreadystatechange=tillRefresh;
httpReq.open("GET",url,true);//异步
httpReq.send(null);
}
else
{
alert("不能创建一个XMLHTTPRequest实例");
}
}
function tillRefresh()
{
var tempDiv=document.createElement("div");
var userList=document.getElementById("userList");
if(httpReq.readyState==4)
{
if(httpReq.status==200)
{
tempDiv.innerHTML=httpReq.responseText;
var required=tempDiv.getElementsByTagName("div");
userList.innerHTML=required[0].innerHTML;
setTimeout("makeRequest(url)",1000);
}
else
{
alert("对不起,出现了一些问题 :"+httpReq.status);
}
}
}
function window.onbeforeunload()
{
if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey)
{
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET", "/ChatDemo/login.do?methodName=logout", false);
xmlhttp.send();
}
}
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, u.getUsername()); 这里写错了,不好意思啊,u.getUsername()应该改成,userid