J2EE

Webove aplikace

Zakladni servlet Hello:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Hello extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("Hello");
  }
}

javac -classpath /usr/share/tomcat6/lib/tomcat6-servlet-2.5-api-6.0.26.jar -d /usr/share/tomcat6/webapps/WEB-INF/classes/ Hello.java

Servlet HelloHtml:
package www1; // nazev adresare, do ktereho se to pak zkompiluje

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

public class HelloHtml extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String docType = "<!DOCTYPE HTML PUBLIC \"=//W3C/DTD HTML 4.0" +
      "Transitional//EN\">\n";
    out.println(docType + "<HTML>\n" +
      "<HEAD><TITLE>Hello HTML</TITLE></HEAD>" +
      "<BODY>\n<H1>Hello</H1>\n" +
      "</BODY></HTML>");
  }
}