How parsethrough jsonarray and store each column in textbox - java

<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.get("JsonCreation.jsp",function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
</head>
<body>
<button>Send an HTTP GET request to a page and get the result back</button>
</body>
</html>
i am trying to get data from db create jsonarray from resultset and use it to populate text box when a tables row is clicked so far i have displayed the data using alert and i am getting the exact data but i want to access column by column and populate corresponding text
<%#page import="org.json.JSONObject"%>
<%#page import="java.lang.Thread.State"%>
<%#page import="org.json.JSONArray "%>
<%#page import="org.json.JSONObject" %>
<%# page language="java" import="java.io.*,java.sql.*" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
try{
Class.forName("com.mysql.jdbc.Driver");
String mySqlUrl = "jdbc:mysql://localhost:3306/permit";
Connection con = DriverManager.getConnection(mySqlUrl ,"root","moodle123");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from enterprisemaster where enterpriseId=1001");
JSONArray respJson = new JSONArray();
java.sql.ResultSetMetaData rsmd = rs.getMetaData();
int numColumns = rsmd.getColumnCount();
while (rs.next()) {
JSONObject obj = new JSONObject();
for (int i = 1; i < numColumns + 1; i++) {
String columnName = rsmd.getColumnName(i);
obj.put(columnName, rs.getString(columnName));
}
respJson.put(obj);
}
respJson.toString();
out.println(respJson);
}
catch(Exception e)
{
System.out.println(e);
}
%>
</body>
</html>
the out put from the alert box is this
<!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/json; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
[{"phone":"0471-2318922,0435-6203","fax":"0471- 2315893","website":"www.ksidc.org","enterpriseName":"KERALA STATE INDUSTRIAL DEVELOPMENT CORPORATION LIMITED","enterpriseId":"1001","factoryAddress":"","isActive":"1","category":"Financial Services","chairmanName":"Sri.T.K.A.Nair","administrativeDept":"Industries Department","incorporationDate":"21/07/1961","email":"ksidc#vsnl.com","incorporationAct":"Companies Act,1956","mdName":"Sri.Alkesh Kumar Sharma IAS","officeAddress":"T.C.XI/226,Keston Road,Kowdiar,Thriruvananthapuram-695003","activities":"Providing Promotional and financial assistance for industries in Kerala and acting as nodal/implementing/facilitating agency for mega projects"}]
</body>
</html>
Status:success
![alert output its content[1]
[1]: http://i.stack.imgur.com/EIlJw.png

$(document).ready(function(){
$.getJSON("Test.json",function(data){
$.each(data,function(key,value){
alert(data[key].activities);
alert(data[key].enterpriseName);
$('input').val(data[key].enterpriseName);
activities=data[key].activities;
console.log(value);
});
});
});

Related

My jsp code doesn't insert data in database

I am working on a JSP code and want to connect my page with mysql database and to insert name and email in database but it couldn't , I want to know why
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page import="java.sql.*, java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP handle Page</title>
</head>
<body>
<%
String first_name = request.getParameter("user");
String email = request.getParameter("email");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/user","root","");
Statement st=conn.createStatement();
int i=st.executeUpdate("insert into info (name,email) values ('"+first_name+"','"+email+"')");
if(i!=0)
{
System.out.println("Data inserted");
}
else
{System.out.println("no");}
}
catch (Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>

SAP BO login using custom.jsp

I'm using custom.jsp in order to avoid the login screen and load the home screen directly, but still it's asking for login credentials after restarting the tomcat. The path of the custom.jsp file is "SAP BusinessObjects\tomcat\webapps\BOE\WEB-INF\eclipse\plugins\webpath.InfoView\web"
I don't know what I'm missing here and I'm new to this. Below is my jsp code. Thanks.
<%# page import="com.crystaldecisions.sdk.exception.SDKException" %>
<%# page import="com.crystaldecisions.sdk.framework.*" %>
<%# page import="com.crystaldecisions.sdk.occa.infostore.*" %>
<%# page import="com.crystaldecisions.sdk.occa.security.*"%>
<%# page import="java.net.*"%>
<%# page import="com.crystaldecisions.enterprise.*"%>
<%# page import="com.crystaldecisions.sdk.plugin.admin.*"%>
<%# page import="java.sql.*"%>
<%# page import="com.businessobjects.webutil.Encoder" %>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
IEnterpriseSession enterpriseSession;
final String BO_CMS_NAME = "BI98svr";
final String BO_AUTH_TYPE = "secEnterprise";
final String BO_USERNAME = "user121";
final String BO_PASSWORD = "pass121";
ILogonTokenMgr logonTokenMgr;
String defaultToken = "";
boolean loggedIn = true;
try {
enterpriseSession =
CrystalEnterprise.getSessionMgr().logon(BO_USERNAME,BO_PASSWORD,
BO_CMS_NAME,BO_AUTH_TYPE);
logonTokenMgr = enterpriseSession.getLogonTokenMgr();
defaultToken = logonTokenMgr.createWCAToken("", 20, 1);
response.sendRedirect("http://"+BO_CMS_NAME+":8080/BOE/BI/logon/start.do?ivsLogonToken="+Encoder.encodeURL(defaultToken));
}
catch (Exception error)
{
loggedIn = false;
out.println(error);
}
%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
</body>
</html>

Trying to check MySQL database with java, jsp

I am trying to print some data from my database in Mysql using some classes in java and jsp. It's my first time doing this. I googled a lot but nothing helped me. Here is my code:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<form action="DB_results.jsp">
<input type = "submit" value = "Database">
</form>
</body>
</html>
Draw.java
package DB_draw;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Draw {
ResultSet myRs;
public ResultSet getMyRs() {
return myRs;
}
public void setMyRs() {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/tst","root","");
Statement myStmt = myConn.createStatement();
this.myRs = myStmt.executeQuery("select * from tst.people");
while (this.myRs.next())
{
System.out.println(this.myRs.getString("onoma") + " " + this.myRs.getString("epitheto") + " " + this.myRs.getInt("id"));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
DB_results.jsp
<%#page import="java.sql.ResultSet"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page import="java.io.*,java.util.*,java.sql.*"%>
<%# page import="javax.servlet.http.*,javax.servlet.*" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%# page import="DB_draw.*" %>
<jsp:useBean id="DB_draw" class="DB_draw.Draw" scope="session" />
<jsp:setProperty name="DB_draw" property="*"/>
<!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=ISO-8859-1">
<title>Database</title>
</head>
<body style="background-color:black">
<div style = "color:yellow; font-family:fantasy; font-size:30px">
<%
ResultSet myRs = DB_draw.getMyRs();
if(myRs == null) out.println("Problem");
else
while(myRs.next())
{
out.println(myRs.getString("onoma") + " " + myRs.getString("epitheto") + " " + myRs.getInt("id"));
}
%>
</div>
</body>
</html>
The Problem is that when i try to print the results in DB_results.jsp using variable myRs, myRs == NULL. Why is this happening? I'm using Tomcat-Apache 8.0.
You never call setMyRS(). And even if you did, setMyRS() iterates through the resultset, so myRs.next() would always return false in the JSP.

Failed to load the resource:server responded with a status 404

Im working on a DynamicWebProject in Eclipse IDE.I got an error in console while running a jsp page showing "Failed to load resource: the server responded with a status of 404 (Not Found)".I already referred many questions regarding this problem but could not resolve it.Here is my javascript file
availability.js
var ajaxRequest = null;
if (window.activeXObject) {
ajaxRequest = new ActiveXObject("Microsoft.HTTP");
} else {
ajaxRequest = new XMLHttpRequest();
}
function ajaxValidation() {
var name = document.getElementById("username").value;
ajaxRequest.open("GET", "/SpringHibernateProject/validate?name=" + name,
true);
ajaxRequest.onreadystatechange = function() {
if (ajaxRequest.readyState == 4 && ajaxRequest.status == 200) {
var message = ajaxRequest.responseText;
document.getElementById("div").innerHTML = "<font color='red'>"
+ message + "</font>";
}
};
ajaxRequest.send(null);
}
Here is my jsp page
AjaxPage.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="../Js/availability.js"></script>
</head>
<body>
<input type="text" name="username" id="username" onkeyup="ajaxValidation()"></input>
<div id="div"></div>
</body>
</html>
Im getting this error though I referred the script file correctly.Why is it so?

How to retrieve image from mysql db and show it inside <td> and <img> tag in HTML?

How to retrieve image from mysql db and show it inside tag in HTML and that img tag should be placed inside ? Here s my code:
It displays only the image . Its not showing any content other than image.
Thanks in advance.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%# page import="java.io.*"%>
<%# page import="java.sql.*"%>
<%# page import="java.util.*"%>
<%# page import="java.text.*"%>
<%# page import="javax.servlet.*"%>
<%# page import="javax.servlet.http.*"%>
<%# page import="javax.servlet.http.HttpSession"%>
<%# page language="java"%>
<%# page session="true"%>
<%
try{
//PrintWriter out=response.getWriter();
out.println("Retrieve Image Example!");
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/";
String dbName = "db";
String userName = "root";
String password = "root";
Connection con = null;
Class.forName(driverName);
con = DriverManager.getConnection(url+dbName,userName,password);
Statement st = con.createStatement();
%>
<table border='1'>
<tr>
<td>Name:</td><td>
</td>My Name</td>
</tr>
<tr>
<td>Image:</td>
<td width=10px;>
<%
PreparedStatement pre1 = con.prepareStatement("select * from image where id="+8);
ResultSet rs1=pre1.executeQuery();
while(rs1.next())
{byte[] bytearray1 = new byte[4096];
int size1=0;
InputStream sImage1;
sImage1 = rs1.getBinaryStream(2);
response.reset();
response.setContentType("image/jpeg");
response.addHeader("Content-Disposition","filename=logo.jpg");
while((size1=sImage1.read(bytearray1))!= -1 )
{
response.getOutputStream().write(bytearray1,0,size1);
}
response.flushBuffer();
sImage1.close();
rs1.close();
}
out.println("Retrieved Successfully!");
pre.close();
con.close();
}
catch (Exception e){
out.println(e.getMessage());
}
%>
</td></tr>
</table>
</body>
</html>
Images are loaded as separate requests to the request that loaded the html. As such you need to:
establish a URL encoding scheme that you use when specifying the src of the img element.
map a servlet (or similar) to that URL. The servlet loads the image based on the parameters of the URL returning that image in its response.
I found out.
r.jsp:-
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table border=2>
<tr><th>DISPLAYING IMAGE</th></tr>
<tr><td>hi</td></tr>
<tr><td>
<img src="retrieve.jsp" width=130 height=130>
</td></tr>
</table>
</body>
</html>
retrieve.jsp:-
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
</head>
<body>
<%# page import="java.io.*"%>
<%# page import="java.sql.*"%>
<%# page import="java.util.*"%>
<%# page import="java.text.*"%>
<%# page import="javax.servlet.*"%>
<%# page import="javax.servlet.http.*"%>
<%# page import="javax.servlet.http.HttpSession"%>
<%# page language="java"%>
<%# page session="true"%>
<%
try{
//PrintWriter out=response.getWriter();
out.println("Retrieve Image Example!");
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/";
String dbName = "db";
String userName = "root";
String password = "root";
Connection con = null;
Class.forName(driverName);
con = DriverManager.getConnection(url+dbName,userName,password);
Statement st = con.createStatement();
PreparedStatement pre1 = con.prepareStatement("select * from image where id="+8);
ResultSet rs1=pre1.executeQuery();
while(rs1.next())
{byte[] bytearray1 = new byte[4096];
int size1=0;
InputStream sImage1;
sImage1 = rs1.getBinaryStream(2);
response.reset();
response.setContentType("image/jpeg");
response.addHeader("Content-Disposition","filename=logo.jpg");
while((size1=sImage1.read(bytearray1))!= -1 )
{
response.getOutputStream().write(bytearray1,0,size1);
}
response.flushBuffer();
sImage1.close();
rs1.close();
}
pre.close();
con.close();
}
catch (Exception e){
out.println(e.getMessage());
}
%>
</body>
</html>

Categories

Resources