如何显示从数据库到Java脚本变量的值 [英] how to show value from database to java script variable

查看:61
本文介绍了如何显示从数据库到Java脚本变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Java Servlet代码,从中我从数据库表中获得3个字段,并且我想在JS(Java脚本)页面上以html标签(例如Name =)显示此字段,并且我想从datbase中显示名字字段标签名称的前面.

i have created one java servlet code from which i get 3 field from database table and i want to show this field on my js(java script) page in html tag like Name= and i want to show first name field from datbase infront of lable Name.

推荐答案

只是一个小的google搜索,您就可以进入
just a small google search leads you to this place.[^]


我的建议是在这种情况下使用AJAX,但是如果您对AJAX不确定,则最可能的解决方案是使用Cookie.
将从数据库中检索到的数据存储到cookie对象中,将cookie添加到响应对象中,并在HTML/JSP页面中使用javascript读回cookie.我在这里给你举个例子:

Servlet:GetData.do



受保护的void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException {

试试{
Class.forName("com.mysql.jdbc.Driver");
连接con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","root","root");
PreparedStatement ps = con.prepareStatement(从员工中选择*");
ResultSet rs = ps.executeQuery();
rs.next();
Cookie ck1 =新的Cookie("Name",rs.getString(2));
Cookie ck2 =新的Cookie(地址",rs.getString(3));
Cookie ck3 =新的Cookie(电话",rs.getString(4));
Cookie ck4 =新的Cookie("Email",rs.getString(5));
response.addCookie(ck1);
response.addCookie(ck2);
response.addCookie(ck3);
response.addCookie(ck4);
request.getRequestDispatcher("index.jsp").forward(request,response);

}
catch(例外ee){

}
}




JSP:index.jsp



My Suggestion would be to use AJAX in this scenario, but if you are not confident with AJAX the most probable solution is here to use cookies.
Store your data retrieved from database into cookie object, add the cookie to response object and in HTML/JSP Page use javascript to read the cookie back. Here i''m giving you an example:

Servlet: GetData.do



protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "root");
PreparedStatement ps = con.prepareStatement("Select * from Employee");
ResultSet rs = ps.executeQuery();
rs.next();
Cookie ck1 = new Cookie("Name", rs.getString(2));
Cookie ck2= new Cookie("Address", rs.getString(3));
Cookie ck3 = new Cookie("Phone", rs.getString(4));
Cookie ck4 = new Cookie("Email", rs.getString(5));
response.addCookie(ck1);
response.addCookie(ck2);
response.addCookie(ck3);
response.addCookie(ck4);
request.getRequestDispatcher("index.jsp").forward(request, response);

}
catch(Exception ee){

}
}




JSP: index.jsp



<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1>Welcome to Javascript - Database Interactioin</h1>
    <hr />
    <script type="text/javascript">
        var x = document.cookie;
        alert(x);
    </script>
    <a href="GetData.do">Get Data</a>
</body>
</html>






数据库: MyDB
表:员工
列: EmpID,名称,地址,电话,电子邮件






Database:MyDB
Table:Employee
Columns: EmpID, Name, Address, Phone, Email


这篇关于如何显示从数据库到Java脚本变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆