How to compile servlet

Compiling Servlets

When you develope servlets you need to have two things: the Servlet API class files, which are used for compiling, and a servlet engine such as a web server, which is used for deployment.

To obtain the Servlet API class files, you have several options: " Install the Java Servlet Development Kit (JSDK), available for free at http://java.sun.com/products/servlet/. JSDK Version 2.0 contains the class files for the Servlet API 2.0, along with their source code and a simple web server that acts as a servlet engine for HTTP servlets.

It works with JDK 1.1 and later. Install one of the many full-featured servlet engines, each of which typically bundles the Servlet API class files. You can compile servlets with the JDK, which is available from http://www.javasoft.com and other Java compilers, just like Applets and Applications.

If the Servlet packages javax.servlet and javax.servlet.http are not included with the compiler or your Servlet engine you have to install them separately by downloading the JSDK (Java Servlet Development Kit) and you should include the JSDK classes in your CLASSPATH environment variable.

This is the example of how to compile the TheHelloServlet on a Unix machine with a JDK installed and the JSDK 2.0 unpacked in /usr/java/JSDK2.0:

$ echo $CLASSPATH .:/usr/java/jdk/lib/classes.zip $ export CLASSPATH=$CLASSPATH:/usr/java/JSDK2.0/lib/jsdk.jar $ echo $CLASSPATH .:/usr/java/jdk/lib/classes.zip:/usr/java/JSDK2.0/lib/jsdk.jar $ ls TheHelloServlet.* TheHelloServlet.java $ javac TheHelloServlet.java $ ls TheHelloServlet.* TheHelloServlet.class TheHelloServlet.java

You can test your servlet using tomcat server. Please reffer to tomcat.apache.org for more details on the installation and use of servlets with the version supplied. Bellow are displayed some new classes and interfaces for servlet use:

For tomcat hosting, please visit our tomcat hosting solution page.

Interface javax.servlet.SingleThreadModel Interface javax.servlet.http.HttpSession Interface javax.servlet.http.HttpSessionBindingListener Interface javax.servlet.http.HttpSessionContext Class javax.servlet.http.Cookie Class javax.servlet.http.HttpSessionBindingEvent New Methods, Constructors and Fields Method javax.servlet.ServletContext.getServletNames() Method javax.servlet.ServletContext.log(Exception, String) Method javax.servlet.ServletRequest.getCharacterEncoding() Method javax.servlet.ServletRequest.getReader() Method javax.servlet.ServletResponse.getCharacterEncoding() Method javax.servlet.ServletResponse.getWriter() Constructor javax.servlet.ServletException.ServletException() Method javax.servlet.http.HttpServletRequest.getCookies() Method javax.servlet.http.HttpServletRequest.getRequestedSessionId() Method javax.servlet.http.HttpServletRequest.getSession(boolean) Method javax.servlet.http.HttpServletRequest.isRequestedSessionIdFromCookie() Method javax.servlet.http.HttpServletRequest.isRequestedSessionIdFromUrl() Method javax.servlet.http.HttpServletRequest.isRequestedSessionIdValid() Field javax.servlet.http.HttpServletResponse.SC_CONFLICT Field javax.servlet.http.HttpServletResponse.SC_CONTINUE Field javax.servlet.http.HttpServletResponse.SC_GATEWAY_TIMEOUT Field javax.servlet.http.HttpServletResponse.SC_GONE Field javax.servlet.http.HttpServletResponse.SC_HTTP_VERSION_NOT_SUPPORTED Field javax.servlet.http.HttpServletResponse.SC_LENGTH_REQUIRED Field javax.servlet.http.HttpServletResponse.SC_METHOD_NOT_ALLOWED Field javax.servlet.http.HttpServletResponse.SC_MULTIPLE_CHOICES Field javax.servlet.http.HttpServletResponse.SC_NON_AUTHORITATIVE_INFORMATION Field javax.servlet.http.HttpServletResponse.SC_NOT_ACCEPTABLE Field javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT Field javax.servlet.http.HttpServletResponse.SC_PAYMENT_REQUIRED Field javax.servlet.http.HttpServletResponse.SC_PRECONDITION_FAILED Field javax.servlet.http.HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED Field javax.servlet.http.HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE Field javax.servlet.http.HttpServletResponse.SC_REQUEST_TIMEOUT Field javax.servlet.http.HttpServletResponse.SC_REQUEST_URI_TOO_LONG Field javax.servlet.http.HttpServletResponse.SC_RESET_CONTENT Field javax.servlet.http.HttpServletResponse.SC_SEE_OTHER Field javax.servlet.http.HttpServletResponse.SC_SWITCHING_PROTOCOLS Field javax.servlet.http.HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE Field javax.servlet.http.HttpServletResponse.SC_USE_PROXY Method javax.servlet.http.HttpServletResponse.addCookie(Cookie) Method javax.servlet.http.HttpServletResponse.encodeRedirectUrl(String) Method javax.servlet.http.HttpServletResponse.encodeUrl(String) Method javax.servlet.http.HttpServlet.doDelete(HttpServletRequest, HttpServletResponse) Method javax.servlet.http.HttpServlet.doOptions(HttpServletRequest, HttpServletResponse) Method javax.servlet.http.HttpServlet.doPut(HttpServletRequest, HttpServletResponse) Method javax.servlet.http.HttpServlet.doTrace(HttpServletRequest, HttpServletResponse) Deprecated Methods Method javax.servlet.ServletContext.getServlets() (use getServletNames in conjunction with getServlet instead)

Changes from Version 2.0 to Version 2.1

Version 2.1 introduced the new model for inter-servlet communication and request delegation. There are some new minor features like nested Exceptions. Several inconsistencies in the API have been corrected.

New Interfaces

Interface javax.servlet.RequestDispatcher

Deprecated Interfaces (removed for security reasons)

Interface javax.servlet.http.HttpSessionContext

New Methods and Constructors

Method javax.servlet.ServletContext.getAttributeNames() Method javax.servlet.ServletContext.getContext(String) Method javax.servlet.ServletContext.getMajorVersion() Method javax.servlet.ServletContext.getMinorVersion() Method javax.servlet.ServletContext.getRequestDispatcher(String) Method javax.servlet.ServletContext.getResource(String) Method javax.servlet.ServletContext.getResourceAsStream(String) Method javax.servlet.ServletContext.log(String, Throwable) Method javax.servlet.ServletContext.removeAttribute(String) Method javax.servlet.ServletContext.setAttribute(String, Object) Method javax.servlet.ServletRequest.getAttributeNames() Method javax.servlet.ServletRequest.setAttribute(String, Object) Method javax.servlet.GenericServlet.init() Method javax.servlet.GenericServlet.log(String, Throwable) Constructor javax.servlet.ServletException.ServletException(String, Throwable) Constructor javax.servlet.ServletException.ServletException(Throwable) Method javax.servlet.ServletException.getRootCause() Method javax.servlet.http.HttpServletRequest.getSession() Method javax.servlet.http.HttpServletRequest.isRequestedSessionIdFromURL() Method javax.servlet.http.HttpServletResponse.encodeRedirectURL(String) Method javax.servlet.http.HttpServletResponse.encodeURL(String) Method javax.servlet.http.HttpSession.getMaxInactiveInterval() Method javax.servlet.http.HttpSession.setMaxInactiveInterval(int)

Deprecated Methods

Method javax.servlet.ServletContext.getServlet(String) (potentially unsafe) Method javax.servlet.ServletContext.getServletNames() (makes no sense without getServlet) Method javax.servlet.ServletContext.log(Exception, String) (use log(String, Throwable) instead) Method javax.servlet.ServletRequest.getRealPath(String) (use ServletContext.getRealPath instead) Method javax.servlet.http.HttpServletRequest.isRequestedSessionIdFromUrl() (use isRequestedSessionIdFromURL instead) Method javax.servlet.http.HttpServletResponse.encodeRedirectUrl(String) (use encodeRedirectURL instead) Method javax.servlet.http.HttpServletResponse.encodeUrl(String) (use encodeURL instead) Method javax.servlet.http.HttpSession.getSessionContext() (HttpSessionContext is deprecated)
Contact sales!