Results 1 to 9 of 9
  1. #1
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default Placing java code/html pages on website host

    I wanted to try something really simple. I placed my java code into my public_html folder along with a html page with the code:

    Java Code:
    <form action = "MyFirstServlet" method = get>
    and my Java code is:

    Java Code:
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class MyFirstServlet extends HttpServlet
    {
    	protected void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException
    	{
    		response.setContentType("text/html");
    		PrintWriter out = response.getWriter();
    		out.println("<HTML>");
    		out.println("<HEAD><TITTLE>My First Servlet</TITLE></HEAD>");
    		out.println("<BODY>");
    		out.println("<H1>My First Servlet, December 2008</H1>");
    		out.println("</BODY>");
    		out.println("</HTML>");
    		out.close();
    	}
    }
    When I open the html page, nothing comes out though. Do I have to put the Java code in another location or something? It's supposed to display "My First Servlet, December 2008" right when I open the html page. Don't pay to much attention to the code, I picked this out of a textbook to have something to work with.

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

    Default

    It should print the html, several things could be at fault but it should display the html. To include the Java code, you can use <pre> tags, ther is also something called an iframe, which works well here to display the java code.

    move to servlets....
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default

    Well it's supposed to be a servlet. What should I do to make it show properly? In my school, I think tomcat was used but I don't see that in the webhost i'm using.

  4. #4
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    The html code should be located in WebContent/WEB-INF folder

    If you use 8080 port on your localhost and your project is called Site,so it should be like this:

    Java Code:
    http://localhost:8080/Site/MyFirstServlet
    You should also check web.xml file where you declare which file is loaded as a welcome file,for example your welcome file is called index.html,your web.xml should be


    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>Site</display-name>
      <welcome-file-list>
      
        <welcome-file>index.html</welcome-file>
     
      </welcome-file-list>
    </web-app>
    Last edited by serjant; 11-29-2008 at 12:51 PM.

  5. #5
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default

    Damn it, I can't post images yet =/ I was going to show the webhost I use. Atm I only have a public_html folder being used. Do I make the WEB-INF folder inside the public_html folder or in the home folder outside public_html. I can probably email the company that hosts my website to find out the port number.

    So the java files need to be in its own project folder and then use the form action to call it out? Same as above, does it go in the public_html folder or in the home folder outside public_html?

    Thanks.

  6. #6
    Bomber_Will is offline Member
    Join Date
    Nov 2008
    Posts
    40
    Rep Power
    0

    Default

    Forget my last post, I saw what you meant but I still need to figure out my port number. Still waiting on email response to company.

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

    Lightbulb port numbers

    WEB-INF should be something that is part of and controlled by the server, you do not code WEB-INF either in the url nor in the code you write. It is a convention that all the mapping to WEB-INF occurs inside the server code, thus preventing access to that directory except by code running on the server.

    You write and compile java code, placing it behind WEB-INF during ftp upload, the mapping to that code is facilitated by some xml but server is supposed to ( and will ) look there for execuatable java code, you just do not put that in the url that opens the interaction.

    Port number is similar, let the server do the port stuff while it is running. I if you need to pass variables to the program, use GET name/value pairs, or use forms. You can get into port numbers if you are running locally and want to do some testing, the RFC for port numbers allows a wide range for development and testing:
    Java Code:
    /* The Dynamic and/or Private Ports are those from 49152 through 65535
     * Source:  http://www.iana.org/assignments/port-numbers
     * The port numbers are divided into three ranges: the Well Known Ports,
     * the Registered Ports, and the Dynamic and/or Private Ports.
     */
    which is the fancy version. In general you may use any port number over 1024

    There is a powerful server in the sample docs, study it if you can. Some hosts block access to some port numbers due to undisciplined abuse by some traffic, that is not a concern unless you are doing advanced studies.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  8. #8
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    I recommend you to work with eclipse,create there Dynamic Web project which will automatically create all the said above stuff for you,configure the server in eclipse and done .Make Ant build for your project and run it.

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

    Default

    Quote Originally Posted by serjant View Post
    I recommend you to work with eclipse,create there Dynamic Web project which will automatically create all the said above stuff for you,configure the server in eclipse and done .Make Ant build for your project and run it.
    Or Netbeans, which also makes all the magic happen with one click.

    With either, to put the pages/servlets into production, you need a productions.

Similar Threads

  1. How to download website (Get all link in website)
    By finalmem in forum Advanced Java
    Replies: 0
    Last Post: 11-12-2008, 08:43 AM
  2. Replies: 0
    Last Post: 10-31-2008, 07:07 PM
  3. Replies: 1
    Last Post: 08-29-2008, 01:54 PM
  4. Kode Java Website
    By wsaryada in forum Reviews / Advertising
    Replies: 4
    Last Post: 01-18-2008, 09:16 PM
  5. Replies: 0
    Last Post: 06-27-2007, 07:48 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
  •