java web中页面加载时执行函数的问题
本人想在页面加载和关闭的时候都对数据库操作一次,但发现Javascript无法访问数据库,也不知道beforeunload方法后面是不是只能跟js的方法,不知道大家有没有...
本人想在页面加载和关闭的时候都对数据库操作一次,但发现Javascript无法访问数据库,也不知道beforeunload方法后面是不是只能跟js的方法,不知道大家有没有什么好建议,最好写一点代码看看,急!!!QQ:907600323
展开
5个回答
展开全部
JSP:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<script language="JavaScript" type="text/javascript">
window.onload = function() {
checkLocation("onload");
};
function checkLocation(location){
var result = "";
if ( window.XMLHttpRequest )
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
}
var url = "myServlet?locationName="locationmyMethod;
xmlhttp.open( "GET", url , false );
xmlhttp.send( null );
result = xmlhttp.responseText;
xmlhttp.close;
if(result!=""){
//success
}else{
//error
}
}
function cos(){
checkLocation("close");//
window.close;
}
</script>
<body>
<form action="" method="post">
<table>
</table>
<input type="button" value="Close" onclick="cos();">
</form>
</body>
JAVA:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class myServlet
*/
public class myServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public myServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String local = request.getParameter("locationName");
if("".equals("close")){
//关闭网页时你调用的数据库代码
}else{
//进入网页时你调用的数据库代码
}
}
}
WEB.XML加入
<servlet>
<javaee:description></javaee:description>
<javaee:display-name>myServlet</javaee:display-name>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.my.test.myServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/myServlet</url-pattern>
</servlet-mapping>
当然,如果是网页强制关闭(就是点上方的X)
你需要判断下了,
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<script language="JavaScript" type="text/javascript">
window.onload = function() {
checkLocation("onload");
};
function checkLocation(location){
var result = "";
if ( window.XMLHttpRequest )
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
}
var url = "myServlet?locationName="locationmyMethod;
xmlhttp.open( "GET", url , false );
xmlhttp.send( null );
result = xmlhttp.responseText;
xmlhttp.close;
if(result!=""){
//success
}else{
//error
}
}
function cos(){
checkLocation("close");//
window.close;
}
</script>
<body>
<form action="" method="post">
<table>
</table>
<input type="button" value="Close" onclick="cos();">
</form>
</body>
JAVA:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class myServlet
*/
public class myServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public myServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String local = request.getParameter("locationName");
if("".equals("close")){
//关闭网页时你调用的数据库代码
}else{
//进入网页时你调用的数据库代码
}
}
}
WEB.XML加入
<servlet>
<javaee:description></javaee:description>
<javaee:display-name>myServlet</javaee:display-name>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.my.test.myServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/myServlet</url-pattern>
</servlet-mapping>
当然,如果是网页强制关闭(就是点上方的X)
你需要判断下了,
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
类似session监听吗?
这个代码有现成的,你可以搜索一下,代码一般分2部分,java部分和javascript部分。
要考虑浏览器被强制关闭的情况,这种情况下不会触发beforeunload。需要在session销毁时做处理。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用DWR或者先刷新一次页面,把要得到的数据隐藏起来!之后再用JQ进行判断!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个用ajax试试
来自:求助得到的回答
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询