To run the servlet, JSP / Servlet Container, I suggest that beginners use Tomcat.
Tomcat (Latest Version 5.0):
Http://cvs.apache.org/builds/jakarta-tomcat-5/nightly/jakarta-tomcat-5-bin-20030725.zip
Then extract this compression package to:
C: / Tomcat
Then configure environment variables; add three system variables:
Java_Home: C: / JDK
Tomcat_home: C: / Tomcat
ClassPath:% java_home% / lib;% Tomcat_Home% / LIB
Tomcat's environment variable is configured, the following verifies whether Tomcat can run:
Go to the C: / Tomcat / bin in the console, run startup, then appear back, and jump a big string, and finally indicate that Server has run.
Enter http: // localhost: 8080 in the browser, the welcome interface, indicating that Tomcat is no problem. Then write your first servlet as above, write your first servlet.
Import java.io. *;
Import javax.servlet. *;
Import javax.servlet.http. *;
Public Class HelloWorld Extends httpservlet
{
Public void doget (httpservletRequest request, httpservletResponse response)
Throws ServleTexception, IOException
{
Response.setContentType (Text / HTML ");
PrintWriter out = response.getwriter ();
Out.println ("
Out.println ("this is my first servlet);
Out.println (" Title> head>
Out.println ("
Out.println (" Body> HTML>");
}
}
Then use Javac HelloWorld.java to compile this file, if there is an import javax.servlet. *
Then you should copy the servlet.jar file in C: / Tomcat / Common / Lib to C: / JDK / JRE / LIB / EXT, compile again, no problem!
Then in the C: / Tomcat / WebApps / root inside the Tomcat directory, follow the following file structure:
Root / index.html
Root / welcom.jsp
Root / Web-INF / LIB / MyServlet.jar (if your servlet is piled up .jar file, put it under LIB)
Root / Web-INF / CLASS / HELLOWORLD.CLASS (put the helloWorld.class file generated above))
Then enter http: // localhost: 8080 / servlet / helloWorld in the browser, and the error is returned by the service: Error 404 - NOT FOUND What is going on?
Servlet must register with the web.xml file below the directory of C: / Tomcat / WebApps / root / web -in, open this web.xml file with EP, join:
Servlet>
Servlet-maping>
Such structure
Servlet>
Represents the specified servlet class. And the following structure:
Servlet-maping>
Indicates which URL schema that specifies the Helloservlet to map.
After modifying Web.xml, restart Server, then enter http: // localhost: 8080 / servlet / helloworld, then a Hello, World! Waiting for you.