Results 1 to 19 of 19
  1. #1
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Question getting started with mySQL

    I just spent a day trying to write from my Servlet, I now find on the Sun Forum that the app packaged in a dot war file writes to a temp location. Apparently, based on the report in the Sun Java Fourm, it will be necessary to write to some sort of database to get persistence across server restarts.

    I have already mySQL set up on the www server ( not local ) along with being able to find where I put the database name, the user name and the password for the user. I have between five and ten fields that are extracted from a servlet based "Web Application" as NetBeans calls it. All of the fields are either strings, or I will do the conversion to string as needful.

    I am about an hour short of becoming ineffective under all the new info. In paticular, the
    Java Code:
    Class.forName("com.mysql.jdbc.Driver").newInstance ();
    syntax ..... is that what I put in my code?...

    I am at MySQL Forge :: Dashboard and MySQL :: Developer Zone right now trying to get going. I need some orientation on where I am at and where I am trying to get to. All data items will be converted to a normal Java String before ......

    I'm so lost I do not have terminology available. It's mySQL, on the server where the web app is running, five or ten normal strings.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  2. #2
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    The setup of the JDBC connector is a bit strange. I'll have to grab some live code from my SVN repository to get a solid example for you. But the basic idea is:

    You use the class loader to load the JDBC class, using its name, once. That causes a miracle to occur. From then on, you can do things like get a Connector to the database, create a Statement, do an execute() to do a select to get a ResultSet.

    Its really a silly design. You never have to use the object you create, you just have to specify it before you do anything.

    Let me go find some sample code....

  3. #3
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Lightbulb H2 is hot....

    worked for awhile on this, then slept the morning away due to value of cool weather in hot climates. Read some ( as much as I could ) comments in all of the sources for java.sql -> == Driver ( as a class in the source code ) is instance ( 'tized ) by a static initializer in a very self-referential declaration.

    Really, not unlike the basic way one hangs a machine.

    One then exposes the class getInstance() by returning the - get this! - the instance created in the static initializer ... I bet we could really have some fun with 16-bit pointers,... better yet let's do some eight byte pointers with a file type that piovts world empires on the 8.3 filename inversion. Wow, we could have 2,000 pound elephant calves on twenty dollar tricycles wearing clown hats and sell tickets!

    Magical invisible pointers, 5 bucks get you in and you can never leave!

    Think I am gonna work on H2 to start with == they need some gui to front for the console. Ever heard of Dirigible Beer Interface? An auto-generator for java code generation that takes a mathematical expression of some kind and distributes Integer.MAX_INT <-> Integer.MIN_INT across n-switch cases accoding to the mathematical formula provided. We would need some work on IEEE remainder problem to implement the design reliably.

    This H2 guy is HOT_POTATOE, look in http://www.java-forums.org/search.php?do=getnew or Java Forums - Search Results

    Maybe we can get away from Tabby the Cat for a change.
    Last edited by Nicholas Jordan; 07-19-2008 at 11:02 PM.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    I am about an hour short of becoming ineffective under all the new info. In paticular, the
    Java Code:
    Class.forName("com.mysql.jdbc.Driver").newInstance ();
    syntax ..... is that what I put in my code?...
    Just as a note, the newInstance() is completely unneccesary. Also, if you are writing an Enterprise application, it will be running in a container/server that provides Connection Pooling resources, so that line should never occur in your code, but rather, you should be using DataSources.

    See
    The J2EE(TM) 1.4 Tutorial
    more specifically
    Database Connections

    And, for the most recent
    - The Java EE 5 Tutorial
    And
    <tt>DataSource</tt> Objects and Connection Pools - The Java EE 5 Tutorial

  5. #5
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Exclamation my own code

    Quote Originally Posted by masijade View Post
    Enterprise application,
    I have two ends, my own local machine upon which I will eagerly dig through ten thousand lines of code and build the entire jvm from sources if I absolutely have to and the open wire on which I will have to do as you are instructing me.

    I have totally had it ten times over with getting s#####d by people who should know better. People with plenty of experience and degrees who have an agenda. Not my agenda. The link you cite goes to Derby. Load times there are measured in seconds, I measure time in milliseconds: About 1/75th second to 1/85th second for something to respond or I am under attack ~ no questions, no exception trapping.

    I am not trying to be difficult, I just went through a website where the author has 3,700 pages in html 1.1 / 1.0 and not one ounce of scripting or throttled responses.

    Please do not take this as bad manners. There is a discussion of the matter under The Java Community Process(SM) Program - JSRs: Java Specification Requests - detail JSR# 282 which I cite because that is the work of professionals. I am not degreed.

    Quote Originally Posted by masijade View Post
    it will be running in a container/server that provides Connection Pooling resources, so that line should never occur in your code, but rather, you should be using DataSources.
    I will be doing my own connection pooling, for the above cited reasons.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    First of all, I was simply pointing out that newInstance was not needed. Secondly, you mentioned Servlet, so, of course, since you didn't say anything else about it, I assumed you would be using some sort of servlet container or enterprise application container, so I pointed out that they all have a Connection pooling resource so building your own connections was, also, not needed (and, usually, counter productive).

    The links I sited, went to Sun, they may use Derby as the example db (and probably do now that Sun distributes Derby with JDK 6, earlier they used pointbase, even worse) in those links, but that makes no difference, the principle (as long as the db driver is built to the JDBC specs) remains the same regardless of the db.

    If you do not want to use the resources that are already provided, more power to you. But, then, either state that before hand, or at least don't take offense when it is pointed out to you when you don't state that. Maybe that is a large part of the reason that you "have totally had it ten times over with getting s#####d by people who should know better". Not once, in your entire post, did you actually state you were attempting to write your own servlet container or anything else of that sort. Nor did you state anything that had to do with any kind of realtime Java server (which is what that link is about, not anything to do with DBs which was what your original question was about).

    No one was either putting you down, or attempting to force anything on you. If you want to take it that way, fine. And I have no agenda, where in my post is anything even close to resembling an "agenda".

    It seems to me that you have an "agenda" against anything that, to you, even seems to resemble an agenda.
    Last edited by masijade; 07-20-2008 at 11:22 AM.

  7. #7
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Smile Thank you for being explanatory.

    I woke up hoping we would work the issuses and I am going to work the details, you are obviously smart enough to grasp the value in what I said.

    First, mostly to get it out of the way: Agenda, as a term generally without context, is an erudite dismissal when I use it. Your last remark is 100% exactly, neither to much nor too little, what the term carries in my think. At 180 knots on final to LAX 24-R all agendas would have been tucked away long ago. It's just my nature, I will walk a mile rather than ride.

    RE: counter-productive && servlet-container && newInstance: Were gonna spin if I try to do too much with this, it gets back to why I did not put it all in the first post. Let me simplifiy by saying I will spend at least part of a day here, starting with building my own .dll. ~ it will be 32-bit pointers everywhere, and if needful I will begin a new language Toba. I am sure someone has already taken that name and as well I will never dig all the documentaion needed to get the networking to run .... but where I plan to start is H2 just ceases to function after awhile. That is really obvious to me what is going on, we get into systems programming and that will not vis-a-vis day to day operations in shops that do not have the expertise, time and budget to write their own os.

    To try to cover everything would be too much, let me ask: It appears to me what the system is doing here is pushing the enitire transaction through a 16-bit stack. iow in some code, SS==DS==ES at all times; Sorry if I lost you but I am used to dealing with ftr and these things are obvious to us. For others, building class myBeginnerIdeas extends java.sql.Driver so that the effort ( db driver ) is built to the JDBC specs is just a Section 8 ( attracts uninformed critcal rebuke )

    If you do not want to use the resources that are already provided, more power to you. But, then, either state that before hand, or at least don't take offense when it is pointed out to you when you don't state that.
    Let's use the energies productively. The fire so far can be used to diffract the nuances. I did take offense, but that is just to shake off people who aren't even trying. I had noted in your posts a full featured thread of comprehension of the details so rather than trolling for phd's as it may appear; What I was doing { totally flat and no skimming involved } is have you fire on me with the main barrel so that I could see what really needs work ~ thus avoiding a lot of wasted beginner effort digging through "Latest Software" sites that don't know a drill from a diviot. Perhaps you can tell me where to look in the h2 source tree to begin work, that is the ultimate root intent of my response to you.

    Nor did you state anything that had to do with any kind of realtime Java server (which is what that link is about, not anything to do with DBs which was what your original question was about).
    ( message edit: what's the difference? DB's aren't real? - no pun intended )

    I do not have the nomeclature to be effective. What I am trying to do is I have a machine, local, on which I can build and run anything I can comprehend. There, I have a well devloped style that allows me to maintain total control. No one does any work on that box, passwords I use would not be believed by you:
    Java Code:
    public class PassTheWord
    {
        private static final Integer MIN_PASSWORD_LENGTH = 0x0f;
    }
    I just wrote that, just now as I type this. In a moment I will use something from javax.crypto and map the generated bytes to ascii ' ' through '~'

    On the open wire, again I do not have the nomenclature to directly convey. We may have a round or two determining exactly what I am trying to say:

    There, the www server runs Tomcat. I spent good effort trying to persist data across server restarts. That's how I ended up here, I cannot write a servlet container to be deployed there. I have to use accepted practices. That may involve ineffectiveness as I see it, but it is incumbent on me to write my own java app locally that will retrieve all the data and archive it locally. This may run as an unattended background process. The server uses mySQL - that's all I really know about it. No one who uses the website will even know of the existence of the database, I am using this as it is the accepted convention and thus should harmonize with all their administration tools.

    They would like my password generator.
    Last edited by Nicholas Jordan; 07-20-2008 at 09:14 PM.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Does this relate to your post where you were attempting to write to WEB-INF?

    Would you prefer a solution that allows you to do that?

    In that case (as long as your app is not contained within a jarfile on the server, as then you won't be able to write anything into that jarfile anyway), you can reliably find the path to your WEB-INF directory (as a file: URL, which you can then use in new File using the toURI method) as follows:

    Java Code:
    this.getClass().getResource("/" + this.getClass().getName().replaceAll("\\.", "/")).replaceFirst("WEB-INF/.*$", "WEB-INF/");
    Obviously only needs to be done once and the result saved (maybe write a ServletContextListener and get the path at application start and save it to the application context, and another to save the data there on application shutdown). That will, reliably, get you the system path to your WEB-INF directory.

    As far as how the DB connection stacks work, I don't know, have not really looked into (have not needed to, and have not really been all that interested in it).

    And, as a note, as far as DB's being real, yes they are real, but normally not "realtime" (although they may contain a realtime thread, but it will only do a very small amount of real work, mainly just scheduling/coordination issues, maybe). Realtime processes can play real havoc on a sytems scheduling capabilites if they use too much CPU, which about any DB will (as I'm sure you know).
    Last edited by masijade; 07-21-2008 at 12:39 AM.

  9. #9
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Thumbs up This will work

    Quote Originally Posted by masijade View Post
    Does this relate to your post where you were attempting to write to WEB-INF?
    This is the same coding challenge, virtual zero difference.

    Quote Originally Posted by masijade View Post
    Would you prefer a solution that allows you to do that?
    I am trying to achieve the same goal, I am not sufficiently well informed to provide you an answer. As long as I can do a write on session transaction ( iow customer goes through several steps, I have burdens under Sarbanes-Oxley and common sense ) then I will not be picky about how we go about that.

    Quote Originally Posted by masijade View Post
    In that case (as long as your app is not contained within a jarfile on the server, as then you won't be able to write anything into that jarfile anyway), you can reliably find the path to your WEB-INF directory (as a file: URL, which you can then use in new File using the toURI method) as follows:
    I have it both ways now, I will delete the jar file or whatever else is needful. May need additional instructions.

    Quote Originally Posted by masijade View Post
    Obviously only needs to be done once and the result saved (maybe write a ServletContextListener and get the path at application start and save it to the application context, and another to save the data there on application shutdown). That will, reliably, get you the system path to your WEB-INF directory.
    I will have to study your regex, it looks like you are,... shakey here but it looks like we are putting WEB-INF at the end and replacing backslashes with forward slashes.

    This is several steps ahead of where I am at ..... I will have to study this and ask a few questions so that I do not blunder in unknown ways trying to use it. Basically what I hear you telling me is I can run this once and see what it is produciing - then copy paste into the sources that will run on the server. As well I could place it in init() and save it somewhere in a static string. I did note that on the Tomcat installation ( on local ) the files were still visible after shutdown. Just because it wrote them on local is not sufficient when we are talking about real people and so no.

    Quote Originally Posted by masijade View Post
    Realtime processes can play real havoc on a sytems scheduling capabilites if they use too much CPU, which about any DB will
    Realtime DB's require specialized hardware. I am sure it is the line of code you provided that will solve my moment. I will test the code today. I am lost trying to read it, I will be running blind bluff.

    Realtime here is real people, real world, that cannot be blind-bluff. Does not have to be true realtime. just real world.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    I will have to study your regex, it looks like you are,... shakey here but it looks like we are putting WEB-INF at the end and replacing backslashes with forward slashes.
    Well, actually there's two replaces. getResource requires the package path to be represented with forward slants, but getName gives you the package path with ".", so the first one replaces those with / (and added a leading / before that).

    The second one is simply stripping everything off the URL that getResource returns that comes after WEB-INF, thereby leaving you with a file: URL path to WEB-INF.

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Sorry I made a mistake (I can only claim temporary insanity). getResource returns a URL, not a String, so we can't, of course, use replaceFirst on that directly. Two choices, one use toString to get the URL as a String and use replaceFirst and create a new URL from that that we can then create File item with using toURI.
    Option 2 is to create a File directly, then simply walk the path backwards until we come to the WEB-INF directory. I will display both here

    Choice 1:
    Java Code:
    URL url = this.getClass().getResource("/" + this.getClass().getName().replaceAll("\\.", "/"));
    String s = url.toString().replaceFirst("WEB-INF/.*$", "WEB-INF/");
    url = new URL(s);
    File webinfPath = new File(url.toURI());
    if ((!webinfPath.exists()) || (!webinfPath.isDirectory())) {
      // error
    }
    Choice 2
    Java Code:
    URL url = this.getClass().getResource("/" + this.getClass().getName().replaceAll("\\.", "/"));
    File webinfPath = new File(url.toURI());
    while ((webinfPath != null) && (!webinfPath.getName().equals("WEB-INF"))) {
        webinfPath = webinfPath.getParentFile();
    }
    if ((webinfPath == null) || (!webinfPath.exists()) || (!webinfPath.isDirectory())) {
      // error
    }

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Aargh, sorry again, I'm a real winner on this one. In the getResource call, you need to add .class to the search string.
    Java Code:
    URL url = this.getClass().getResource("/" + this.getClass().getName().replaceAll("\\.", "/") + ".class");

  13. #13
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Exclamation temp dir gets stirred

    You can only claim temp ins..., I can demonstrate it!.... ( yuk-yuk )

    I am gonna be swamped for awhile anyway, I have a clear idea where I need to go with this so let me google awhile, just don't worry about writebacks right now - I have a clear design approach for the backfeed issue:
    Java Code:
    Incoming ->|
               |Inbound -> ( incoming data or whatever ) 
                     |Incoming ->|
                                |
    At no time do we try to backfeed this, we just let the acceptor feed and try to write it to a buffer. The fact that we cannot actually get a deteministic value for system issues is known.

    We let this cascade and do a separate reader thread, manually doing admin. Small-shop security model, two sigma reliability avaliable by manual methods already used as primary tool. Your gonna rack your brain trying to delve into issues that are buried, I have strong skills working something like this and have not even found where I put your original. Will be busy for awhile.

    message edit: WEB-INF/.*$" may have issues: bug_id=4527731
    message edit: first work-> why the slash? .getResource("/" + translated name + ".class")
    Last edited by Nicholas Jordan; 07-21-2008 at 09:58 PM.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  14. #14
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Smile preliminary code

    I did not attempt a compile, I saved last version I saw in single line comments at end of work. Please let the variable naming convention be proof of concept for proposed sanity management issues being my domain, not yours.
    Java Code:
    import java.net.URL;
    import java.util.regex.*;
    /*
     *  This is for people who want the comments to line up: 
     *
     *  "No rest for the wicked, no mercy from the cruel."
     * 
     *  For explainations, see  - 8.417 ( para 43.4 ) General Clarifications, as amended.
     */
    public class TestFind
    {
        //
        String nameString;
        // 
        URL MerlesURLs;
        //
        static String filePath;
        //
        TestFind()
        {
            // Okay, this should be our name.
            String string = this.getClass().getName();
            string.replaceAll("\\.", "/"));
            ClassLoader ToadieTheRoadie = this.getClassLoader();
            if(ToadieTheRoadie != null)
            {
                Patten pitterPatter = Pattern.compile("/( \\w+ | / )+WEB-INF/(?:.*)");
                Matcher meuford = pitterPatter.matcher(string);
                try
                {
                    //
                    MatchResult matchResult = meuford.find();//MerlesURLs
                    MerlesURLs = new URL(matchResult.group());
                }
                catch(IllegalStateException miseAreMice)
                {
                    ;// Tim, right below here I have stored some code using 
                    ;// single line comments as temporary buffers so that I 
                    ;// not lose it. This, working with compilers that do not
                    ;// warn about empty statements is why I so often have //
                    ;// in empty combination. It runs at about 100 miliseconds.
                    ;// That is what I have to have to stay ahead of idiots on
                    ;// Boot Hill. 
                }
            }
            // this
            // .getClass()
            // .getResource("/" + this.getClass()
            // .getName()
            // .replaceAll("\\.", "/") + "
            // .class");
            
        }
    }
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  15. #15
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Some suggested changes.

    Quote Originally Posted by Nicholas Jordan View Post
    I did not attempt a compile, I saved last version I saw in single line comments at end of work. Please let the variable naming convention be proof of concept for proposed sanity management issues being my domain, not yours.
    Java Code:
    import java.net.URL;
    import java.util.regex.*;
    /*
     *  This is for people who want the comments to line up: 
     *
     *  "No rest for the wicked, no mercy from the cruel."
     * 
     *  For explainations, see  - 8.417 ( para 43.4 ) General Clarifications, as amended.
     */
    public class TestFind
    {
        //
        String nameString;
        // 
        URL MerlesURLs;
        //
        static String filePath;
        //
        TestFind()
        {
            // Okay, this should be our name.
            String string = this.getClass().getName();
            string.replaceAll("\\.", "/"));
            ClassLoader ToadieTheRoadie = this.getClassLoader();
            if(ToadieTheRoadie != null)
            {
                // Without the call to getResource, you have only a classname with package, not a path
                URL url = this.getClass().getResource("/" + string + ".class");
                Patten pitterPatter = Pattern.compile("file:/(\\w+|/)+WEB-INF/(?:.*)");
                // without including "file:" the resulting URL has no protocol
                // and the spaces must be removed, or they would attempt to be matched.
                Matcher meuford = pitterPatter.matcher(url.toString());
                try
                {
                    //
                    if (meuford.find()) { // find is a boolean method (no MatchResult)
                        MerlesURLs = new URL(meuford.group());
                        // Just to show how to use this.  ;-)
                        filePath = (new File(MerlesURLs.toURI())).getAbsolutePath();
                    }
                }
                catch(MalformedURLException pennyPinchers)
                {
                }
                catch(IllegalStateException miseAreMice)
                {
                    ;// Tim, right below here I have stored some code using 
                    ;// single line comments as temporary buffers so that I 
                    ;// not lose it. This, working with compilers that do not
                    ;// warn about empty statements is why I so often have //
                    ;// in empty combination. It runs at about 100 miliseconds.
                    ;// That is what I have to have to stay ahead of idiots on
                    ;// Boot Hill. 
                }
            }
            // this
            // .getClass()
            // .getResource("/" + this.getClass()
            // .getName()
            // .replaceAll("\\.", "/") + "
            // .class");
            
        }
    }

  16. #16
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default two details

    I am about to try this code in the project, it looks like my regex pattern was not bad, but I need to get some additional details while I am doing my trials:
    Java Code:
    // Without the call to getResource, you have only a classname with package, not a path
    I have never seen this before, do not know where to start so as to know what to ask. classname and package and file directories seem somewhat interchangable. There is some preliminary and fundamental Intro to cs 101 that I have not been given on what this is and what is going on. IOW - how this works in the overall. Possibly I might ask what exactly is it that getResource is returning.

    I managed to discover yesterday that a class is basically an ordered stream of bytes. A file can be defined that way also. When we get resource, what are we getting? ( here, in the context of this post and what we are working on )

    Also, does the prepended / have anything to do with file:/// ( three slashes ) being displayed in the browser?
    Java Code:
    // and the spaces must be removed, or they would attempt to be matched.
    Huh?.... ( iow what spaces where? )

    Rest of it is basically as expected, some details were skipped so that we could move forward.

    message_edit: except for a few diagnostics I understood I only have one error to resolve:
    Java Code:
    symbol: method getClassLoader()
    location: class kitty.Arachnida
            ClassLoader ToadieTheRoadie = this.getClassLoader();
    Last edited by Nicholas Jordan; 07-22-2008 at 11:54 PM. Reason: first compile
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  17. #17
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Smile fix accepted by compiler

    Java Code:
    ClassLoader ToadieTheRoadie = this.getClass().getName().getClass().getClassLoader();//
    The compiler seems to accept this fix for the edit in previous post "two details"

    I have no idea where I am at.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  18. #18
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Well, the classpath starts from the "package root". I.E. if you have your classes in a jarfile then "/" will be the jarfile itself and the rest of the package path is from that point on. getResource returns a URL (jar:file: URL when in a jarfile, file: when in a directory on the system, and some other protocol when loaded through RMI and the like) representing the full physical path to the class file (or other file that you are looking for). You enter only the package path (and it can be relational) as the parameter, and receive the physical path.

    As far as the regex, you had
    Java Code:
    "( \\w+ | / )"
    I changed it to
    Java Code:
    "(\\w+|/)"
    but it should actually be (this is what I tested, but entered wrong in my post)
    Java Code:
    "(\\w|/)+"
    In this way any number of 1 or more word characters or /'s are matched.
    Your way only the following two possibilities are matched:
    1. a space followed by 1 or more word characters followed by a space
    2. a space followed by / followed by a space

    Also, I believe you need to simply leave the
    Java Code:
    (?:.*)
    part of the regex off. If it is there, the entire string is matched, but you only want to match up to the "WEB-INF/" part.

    Yes, the file: is the beginning of the URL, and it needs to be there, or the new URL will throw a MalformedURLException.

    Also, you need this.getClass().getClassLoader(); which will retrieve the ClassLoader that loaded your class.
    this.getClass().getName().getClass().getClassLoade r(); Will get the System ClassLoader, since that is the ClassLoader that loads the String class, and a String is what getName() returns.

    Edit: Also, if the server is a windows machine, you may want to change that regex part to
    Java Code:
    "(\\w|/|%| )+"
    to make sure to catch spaces (which might be represented by %20) in the URL of the physical path.
    Last edited by masijade; 07-23-2008 at 01:13 AM.

  19. #19
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Exclamation second attempt

    I am in over my head, I did the best I could. I will begin preliminary testing on local today. Local is Tomcat on Win, deploy is Tomcat on one of the Unice, probably Linux.
    Java Code:
        // Okay, this should be our name.
        String string = this.getClass().getName();
        string.replaceAll("\\.", "/");
        // Normal sanity check.
        if(string != null)
        {
            // Here, I am at your mercy. I have no idea how antecedent code in Java history affects the construction
            // of this line of code. I have put it here exactly as shown, but we know there is a deep well ( lurking? ) .
            java.net.URL url = this.getClass().getResource("/" + string + ".class");
            
            // Backtracking is not super-linear as we know what we are looking for?
            // IOW one or more of \w / % or space, followed by WEB-INF/
            // I assume that because this may go past "WEB-INF" then backtrack we could consider trying to stop on
            // "WEB-INF" but doing that in an NFA would probably introduce undetectable special casing isssues.
            java.util.regex.Pattern pitterPatter = java.util.regex.Pattern.compile("file:/(\\w|/|%| /)+WEB-INF/");
            
            /****** here we go...  ******/
            
            // Here, what we have done is try to get a URL we can use in code to find where I can look for data files
            // that I write when someone accesses the Servlet. This I can write a custom Java program locally that 
            // will pull my web activity reports ( custom written in Servlets ) from the commercial www server. 
            java.util.regex.Matcher meuford = pitterPatter.matcher(url.toString());
            try
            {
                // If we captured state of where we are at ....
                if (meuford.find()) { // find is a boolean method (no MatchResult)
                    MerlesURLs = new java.net.URL(meuford.group());
                    isValid=Boolean.valueOf(true);//
                }
                // Otherwise we set a value that puts us in diagnostic and tracing during development and recovery.
                else
                {
                    isValid=Boolean.valueOf(false);//
                }
            }
            catch(java.net.URISyntaxException SinTaxException)
            {
                isValid=Boolean.valueOf(false);//
            }
            catch(java.net.MalformedURLException MalEarly)
            {
                isValid=Boolean.valueOf(false);//
            }
            catch(IllegalStateException miseAreMice)
            {
                isValid=Boolean.valueOf(false);//
            }
        }
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. JSP Getting Started.
    By sukatoa in forum JavaServer Pages (JSP) and JSTL
    Replies: 7
    Last Post: 07-21-2008, 03:08 AM
  2. Applet not getting started
    By Preethi in forum New To Java
    Replies: 0
    Last Post: 05-27-2008, 04:43 PM
  3. How do I get started?
    By programmer-in-training in forum New To Java
    Replies: 4
    Last Post: 01-11-2008, 08:45 AM
  4. Getting Started
    By Doorsmaniac in forum Java Applets
    Replies: 0
    Last Post: 11-24-2007, 04:40 AM
  5. I need help getting started
    By Android in forum New To Java
    Replies: 2
    Last Post: 10-30-2007, 05:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •