Java applet outputting HTML

I’m trying to create a simple HelloWorld applet and I’m running into problems getting it to work in Firefox 3.6. The Java Tester page (Java Tester - What Version of Java Are You Running?) says I’m running Java 1.6 and my Internet Explorer 8 doesn’t even show anything at all–just a blank image placeholder where the Java readout should be displayed. Weird thing is that my Java applet runs perfectly fine on Internet Explorer 8 but not on Firefox. :sick:

Would this have anything to do with the “set CLASSPATH=” part of using javac?

(I’m sorry, but I forgot to mention that I’m using Tomcat 7 to deliver these things on my Windows XP box.)

Can you post the html that you’re using to load the applet?

I think I messed-up with explaining what I’m trying to do here…

I have the following .java file located in C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\webdev\WEB-INF\classes\sample:

package sample;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet{
	public void doGet(HttpServletRequest req, HttpServletResponse res)
		throws ServletException, IOException{
			res.setContentType("text/plain");
			PrintWriter out = res.getWriter();
			out.println("<html>");
			out.println("<head><title>Hello World!</title></head>");
			out.println("<body>");
			out.println("<h1>Hello World! Dude!</h1>");
			out.println("</body></html>");
	}
}

The Tomcat XML file being used in this is the following and found in C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\webdev\WEB-INF:

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">  

    <description>
      AppDev Class Example
    </description>
    <display-name>Hello World! Dude!</display-name>

    <servlet>
      <servlet-name>HelloWorld</servlet-name>
      <servlet-class>sample.HelloWorld</servlet-class>
    </servlet>
 
	<servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/HelloWorld</url-pattern>
    </servlet-mapping>


</web-app>

In the Apache Tomcat 7 Properties window (the “Configure Tomcat” tool that’s accessed from the Start / Programs / Apache Tomcat 7.0 directory), “Java Classpath” is set to: C:\Program Files\Apache Software Foundation\Tomcat 7.0\bin\bootstrap.jar;C:\Program Files\Apache Software Foundation\Tomcat 7.0\bin\ omcat-juli.jar

Unfortunately, my HelloWorld Java app wouldn’t compile correctly unless I used the j2ee.jar from the Sun directory: C:\Sun\SDK\lib

I’ve been trying to view my final .class file (after compiling it with javac) by going into Firefox and accessing the URL “[B]http://localhost:8888/webdev/HelloWorld[/B]” (I’ve had to set it to listen to 8888 due to other servers running on the system).

Inside the about:plugins with Firefox 3.6, there’s information about “Java™ Platform SE 6 U25” and “Java Deployment Toolkit 6.0.250.6”. They’re both enabled.

Whenever I try to access this final .class file through the browser (I refer to it as an applet–which is probably wrong) I get nothing but the markup within that Java file. However, when I try to access it inside of IE8, everything comes out great.

Sorry for any confusion. What’s going on here? Why doesn’t this work in Firefox?

Okay, just to clear this up, applets are a completely different animal, this is a servlet.

The fact that you’re getting the mark-up was the key, I see you’ve set the content type to text/plain, then turn around and send text/html.

Given MS’s track record of not following any standards known to mankind, it’s hardly a surprise that IE is a bit more lenient; conversely, FF takes the server’s word for it when it’s told that the response will be plain text and obediently displays the markup.

Change the content type to text/html (or omit it, I think html is the default) and it’ll work fine.

Nail on the head, rushiku. It’s working. Thanks!