help with java servlets

Discussion in 'Mac Programming' started by twoodcc, Mar 17, 2008.

  1. macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #1
    so could someone point me in the right direction of getting started with java servlets on leopard?

    i have netbeans installed, but i'm having trouble getting everything to work.

    also, i need help with connecting a database to my servlet and all

    thanks in advance
     
  2. macrumors demi-god

    fishkorp

    Joined:
    Apr 10, 2006
    Location:
    Ellicott City, MD
    #2
    My prefered method is Eclipse as my IDE and using Tomcat to serve up the servlets. OS shouldn't matter, I've used the same setup in OS X, Windows, and Solaris.

    As for database, I'm a Spring/Hibernate man.
     
  3. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #3
    thanks for the reply. i've heard of Eclipse before. i might give it a try. Netbeans also uses Tomcat, and runs it all together kinda.

    but coming from someone that's really new to servlets, could you give me a link to some examples?
     
  4. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #4
    ok i'm still having trouble with this.

    i need help getting tomcat to work, either in netbeans or running on its own

    i also need help connecting to a database. but i'm trying to make a servlet that is a webapp. can i do this in xcode? i see in there a "java web start" app. what is that?
     
  5. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #5
    Hi

    Sorry, I don't use Netbeans or Eclipse. I might be able to help you with Tomcat and servlets though. How far have you got with installing Tomcat?

    If you have an Intel Mac and VMWare Fusion, you might be better off installing a Linux distribution and setting up Tomcat running in a virtual server. That way you can mess around with Apache, Java and Tomcat settings without worrying about upsetting your Mac.

    I'm not sure what you mean by turning a servlet into a webapp. Servlets are designed/intended to run in Tomcat on a web server, not in a browser on the client.

    b e n
     
  6. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #6
    thanks for the reply.

    okay. i was trying to install Tomcat, but i never got it to work, so i gave up on it when i installed netbeans, b/c netbeans installs it in the IDE (hope that makes sense)

    i do have an intel mac.

    sorry, i guess i just mean a servlet that is controlled by a html file

    so what's the best way to install tomcat? i've tried before and i couldn't get it to work
     
  7. macrumors newbie

    Joined:
    Jul 2, 2007
    #7
    First off, I wouldn't use XCode to write a Java servlet unless my only other option was TextEdit. It's a decent IDE for Objective C and CoCoa, but there are more than a few IDEs that blow it out of the water for Java web development.

    With that said, I think you're on the right track with NetBeans. Personally, I use IntelliJ IDEA, but NetBeans is a great choice, as is Eclipse. You'll probably find more in the way of tutorials and examples if you go with Eclipse, but you seem to be making some progress with NetBeans, so I have a few more pointers given that fact.

    Tomcat is nice, but the full version of NetBeans for Java Web Development comes with Sun's Glassfish application server built in. Glassfish is pretty light, and pretty comparable to Tomcat, plus you have the nice integration already in place.

    If you're looking for a free database, you'll probably want to grab MySQL and learn how to get it running and connect to it using JDBC. If you are hosting on a Mac, a nice program for this is MAMP. It is a nice little app bundle that lets you easily create and run MySQL DBs on your local box.

    There are a lot of configuration steps to coding and deploying a servlet, so I would say you're best bet is to search Google for a Hello World servlet tutorial. Get that running, learn and understand what all the configuration steps mean, and then focus on the actual functionality of your own servlet.

    Hope this helps
     
  8. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #8
    thanks for the reply.

    i am taking a java class, and we have just started servlets, and i'm a little lost (which is why i'm posting this). i will google for a tutorial, but if you or anyone else could also help me with making a servlet, i'd appreciate it.

    basically i'm using a java servlet, html page, xml file, xsl file, and a database
     
  9. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #9
    maybe it would be better to post about an assignment that i've been working on.

    it has 5 files: a servlet file, xml file, xsl file, database, and html file

    when i run the program in netbeans, i get a blank html file. that's b/c when i created the project in netbeans, i created a web app. i then added these files in.

    anyways, i can run the html file, but when i hit the submit button on the form, i get an error saying that the servlet file was unavailable. any ideas?
     
  10. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #10
    Have you checked that Tomcat is actually running? You can check with something like http://localhost:8080.

    Also, it might be worth checking that you have a WEB-INF directory. It should have a conf subdirectory with your compiled servlet classes. Also in the WEB-INF directory there should be a web.xml file. This file describes the servlet's name, any start up parameters etc.

    b e n
     
  11. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #11
    see, with netbeans, tomcats runs when netbeans is running.

    i do have a WEB-INF directory.

    now how do i tie-in the servlet with the html file?
     
  12. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #12

    Well the servlet will be associated with an url. So somewhere in your html you will access it's url, eg as the action of a form element.

    b e n
     
  13. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #13
    okay i see that part. but what should my url be? the file path?
     
  14. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #14
    Okay, you need to look at web.xml in WEB-INF. That's where you define what the url to the servlet is. Like I said, I haven't used Netbeans so can't comment on whether it manages web.xml for you. But if it doesn't then you can edit web.xml directly. You'll need see some tags, eg:-

    Code:
    <servlet>
            <servlet-name>garage</servlet-name>
            <servlet-class>garage</servlet-class>
     </servlet>
    
     <!-- servlet mappings -->
    
         <servlet-mapping>
            <servlet-name>garage</servlet-name>
            <url-pattern>/garage/*</url-pattern>
        </servlet-mapping>
    
    In the first part you declare your servlet by giving it a name and the name of the associated java class. In the second part you define what the url to the servlet is.
    So in this case you would access the servlet by any of the following:-

    www.your-website.co.uk/garage
    www.your-website.co.uk/garage/more/paths/something

    The * after the garage means everything after /garage is passed as path info to your servlet for you to do with what you want.

    Probably best if you follow some well tutorial on the web on setting a servlet up. The above is a pretty minimal description of what you'll need.

    b e n
     
  15. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #15
    thanks. that does help. netbeans kinda does it for me, and kinda doesn't. but i think i got the html looking for the servlet correctly now.

    so instead of getting the "can't find server" error, now i get a 505 error saying that the server encountered a problem and couldn't process my request.

    any ideas? should i post some code in here?
     
  16. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #16
    Is the servlet class file(s) in WEB-INF/class directory, ie your compiled Java files should be in WEB-INF/classes? You can also check Tomcat's log files for errors. But it sounds to me like Tomcat isn't configured. You need to tell Tomcat about your site. In the Tomcat directory there should be a config file. I can't remember offhand the name of the file you need to edit. But it's another xml file, and in it you define a server for your site.

    However, this all sounds a bit too much like hard work! I would imagine Netbeans handles all the configuration of Tomcat and web.xml for you. Are you sure you've selected the right project type in Netbeans, and set all the preferences for servlet development? Before you start configuring Tomcat by hand you might want to get some help/guidance from somebody who has Netbeans experience!

    b e n
     
  17. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #17
    yes, the compiled java class is in WEB-INF/classes.

    yeah, it would be better to have someone who has used Netbeans. but you have helped me so far! thanks for your help.

    i'm sure we could figure this out, if you don't mind to continue to help
     
  18. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #18
    The directory which WEB-INF is in will be the document root for your test (web) site. Does the directory have a default page, eg index.html or default.html etc? You can quickly see if Tomcat is working. Enter this url into your browser http://localhost:8080 You should get something, either a Tomcat test page or your default page. Do you know what url Netbeans defines for you to access your test site? Tomcat usually listens on port 8080 so your problem may simply be that you are missing :8080 in your url.

    If none of the above seems to work then you can check your Tomcat configuration file. Look in the Tomcat installation directory (mine is in /usr/local/tomcat) for a file called server.xml. See if there is a host defintion for your test site. It will look something like:-

    Code:
    <Host name="domain-name" appBase="/home/your-site>
                    <Context path="" docBase="." reloadable="true"/>
    </Host>
    
    Where domain name might be a made up domain name, an ip number or localhost and appBase is the path to your test site's root directory. I'm guessing Netbeans will have set this up for you… fingers crossed!

    b e n
     
  19. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #19
    well tomcat is working. it uses port 8084 with netbeans though.

    the directory where WEB-INF is in is called "web" and my html file is in there.
     
  20. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #20
    Ok, so do you know what url you need to use to access your site? Can you access your html file through the url?

    Might be an idea to post your web.xml file.


    b e n
     
  21. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #21
    ok. the name of the html file is: PizzaPointeXML.html - it is located in the "web" directory, same as WEB-INF

    the java class is pizzaXML.class - it's located in "classes, which is in WEB-INF

    now netbeans handles the web.xml file kinda weird. its like a form that i fill out - kinda. there are tabs, and one of the tabs is "servlet". so in there, it asks for servlet name, and i put in pizzaXML. it also has a "servlet class" option, and i browse for file. i found the pizzaXML.class file and selected it. then there's the url pattern. i just put "/pizzaXML/" what should i put here?
     
  22. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #22
    I think you can put anything for the path to your servlet. The leading / means it's from the root of the url that you use to access the site. If your servlet doesn't process any path info after it then you could use "/pizzaXML". It doesn't really matter what name you use, "/hello" would work too. If you want the servlet to process the url path after it then use "/pizzaXML/*".

    b e n
     
  23. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #23
    well i'm using "/pizzaXML" but it's still not working. i noticed in the web.xml file that there are other servlets in there. should i remove them?
     
  24. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #24
    Hi, I wouldn't move anything. What url are you using to access the servlet?
    b e n
     
  25. thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #25
    under form i have: name="pizzapointe" action="pizzaXML" method="get"
     

Share This Page