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
Related
When I run on tomcat server my java web application
eclipse-workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ProvaUseBean2\WEB-INF doesn't contain the directory classes that must contain all the BeanClasses that I use in my index.jsp
my project structure
my index.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<jsp:useBean id="classBean" scope="request" class="logic.bean.ClassBean"/>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
my ClassBean:
package logic.bean;
public class ClassBean {
private String myAttr;
public void setMyAttr(String s) {
myAttr = s;
}
public String getMyAttr(String s) {
return myAttr;
}
}
When I run on TomcatServer I receive this error:
org.apache.jasper.JasperException: /index.jsp (line: [3], column: [0]) The value for the useBean class attribute [logic.bean.ClassBean] is invalid.
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.
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";
}
}
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"
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?