World class Hard Drive Recovery and renowned raid recovery services

WestNIC provides reliable web hosting services

Site navigation below

This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQs and more.

Subscribe to this FAQ: RSS news feed

Servlet concepts

Q: What's the difference between applets and servlets?

A: There are many fundamental differences between Applet and Servlet classes, the Java API documentation for the two types will show you they have little in common.

Applets are essentially graphical user interface (GUI) applications that run on the client side in a network environment, typically embedded in an HTML page. Applets are normally based on Abstract Windowing Toolkit components to maintain backward-compatibility with the widest range of browsers' Java implementations. The application classes are downloaded to the client and run in a Java Virtual Machine provided by the browser, in a restrictive security environment called a "sandbox".

Servlets are used to dynamically generate HTTP responses and return HTML content to Web browsers on the server side. Servlets are often used to validate and process HTML form submissions and control a series of user interactions in what is known as a Web application. Servlets can be used to control all aspects of the request and response exchange between a Web browser and the server, called a servlet container.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: What part of the Java platform do servlets and JSP belong to?

A: The servlet and JSP APIs are a standard extension to the core Java API and runtime system. Their package names are prefixed javax to indicate they are standard extensions, but that means that they rely upon a code implementation provided by a specific vendor.

For example, the Apache Tomcat project supplies its own implementation of the servlet and JSP API that is integrated with the servlet container. Developers must compile their code using the vendor's servlet package implementation by including it in the compiler's classpath. The servlet container includes the same package classes in its runtime system and feeds concrete instances of the servlet interface types to the servlet's lifecycle methods.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How does the JVM execute a servlet compared with a regular Java class?

A: Servlets are standard Java classes and are executed by the Java Virtual Machine in exactly the same way as any other. However, the environment or context in which servlets are executed is different. A servlet is not invoked directly through a main method, the class is loaded and run by a servlet container.

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
How does the JVM execute a servlet compared with a regular Java class?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How can I write a servlet using Javascript?

A: Java servlets is a server side technology that delivers dynamic content to Web browsers and other clients. Javascript is also delivered by a Web server, but the code is only interpreted and executed after it has been downloaded by the Web browser. This means that it is not possible to write servlet code in Javascript.

It is possible to include Javascript in the output of servlets and Java Server Pages, just like standard Web pages. It is also possible to dynamically generate Javascript using a servlet and use it as the source for a script tag, though this is only advisable in rare cases.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: Can I include normal Java classes in servlets?

A: Any Java class can be used in a Web application, provided you make the classes available to the servlet container at runtime. The Java API classes can be used directly by adding import statements to your servlet class. Other supporting classes can also be imported, but these classes must be added to the classes or lib directory of your application.

If you need to configure the supporting classes, this can be done with standard servlet configuration features using the ServletConfig and ServletContext objects available to the init(ServletConfig) method.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How can I tell when a servlet is instantiated?

A: A servlet must be instantiated before it is brought into service by the servlet container, so one way to check is to make a request to the servlet and check the response. If you need to check indirectly, you can override the init(ServletConfig) method and add log(String) statements to it. This method is called after the servlet container has instantiated the servlet before it is brought into service.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Servlet start up

Q: What are the basic steps to run a servlet?

A: The key steps in creating and running a servlet are outlined below in the simplest form. There are different techniques that can be used to complete these stages, described in other FAQ answers.

  1. Write and compile your servlet, e.g. ExampleServlet.class
  2. Create a Web application directory structure under the webapp directory of your servlet container (or use an existing one), e.g.
    {webapps-dir}/example
    {webapps-dir}/example/WEB-INF
    {webapps-dir}/example/WEB-INF/classes
              
  3. Place your servlet class file in the WEB-INF/classes directory for your application, e.g.
    {webapps-dir}/example/WEB-INF/classes/ExampleServlet.class
              
  4. Create a WEB-INF/web.xml file for your application (or edit an existing one) and add servlet and servlet-mapping elements for your servlet, e.g.
    {webapps-dir}/example/WEB-INF/web.xml
              
  5. Start the servlet container.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How do I compile a servlet?

A: To compile a servlet, you will need to have the Java Servlet API classes in your classpath. Most Java servlet containers come with a copy, it may be called servlet.jar or something similar. The basic classes are in the packages javax.servlet and javax.servlet.http.

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
How do I compile a servlet?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: Why won't my servlet compile?

A: All those "cannot find symbol" error messages mean that you do not have the Java servlet JAR file on your compiler's classpath, so it cannot find the servlet class files. The servlet JAR file is normally distributed with your servlet container. For Apache Tomcat it is a file named {CATALINA_HOME}/common/lib/servlet-api.jar. Add this to your compiler classpath as follows.

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
Why won't my servlet compile?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: Can I use a constructor in my servlet?

A: A servlet is a normal Java class, so when there are no custom constructors, there is an implicit default constructor with no arguments. Servlet containers typically use the Class.newInstance() method to load servlets, so you must be careful to add an explicit default constructor if you add non-default constructors.

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
Can I use a constructor in my servlet?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: Can I use a normal class to handle my requests?

A: Servlets are normal Java classes, they compile and run just like any other class. All that is required is that servlets implement the javax.servlet.Servlet interface. Usually, they extend a protocol-specific class such as javax.servlet.http.HttpServlet.

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
Can I use a normal class to handle my requests?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: What is the difference between JAR and WAR files?

A: There is no difference between the binary form of JAR and WAR files, they both use zip compression provided by the jar tool. You can create a WAR file by navigating to the root directory of your Web application and typing the jar command.

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
What is the difference between JAR and WAR files?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Servlet techniques

Q: Can I access a servlet from a stand alone client?

A: It is certainly possible to access a servlet that is hosted in a servlet container. Any HTTP client should be able to connect to a properly configured servlet container and make requests to a servlet. However, servlets do not run in their own right, they are not server applications.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: How can I write a servlet client for testing?

A: Marty Hall provides the source code for a basic HTTP client in his book Core Servlets and Java Server Pages. The source code for chapter 3 is available online. Look for WebClient.java and supporting classes. This application allows you to manually input the host, request path, HTTP header values and view the headers returned by a Web application.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: What is URL-rewriting?

A: URL-rewriting is a way of maintaining a session between an HTTP client and a servlet container which does not use cookies. Rather than exchange a session ID in a cookie, the servlet container includes it in the hyperlink URLs it generates for servlets and JSP.

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
What is URL-rewriting?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: What's the difference between forward, include and redirection?

A: The RequestDispatcher forward() and include() methods are mechanisms that are internal to a servlet container and do not affect the public URL of a Web resource. When you call the forward() method on a RequestDispatcher with a JSP path, the servlet container returns the JSP content on the original servlet's URL; this effectively becomes the response of the servlet itself.

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
What's the difference between forward, include and redirection?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Add this page to your chosen social bookmarking service

Style warning - please read

Home · CSS · Java · Javascript · HTML · Help · Log