Latest web development tutorials

Servlet package

Relates to the structure of the Web application's WEB-INF subdirectory is the standard for all the Java web applications, specified by the Servlet API specification. Given a top-level directory name myapp, the directory structure is as follows:

/ Myapp
    / Images
    / WEB-INF
        / Classes
        / Lib

WEB-INF subdirectory contains the application deployment descriptor called web.xml. All HTML files are located in the top-level directorymyapp.For the admin user, you will find the ROOT directory is myApp parent directory.

Create package Servlet

WEB-INF / classes directory contains all of the Servlet class and other types of documents, the class file directory structure match with their package name. For example, if you have a fully qualified class namecom.myorg.MyServlet, then the Servlet class must be located in the following directories:

/myapp/WEB-INF/classes/com/myorg/MyServlet.class

The following example creates MyServlet class package calledcom.myorgof.

// For the package named package com.myorg;  

// Import necessary java library import java.io. *;
import javax.servlet *.;
import javax.servlet.http *.;
 
public class MyServlet extends HttpServlet {
 
  private String message;
 
  public void init () throws ServletException
  {
      // Perform the required initialization message = "Hello World";
  }
 
  public void doGet (HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      // Set the response content type response.setContentType ( "text / html; charset = UTF-8");
 
      // Actual logic is here PrintWriter out = response.getWriter ();
      out.println ( "<h1>" + message + "</ h1>");
  }
  
  public void destroy ()
  {
      // do nothing}
}

Compiled package Servlet

Compiled package classes and other classes compiled no big difference. The easiest way is to get your java files remain fully qualified path to the class as mentioned above, will be retained in the com.myorg. You also need to add the directory in the CLASSPATH.

Suppose that your environment is set up correctly, enter<Tomcat-installation-directory> / webapps / ROOT / WEB-INF / classes directory, and compile MyServlet.java, as follows:

$ Javac MyServlet.java

If Servlet rely on other libraries, you must also cite those JAR file in the CLASSPATH. I was only quoting a servlet-api.jar JAR file, because I did not use any other library in the Hello World program.

The command line uses the built-in javac compiler, which is a Sun Microsystems Java Software Development Kit (JDK, the full name of Java Software Development Kit) that comes. Microsystems' Java software development kit (JDK). In order for the command to work properly, it must include the location of the Java SDK you are using the PATH environment variable.

If all goes well, this compiler will generateMyServlet.class file in the same directory.The next section will explain how a compiled Servlet deployed into production.

Servlet Deployment package

By default, Servlet application in the path under <Tomcat-installation-directory> / webapps / ROOT, and the class files in the <Tomcat-installation-directory> / webapps / ROOT / WEB-INF / classes in.

If you have a fully qualified class namecom.myorg.MyServlet, then the Servlet class must be in WEB-INF / classes / com / myorg / MyServlet.class , you need located in <Tomcat-installation-directory> / webapps / Create the following entry ROOT / WEB-INF / web.xml file:

    <Servlet>
        <Servlet-name> MyServlet </ servlet-name>
        <Servlet-class> com.myorg.MyServlet </ servlet-class>
    </ Servlet>
 
    <Servlet-mapping>
        <Servlet-name> MyServlet </ servlet-name>
        <Url-pattern> / MyServlet </ url-pattern>
    </ Servlet-mapping>

The above entries are to be created in the web.xml file <web-app> inside ... </ web-app> tag. In this file you may have a variety of items available, but do not care.

Here, you are almost done, now let's use the <Tomcat-installation-directory> \ bin \ startup.bat (on Windows) or <Tomcat-installation-directory> /bin/startup.sh (in Linux / Solaris and so on) to start tomcat server, and finally enter thehttp in the browser's address bar: // localhost: 8080 / MyServlet.If all goes well, you will see the following results:

Hello World