getCodeBase() gives nullpointer in Java Applet - java

I have created an applet to read some info from a file on the server. I try to access the file using the following code:
Properties Settings = new Properties();
settings.load(new URL(getDocumentBase(), "settings.ini")).openStream());
All of a sudden, the second line is giving me the error:
java.lang.NullPointerException
at java.applet.Applet.getDocumentBase(Unknown Source)
My applet is signed and I access it through my localhost.Why can't I use getDocumentBase anymore?
Btw, I am using Netbeans Web Start option to create the necessary files (jars, html, jnlp) and then move them to my IIS local server.
SOLUTION
I'm loading the ini file from within the jar now:
Properties Settings = new Properties();
URL url = this.getClass().getResource("/myapplet/settings.ini");
settings.load(url.openStream());

At first glance I would expect:
new URL(getCodeBase(), "settings.ini")
as getCodeBase gives the directory URL, getDocumentBase gives the HTML URL.
That it worked previously is astonishing. Maybe the HTML URL ended with ?... and you read the HTML page?

SOLUTION
I'm loading the ini file from within the jar now:
Properties Settings = new Properties();
URL url = this.getClass().getResource("/myapplet/settings.ini");
settings.load(url.openStream());

Related

java.io.FileNotFoundException: db.properties (The system cannot find the path specified)

I have written a basic jsp code for storing and retrieving the data from db.
Before that am checking validation of user.
When i click submit button it will redirect to my jsp page.
i have written a db.properties file separately.
When i gave complete path to read properties file., program is executing fine. (Which is not best way to hard code like below).
FileInputStream in = new FileInputStream("C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ServiceDisplay\db.properties");
But when i specify only "db.properties" like
(FileInputStream in = new FileInputStream("db.properties");) program is not executing i got file not found exception.
Please not this properties file is in current working dir only. (i.e., my db.properties file and my jsp file is under ServiceDisplay
I tried to changing the file name as "//db.properties", "/db.properties", "./db.properties", "\db.properties", .\db.properties, ../db.properties", "..\db.properties" .
But still i am getting java.io.FileNotFoundException: db.properties
The file 'ServiceDisplay\db.properties' is in your resources directory, to load this file is necessary to use the getResoursceAsStream. Code example below:
ServletContext context = request.getSession().getServletContext();
InputStream is = context.getResourceAsStream("/db.properties");

Applet cannot recognize relative path on Tomcat Service?

I got some errors in my app. The app is using applet for accessing local file system.
converting file, for example.
First: Using relative path.
I can place project folder wherever I need and the application still working properly. However, when I placed it on the tomcat server directory, it cannot find the files' path.
Run with browser: file:///C:/report-param/index.html, ok
Run with browser: localhost:8080/report-param/index.html, error
Second: Using absolute path.
The application working well in both browser and tomcat server.
Run with browser: file:///C:/report-param/index.html, ok
Run with browser: localhost:8080/report-param/index.html, ok
Here is my sample code:
File jrxmlPath = new File("reports//people-report-withparam-multi.jrxml");
File pdfPath = new File("reports//people-report-withparam-multi.pdf");
String sourceJrxml = jrxmlPath.getPath();
String destPdf = pdfPath.getPath();
JasperReport jasperReport = JasperCompileManager.compileReport(sourceJrxml);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, paramMap,
getPostgreSQLDataSource(driverUrl, username, password));
JasperExportManager.exportReportToPdfFile(jasperPrint,destPdf);
What is the matter on here? Any ideas?
How can I fix the error above? Please help...
Note: if you don't understand, feel free to ask me...

Unable to Insert image to mysql database using servlet

Could anyone please help me out with the following?
I'm trying to insert an image to the blob column in mysql database through a servlet.
I'm selecting the image through the "HTML FILE INPUT TYPE" which is in a JSP file.So the full path of the image is selected.
The error I have been getting is:
HTTP Status 500 - Desert.jpg (The system cannot find the file specified)
type Exception report
message Desert.jpg (The system cannot find the file specified)
description The server encountered an internal error (Desert.jpg (The system cannot find the file specified)) that prevented it from fulfilling this request.
exception
java.io.FileNotFoundException: Desert.jpg (The system cannot find the file specified)
java.io.FileInputStream.open(Native Method)
java.io.FileInputStream.<init>(Unknown Source)
Image.ImgInsert.doGet(ImgInsert.java:51)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.28 logs.
Apache Tomcat/7.0.28
Where "Desert.jpg" is the image which is on my desktop.
This same program works on my friends computer.
Here is the servlet code:
String s_id = request.getParameter("myid");
int id = Integer.parseInt(s_id);
//IMAGE ACQUIRED FROM THE FROM THE JSP PAGE
String img = request.getParameter("myimg");
File f = new File(img);
FileInputStream fis = new FileInputStream(f);
String query="insert into images.imageinsert(id,image) values(?,?)";
try
{
PreparedStatement pStatement = conn.prepareStatement(query);
pStatement.setInt(1, id);
pStatement.setBinaryStream(2, fis);
int result = pStatement.executeUpdate();
if(result != 0)
{
response.sendRedirect("ImageHome.html");
}
pStatement.close();
Could anyone please help me out?
There are at least two serious conceptual mistakes here.
You seem to think that having the client side local disk file system path is sufficient to obtain the entire file contents in the server side. This is impossible as the client and server don't share the same disk file system (unless they both happen to run on physically the same computer, which of course don't occur in real world).
You're relying on relative paths in java.io stuff. It becomes relative to the so-called "Current Working Directory" which is the folder which is been opened at exactly that moment the webserver was started. This is definitely not the folder where the webapplication is directly sitting in. This is for example C:\path\to\tomcat\bin. The uploaded file surely isn't magically been placed in there.
As to why it works on the machine of your "friend", that's undoubteldly because he's using Internet Explorer which has a security bug wherein the full file path instead of only the file name is been sent as request parameter on an <input type="file"> field of a <form> without enctype="multipart/form-data". Again, as answered, this approach surely won't work in a real production environment.
Your concrete problem can be understood and solved by carefully reading the following answers.
How to get the file path from HTML input form in Firefox 3
getResourceAsStream() vs FileInputStream
How to upload files to server using JSP/Servlet?

Loading Properties files in an applet

New to Applets, I have never dealt with having to export the resources to the jar.
The browser is failing to load properties files:
access denied ("java.io.FilePermission"
"config\en-us.properties""read")
Properties files are imported as so:
Code to load Properties file:
prop.load(new FileInputStream("config/en-us.properties"));
Obtain an URL to the properties file in the jar using:
URL urlToProps = this.getClass().getResource("/config/en-us.properties");
Use an URLConnection to set a read timeout.
// courtesy of MyTitle 'default timeout is infinity'
URLConnection connection = urlToProps.openConnection();
connection.setConnectTimeout(5000);
Get an InputStream.
InputStream is = connection.getInputStream();
Then use Properties.load(InputStream) to load it.
prop.load(is);

Deployment in tomcat

i am getting a problem
i have deployed a war file, when i run localy through tomcat it works fine but when i run on another system by giveing my system ip and then project folder e.g
http:\192.168.0.145\DllTest it loads the applet but when i click on a button to load the functionality it is throwing an exception
Exception in thread "AWT-EventQueue-3" java.lang.UnsatisfiedLinkError: Expecting an absolute path of the library: http:\192.168.0.145:8080\DllTest\lib\jinvoke.dll
while it is working fine localy but not in another system. Please tell me what is the problem.
Is it a rights issue or something else.
You cannot load a DLL on an external host. It has to be an absolute disk file system -as the exception message already hints. Your best bet is to download it manually, create a temp file and load it instead.
File dllFile = File.createTempFile("jinvoke", ".dll");
InputStream input = new URL(getCodeBase(), "lib/jinvoke.dll").openStream();
OuptutStream output = new FileOutputStream(dllFile);
// Write input to output and close streams the usual Java IO way.
// Then load it using absolute disk file system path.
System.loadLibrary(dllFile.getAbsolutePath());
dllFile.deleteOnExit();

Categories