怎么在JavaScript脚本中执行java后台代码,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
使用场景:关闭页面弹窗时执行sql语句。
其实js里执行sql语句有多种方式。
方式一:直接在js代码里调用sql语句,原则上不能使用,因为这将sql直接暴露在客户端,安全性极差。
方式二:在js里运用ajax技术,调用后台方法执行sql语句。
方式三:运用dwr框架,其实是对ajax技术进行了封装。
一、首先编写一个dwr.xml文件
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr//dwr20.dtd">
<dwr>
<allow>
<!-- 对呼叫类型的操作 -->
<create javascript="CallTypeOpDwr" creator="new">
<param name="class"
value="cn.etcom.jfgl.tel.fee.callType.CallTypeOpDwr" />
<include method="addCallType" />
<include method="modifyCallType" />
<include method="deleteCallType" />
<include method="updateSp" />
</create>
</allow>
<!-- 用户信息管理操作中用到的map配置 -->
<signatures>
<![CDATA[
import java.util.List;
import cn.etcom.jfgl.customer.tel.userInfo.UserInfoManageOpDwr;
UserInfoManageOpDwr.addUsersInfo(String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,Map<String,String>);
UserInfoManageOpDwr.addUserInfo(String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,String,Map<String,String>);
]]>
</signatures>
</dwr>
二、在web.xml文件里配置servlet
<!-- DWR相关配置 -->
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>
allowGetForSafariButMakeForgeryEasier
</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr
// 处理呼叫类型的dwr
public class CallTypeOpDwr {
public String updateSp() {
String sql = "exec Pub_Sp_UpdateFieldMemo ?";
Connection conn = DBUtil.getConnection();
PreparedStatement psmt = null;
try {
psmt = conn.prepareStatement(sql);
psmt.setString(1, "CALL");
boolean a = psmt.execute();
DBUtil.close(null, psmt, conn);
conn = DBUtil.getConnection();
psmt = conn.prepareStatement(sql);
psmt.setString(1, "USER");
boolean b = psmt.execute();
System.out.println(a);
System.out.println(b);
System.out.println("关闭弹窗执行存储过程 >> "+sql);
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(null, psmt, conn);
}
return null;
}
}
四、写一个JSP页面,主要导入engine.js和相应的java类映射的js文件,然后编写js即可调用
//以下为jsp的引入js文件部分
<script type="text/javascript">
//设置根路径,放在 tab.js 前
var glbRootPath = "<%=basePath%>";
</script>
<script src="<%=basePath%>js/lhgdialog/lhgcore.min.js" type="text/javascript"></script>
<script src="<%=basePath%>js/lhgdialog/lhgdialog.js" type="text/javascript"></script>
五、js文件里调用后台代码
var basePath = "";
if(typeof(glbRootPath) != "undefined"){
basePath = glbRootPath;
document.write("<script type='text/javascript' src='"+basePath+"\/dwr\/engine.js'><\/script><script type='text/javascript' src='"+basePath+"\/dwr\/interface\/CallTypeOpDwr.js'><\/script>");
}
.
.
.
if(typeof(glbRootPath) != "undefined"&&$(this).parent().children().eq(0).text()=="呼叫类型设置"){
console.log("进来了");
DWREngine.setAsync(false);
CallTypeOpDwr.updateSp();
}
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注编程网行业资讯频道,感谢您对编程网的支持。