How does the Java getCodeBase() method work? - java

So I learned about the getCodeBase() method today in my programming class and to be honest, I'm really interested in it and how it works in general. All I know is that it finds the directory of where the source code is stored and it is mostly used in applets. But anyone explaining, in detail, how it actually works like the technicalities and such, I would be super grateful! By the way, I started Java 6 months ago. Thanks!
This is what I learned, just to put a little code in here.
cards[0] = getImage( getCodeBase(), "cards_gif/1.gif" );
getCodeBase() in this case, is used to initalize a part of an array to a image.

It returns whatever the codebase was that was specified in the HTML, or its default.

getDocumentBase()
Gets the URL of the document in which this applet is embedded.
For example, suppose an applet is contained within the document:
http://java.sun.com/products/jdk/1.2/index.html
The document base is:
http://java.sun.com/products/jdk/1.2/index.html
getCodeBase()
Gets the base URL. This is the URL of the directory which contains this applet.
I want to delete this post, but can't do it as it's accepted.

Related

Java ScriptEngine - Disable Importing

I've been searching everywhere and have found nothing, so I would like use Java's inbuilt JavaScript engine, but whilst users can't import java class, like any of the following:
importPackage(package.example);
or
importClass(package.example.MessWithEverythingClass)
or
new package.example.MessWithEverythingClass(ApplicationIsBroken aib);
I'm a little confused. Based off of your title I think you want to prevent users from importing classes inside JavaScript. In that case this question has already been asked here,
security problem with Java ScriptEngine.
Some of the solutions suggested there were to look into Java SecurityManager and another user said he solves this problem by putting "importPackage = null" on the top of all scripts.
On the contrary based on the contents of your script I get the feeling you are having trouble importing Java classes. If that's the case I answer that here,
Use a jar in JavaScript through Java ScriptEngine
Basically there is another way to import inside JavaScript that you did not show. I do it this way,
pack = Packages.abc.foo.pack.name;
var foo = new pack.ClassFromTheJarFile("arg1","arg2");
foo.doSomething();
var fooSister = new pack.AnotherCLassFromTheJarFile("arg1");
fooSister.doSomthingFromThisClass();
Although that example uses a package from a jar file it does not have to be from a jar and the code would still remain the same.

Programming Documentation

I have always had problems reading documentation for example
I was just researching about file paths for java and I so this :
"
Usage Example:
Path file = ...
BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
"
//http://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/BasicFileAttributes.html
What is ..... ??? :|
I know im not a expert but example code that is full coded would be nice, Or Is is it becouse im not at that
level yet...
I tried testing it..did not even run :(
Please Help
API documentation is meant to give you details about a particular class and serve as a reference to come back to. It's purpose is not to provide you with an explanation how concepts fit together. So ... stands as a placeholder for any code that might make sense in a particular use case. For a general overview Oracle hosts an excellent set of tutorials.
And if you want to see even a bigger picture, I can recommend an excellent book Thinking in Java.
The ... is called an ellipsis, and means that something has been left out there. On this case, it's a call to the Path constructor, putting a path in place ready for the rest of the code.
They do that because they don't want to clutter the example with extraneous stuff.
If you want fully coded examples, the Java Almanac has good ones, albeit a little dated.
http://www.amazon.com/The-Java%C2%BF-Developers-Almanac-Volume/dp/0201752808

Java - Trying to plan the layout of a programs classes

I'm having a bit of trouble figuring this out so if you can help that would be great.
I have written a bit of code that gets a list of URLs from a sitemap. I then go each page, scrape all the links, and then test their status(200,404, etc).
I am using HttpClient. I have it all working OK but as I am new to Java I reckon my code is a complete hack/maze and I could most likely get far better performance if it was organised correctly. So what I have is
Main class - This builds the gui
Parse the sitemap class - This parses the sitemap and get a list of the urls.
Class called PingURl - I'm sure my above is poor but this is the bit I reckon is worst. This class opens all the urls, scrapes them for links, then tests the links for their status and returns it. I presume this class should be broken down? Most importantly I think I should be isolating the testing of the links in it's own class, so it would be easy to implement threads later on?
Basically I'm looking for advice. If someone could help me with laying out the project a bit better. Secondly I believe this is my weakest area so to improve I need to learn more about this, I don't even know what to call this(design/layout problem?). Can you also recommend resources to learn more about this?
Java is a language which IMO, embodies good OO design. Designing with OO in mind is very effective.
http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/oo.html
In terms of your problem, I think it works pretty well. You are kind of following a Model-View-Controller pattern: http://www.codinghorror.com/blog/2008/05/understanding-model-view-controller.html
I can't tell you how to design your code, but I will tell you how I would do it if I were presented with the same problem:
I would get a class to represent a hyperlink. In that class there would be the hyperlink that has been scraped, and a getter and setter. As well as a boolean value and a ping function. That means that if I create a "hyperlink" object, then I can invoke ("ping") on that object because it is part of that class. That means that the ping function simply sets the boolean that represents connectivity.
That way, your parser basically gets a page, and for each link it finds, it creates a new "hyperlink" object based on your own "hyperlink" class. And puts it in an array. So once your parser executes you get an array of pointers to hyperlinks.
Then all you have to do is invoke the ping function on each one to see if it is there.
I think this design is the best because it scales from doing one hyperlink test to doing 1000 quite easily.
There is soooo much stuff out there about design, there are countless principals as well. There is never one absolute way to solve a problem. But the more experience you get, and the more you read up on design patterns and models the better you will get at it :)

Does a ActionScript/Flash equvialant to Java GetDocumentBase() exist?

Is there a Actionscript/Flash equivalent to Java's GetDocumentBase() Method?
I'm trying to figure out if I can create events depending on what url the flash object was loaded from.
If the object is loaded at www.Happywifi.com show a happy face animation. If it is loaded at
www.Happywifi.com/question/ Show a question mark face.
I'm interested in flash, it seems like more detailed animations are possible with flash that what we are using in Java.
For the love of God please don't respond with "Why are you doing it that way!". The platform we are working with has a few constraints.
Have a look at the LoaderInfo class (check out the docs)
You could use either the url property or the loaderURL property. Like this:
trace(root.loaderInfo.loaderURL)
You should be able to use the LoaderInfo class which has the loaderURL property.
Something like:
stage.loaderInfo.loaderURL
Which will give you a string of the URL from where the file was loaded. Then you can do your additional processing.
Note that this is not the URL from which the page that was contains this object, but rather the actual URL of where the .swf file resides.
Using flash vars might be another solution to your problem if you can embed different objects on the page depending on what you want.

getUrl using Java

Is there a way of getting the websites absolute URL (http://www.domain.com/) using Java? because I've googled a bit but I only come across with having to make 2 or 3 classes to create that function =/
Update:
The thing is I am trying to create a crawler that will give me some information and among that I'd like to get the URL of the webpage it's getting the information from. I'm developing this in JAVA and what I meant to say was that I was wondering if there was some getUrl(); or any method like that to get me the Url, because I know it can be done but I've only done it writing a whole other class to retrieve the url and then inherit it and use it further...hope it made it clearer
I'm assuming you just want the domain from a JSP, however you may find you need the entire URL including the prefix, domain, path and parameters. The easiest way to get this quickly is to use the Request object and build it. Have a look here for more info:
http://www.exforsys.com/tutorials/jsp/jsp-request-object.html
Here is Sun's API on the HttpServletRequest interface:
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServletRequest.html
The question is not really clear, but I'll make the assumption that you are trying to get the path from within a Servlet.
String realPath = getServletConfig().getServletContext().getRealPath(relativePath);
Could you be more specific? Your question states:
Is there a way of getting the websites
absolute URL (http://www.domain.com/)
using Java?
By "the website" which website are you asking for? I can see multiple ways of interpreting your question:
Given a URL, if there a way to get the hostname portion of it?
Given a relative path, how do you get the full path?
Within the context of a Servlet, is there a way to get the name of the deployed server?
etc...

Categories