如何在JSP中 即时在网页上更新数据中更新的数据,在线等~~
我的源码如下。
usermanger.jsp
<%@ page language="java"%>
<%@ page import="java.sql.*"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
response.setHeader("Cache-Control", "no-cache, no-store"); //HTTP 1.1
response.setHeader("Pragma", "no-cache"); //HTTP 1.0
response.setDateHeader("Expires", 0); //prevents caching at the proxy server
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<META HTTP-EQUIV= "pragma" CONTENT= "no-cache ">
<META HTTP-EQUIV= "Cache-Control" CONTENT= "no-cache, must-revalidate ">
<META HTTP-EQUIV= "expires" CONTENT= "0 ">
<title>管理员管理</title>
<link href="css/Style.css" rel="stylesheet" type="text/css">
<script language=javascript>
function checkdel()
{
if(confirm("你确定要删除该管理员吗?一旦删除,将不能恢复!"))
return true;
else
return false;
}
</script>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body style="width:100%;">
<jsp:useBean id="connDBBean1" scope="page" class="Conn.conn"/>
<%
ResultSet rs=connDBBean1.executeQuery("select * from denglu");%>
<br>
<table width="99%" border="0" align="center" cellpadding="0" cellspacing="1" style="border: 2px solid #ffffff;" class="border">
<tr align="center" class="sec_menu">
<td width="9%" class="tbbgtitle">ID</td>
<td width="20%" class="tbbgtitle">账号</td>
<td width="19%" class="tbbgtitle">姓名</td>
<td width="15%" class="tbbgtitle">最后登录IP</td>
<td width="19%" class="tbbgtitle">最后登录时间</td>
<td width="18%" class="tbbgtitle">操作</td>
</tr>
<%
while(rs.next()){
%>
<tr align="center" class="lineheight" onMouseOver="this.style.background='#F8F8F8';this.style.cursor='hand';" onMouseOut="this.style.background='';this.style.cursor='default';">
<%
String m=rs.getString(1);
int i=Integer.parseInt(m);
out.print("<td>"+i+"</td>");
out.print("<td>"+rs.getString(2)+"</td>");
out.print("<td>"+rs.getString(3)+"</td>");
out.print("<td>"+rs.getString(4)+"</td>");
out.print("<td>"+rs.getString(5)+"</td>");
%>
<td><a href="delete.jsp?id=<%=i%>" onclick="checkdel();">删除</a>
<input name="admindel" type="hidden" id="admindel" value="del"></td>
</tr>
<%
}%>
</table>
<table width="99%" border="0" align="center" cellpadding="0" cellspacing="1" style="border: 2px solid #ffffff;" class="border">
<tr align="center" class="lineheight" onMouseOver="this.style.background='#F8F8F8';this.style.cursor='hand';" onMouseOut="this.style.background='';this.style.cursor='default';">
<tr align="center">
<td align="center"><a href="adminadd.jsp">*添加新用户*</a></td>
</tr>
</table>
</body>
</html>
下面有一个添加新用户,连接到adminadd.jsp.
adminadd.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登陆页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center><h1>添加管理</h1></center>
<div align="center" style=" margin-top:100px;">
<form id="form" name="form" method="post" action="adadd.jsp">
<p>
<label >请输入添加管理员ID:
<input name="username" type="text" id="username" size="15" /></label>
</p>
<p>
<label>请输入密码:
<input name="password" type="password" id="password" size="15" /></label>
</p>
<p>
<label>请确认密码:
<input name="password1" type="password" id="password1" size="15" /></label>
</p>
<label><input name="" type="submit" value="添加" /></label>
</form>
</div>
</body>
</html>
如上,把写好的数据发到adadd.jsp中
adadd.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登陆页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String userid = request.getParameter("username");
String password = request.getParameter("password");
String password1 = request.getParameter("password1");
//System.out.println(userid + "-----" + password) ;
if (!password.equals(password1) || userid == null || userid == "" || userid.length()>20 || password == null ||password == "" || password.length()>20 )
{
try{
response.sendRedirect ( "wrong.jsp?wrongid=3" );
} catch ( Exception e ) {
}
}
else{
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr="jdbc:odbc:test";
Connection connect=null;
ResultSet rs=null;
Statement stmt=null;
try {
Class.forName(sDBDriver);
connect=DriverManager.getConnection(sConnStr);
stmt=connect.createStatement();
String sql="insert into denglu(id,name,pw)values(' "+userid+" ',' "+password+" ',' "+password+" ')";
stmt.executeUpdate(sql);
response.sendRedirect("usermanger.jsp");
}catch (Exception ex){ //没必要分那么明细的异常 ,反正你一报异常就跳到wrong.jsp
ex.printStackTrace() ;
response.sendRedirect("wrong.jsp?wrongid=4" );
}
finally{
stmt.close();
connect.close();
}
}
%>
</body>
</html>
将数据添加到数据库中, 这时候问题来了,我用的是直接跳转到usermanger.jsp中,但跳转过去之后usermanger.jsp中显示的数据没有随着数据库的更新而更新, 求教,如何让它自动更新啊,在线等, 急求求求求。 有愿意QQ帮忙大神就更好的,直接私信我,谢谢。
[解决办法]
数据库里面确实更新了么?
Conn.conn 这里面是否进行了缓存?
跳转完毕后,直接按F5刷新,新数据会显示么?
[解决办法]
楼主,你是跳转问题,
response.sendRedirect("usermanger.jsp");是直接命令浏览器打开usermanger.jsp
而数据并未更新,所以你的页面才显示的是没更新的页面。
用request.getRequestDispatcher("usermanger.jsp").forward(request, response);
跳转就可以了。