I have a Java Program which contains my own thread (start() and run() methods). Code is shown below :
public class MyClass
{
public void threadStart()
{
Threadone t1 = new Threadone();
t1.start();
}
}
class Threadone extends Thread
{
public void run()
{
String url = "jdbc:mysql://localhost:3306";
String user= "root";
String pwd = "root#123";
String qry = "SELECT Country FROM test.abuse WHERE Priority='High'";
Connection conn = null;
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url, user, pwd);
ResultSet rs = conn.prepareStatement(qry).executeQuery();
while(rs.next())
{
System.out.println(rs.getString("Country"));
}
}
catch (SQLException | ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
JSP File:
<%# 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>
<%
MyClass my = new MyClass();
my.threadStart();
%>
</body>
</html>
As of now I am displaying the result in java console. But my task is to display the result in jsp file, Is it possible? If possible how to print the data in webpage using jsp file?
Create a shared collection like map or list .The background thread will populate into the shared collection.And through AJAX or other mechanism fetch from the shared collection.Consider thread safety as it required multiple thread will access the collection while background thread is processing.
Related
When i want connect to MySql using JSP, i encounted that error "org.apache.jasper.JasperException: Unable to compile class for JSP, An error occurred at line: 14 in the jsp file: /index.jsp DbClass cannot be resolved" whereas everything look like normally in my code. Firstly i tought it would be caused by the MySQL jar file but didn't. Doesn't it have to be the class file (DbClass.class) in my project?
index.jsp
<%# page import="com.sampleWeb.*,java.sql.*"%>
<%# 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>sampleWeb</title>
</head>
<body>
<%
Connection c = DbClass.connect();
out.println(c);
DbClass.closeConnection(c);
%>
</body>
</html>
DbClass.java
package com.sampleWeb;
import java.sql.*;
public class DbClass {
public DbClass(){}
public static Connection connect() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
return DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
} catch() {
return null;
}
}
public static boolean closeConnection(Connection conn) {
try{
conn.close();
return true;
} catch(Exception e) {
return false;
}
}
}
Your code works. I tested it.
The only thing was the empty catch parentheses } catch() {.
I can imagine that compilation issues in the DbClass could made it unresolvable.
In order to test this assumtion and to localize the issue, try simplifying the DbClass to simple return:
package com.sampleWeb;
public class DbClass {
public static String connect() {
return "test";
}
}
Sorry guys I have tried this again through creating class with method inside the main class but it showing this error in jsp page ;
java.lang.UnsatisfiedLinkError:
org.hyperic.sigar.Sigar.getCpuInfoList()[Lorg/hyperic/sigar/CpuInfo;
I'm not sure if that from the code or Sigar API library sorry again but i need help to get this jsp right
package mydata;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
public class MyCpu {
private String cpuInfoList;
public String getCpuInfoList() {
return this.cpuInfoList;
}
public MyCpu() {
Sigar sigar = new Sigar();
String output = " ";
CpuInfo[] cpuInfoList = null;
try {
cpuInfoList = sigar.getCpuInfoList();
} catch (SigarException e) {
e.printStackTrace();
return;
}
for (CpuInfo info : cpuInfoList) {
output += "Vendor: " + info.getVendor() + "";
}
System.out.println(output);
}
public static void main(String[] args) {
MyCpu main = new MyCpu();
}
}
--------------------------------------------------------------------------------------
<!---JSP--->
<%#page import ="org.hyperic.sigar.Sigar"%>
<%#page contentType="text/html" 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>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%#page import="mydata.*"%>
<%
MyCpu cpu = new MyCpu();
Sigar sigar = new Sigar();
out.println(sigar.getCpuInfoList());
%>
</body>
</html>
You can call java code in JSP using scriplet but it's bad practice to use scriplet in jsp instead use JSTL. Lets move on create one class as mentioned below
public class Demo {
public String output = "";
public Demo() {
CpuInfo[] cpuInfoList = null;
try {
cpuInfoList = new Sigar().getCpuInfoList();
} catch (SigarException e) {
e.printStackTrace();
}
for (CpuInfo info : cpuInfoList) {
output += info.getVendor() + "\n";
}
System.out.println(output);
}
public static void main(String[] args) {
new Demo();
}
}
Change your jsp code to following
<%#page import="com.Demo"%>
<!DOCTYPE HTML">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
out.println(new Demo().output);
%>
</body>
</html>
And Add this to classpath -Djava.library.path="./lib" in manifest.mf of webapp so finally menifest.mf looks like:
Manifest-Version: 1.0
Class-Path: -Djava.library.path="./lib"
And ofcourse add sigar.jar in lib folder located in WEB_INF.
EDIT:
You should see some kind of below error in your server log:
Info: 5 [http-listener-1(5)] DEBUG Sigar - no sigar-amd64-winnt.dll in java.library.path
org.hyperic.sigar.SigarException: no sigar-amd64-winnt.dll in java.library.path
So you have to provide sigar-amd64-winnt.dll in lib folder as well.
And now you will see output like
I have got MyCpu java class which is running fine and I need to display the output into web page I have tried using JSP only and it doesn't work and then I added JSTL tag library and added this code -Djava.library.path="./lib" to MANIFEST.MF
also I'm using Sigar API and I added to library then I end with this error:
java.lang.UnsatisfiedLinkError: org.hyperic.sigar.Sigar.getCpuInfoList()[Lorg/hyperic/sigar/CpuInfo;
Any help or advice please?
package mydata;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
public class MyCpu {
private String cpuInfoList;
public String getCpuInfoList(){
return this.cpuInfoList;
}
public MyCpu() {
Sigar sigar = new Sigar();
String output = " ";
CpuInfo[] cpuInfoList = null;
try {
cpuInfoList = sigar.getCpuInfoList();
} catch (SigarException e) {
e.printStackTrace();
return;
}
for (CpuInfo info : cpuInfoList) {
output += "Vendor: " + info.getVendor() + "\n";
output += "Model: " + info.getModel() + "";
output += "DD:" +info.toString()+ "";
}
System.out.println(output);
}
public static void main(String[] args) {
MyCpu main = new MyCpu();
}
}
----------------------------------------------------------------------------------
<%#page import="mydata.MyCpu"%>
<%#page import ="org.hyperic.sigar.Sigar"%>
<%#page contentType="text/html" 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>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%
MyCpu cpu = new MyCpu();
Sigar sigar = new Sigar();
out.println(sigar.getCpuInfoList()[4]);
%>
</body>
</html>
Try adding it as a JVM argument
-Djava.library.path="./lib"
My code is as below, I have a MySQL DB on AWS RDS. I have also already included the mysql-connector-java-5.1.31-bin.jar in my build path.
My code is as below :
<%# 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">
<%# page import="java.sql.*"%>
<%# page import="java.util.Scanner "%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome Customer to Online Mall</title>
</head>
<body>
<%
final String host = "jdbc:mysql://mySqlPath/ShopSystem";
final String uName = "myusername";
final String uPass = "mypassword";
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
//#SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
String Cust_address, Cust_email, Cust_contact;
Connection con = DriverManager.getConnection( host, uName, uPass );
//View all shops
Statement stmt5 = con.createStatement();
ResultSet rs2 = stmt5.executeQuery("SELECT * FROM ShopSystem.Shop");
while(rs2.next())
{
System.out.print("s_id = "+rs2.getInt(1)+"\t");
System.out.print("s_name = "+rs2.getString(4)+"\t");
System.out.print("s_location = "+rs2.getString(3)+"\t");
System.out.println();
}
sc.close();
}
catch(SQLException err)
{
System.out.println(err.getMessage());
}
%>
</body>
</html>
I am getting the following error :
Aug 01, 2014 5:31:46 PM org.apache.jasper.compiler.Compiler removeGeneratedFiles
WARNING: Failed to delete generated Java file [C:\Users\Documents\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\local host\ShopSystem-AWSJavaWebProject\org\apache\jsp\WelcomeCustomer_jsp.java]
No suitable driver found for jdbc:mysql://mySqlPath/ShopSystem
Please help.
Thank you in advance.
Your this line seems to be incorrect.
final String host = "jdbc:mysql://mySqlPath/ShopSystem";
It should be something like this :
jdbc:mysql://localhost/ShopSystem
or
jdbc:mysql://{YOUR_IP}/ShopSystem // e.g. "jdbc:mysql://192.168.15.102/ShopSystem"
The solutions I found for this error were "add the jar file via build path" and "put the jar file containing the driver in WEB-INF/lib" (and similar formulations) but I already tried the two of them.
Back to my problem. Here is my Code:
jsp-file
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page import="db.DBAccess" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="dba" class="db.DBAccess"></jsp:useBean>
<jsp:setProperty name="dba" property="selection" value="s" />
</body>
</html>
javabean
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBAccess {
public DBAccess() {
}
private String selection = "";
public void setSelection(String s) {
final String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
String user = "user1";
String pswd = "user1pswd";
Connection con = null;
Statement stmt = null;
try {
// initialize connection
Class.forName(driver);
con = DriverManager.getConnection(url, user, pswd);
stmt = con.createStatement();
// execute select
ResultSet results = stmt.executeQuery("SELECT * FROM t");
results.next();
selection = results.getString(1);
// close connection
stmt.close();
con.close();
} catch (ClassNotFoundException cnfe) {
System.out.println(cnfe.toString());
} catch (SQLException e) {
System.out.println(e.toString());
}
}
}
The content of t is one column with the label "string" and the content "hello world"
Why am I getting this exception even though I linked the jar file correctly?
Did you insert your jar file to lib folder of Webinf?