What does the Servlet runtime environment need?
First, we will discuss JSP/Servlet iner for running Servlet. I suggest you use Tomcat for beginners.
Tomcat Latest Version 5.0): http://cvs.apache.org/builds/jakarta-tomcat-5/nightly/jakarta-tomcat-5-bin-20030725.zip
Decompress the package:
C: \ Tomcat
Then configure the environment variables of the Servlet runtime environment. Add three system variables:
- JAVA_HOME: C:\JDK
- TOMCAT_HOME: C:\Tomcat
- CLASSPATH: %JAVA_HOME%\lib;%TOMCAT_HOME%\lib
The environment variables of Tomcat are configured. Check whether Tomcat can run as follows:
In the console, go to the C: \ Tomcat \ bin directory and run startup. Then, a window is displayed, which means that the Server is running.
Enter http: // localhost: 8080 in the browser. If the welcome page appears, it means Tomcat is okay. Write your first Servlet as above.
- 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("﹤h﹤tml﹥﹤head﹥﹤title﹥");
- out.println("This is my first Servlet");
- out.println("﹤/title﹥﹤/head﹥﹤body﹥");
- out.println("﹤h1﹥Hello,World!﹤/h1﹥");
- out.println("﹤/body﹥﹤/html﹥");
-
- }
- }
Then use javac HelloWorld. java to compile the file. If the file cannot be imported javax. servlet .*
Then, copy the servlet. jar file in C: \ Tomcat \ common \ lib to C: \ JDK \ jre \ lib \ ext and compile it again!
Then, in the Tomcat directory C: \ Tomcat \ webapps \ ROOT, follow the following file structure:
ROOT \ index.html
ROOT \ welcom. jsp
ROOT \ WEB-INF \ lib \ MyServlet. jar if your servlet. class is typed into a. jar file, put it under lib)
ROOT \ WEB-INF \ classes \ HelloWorld. class (put the above generated HelloWorld. class file in this)
Then, enter http: // localhost: 8080/servlet/HelloWorld in the browser. The Error 404 -- Not Found is returned by the Server.
What's going on?
Servlet must use C: \ Tomcat \ webapps \ ROOT \ WEB-INF directory under the web. xml file registration, open this web. xml file with EP, add in it:
- ﹤servlet﹥
- ﹤servlet-name﹥HelloWorld﹤/servlet-name﹥
- ﹤servlet-class﹥HelloWorld﹤/servlet-class﹥
- ﹤/servlet﹥
- ﹤servlet-mapping﹥
- ﹤servlet-name﹥HelloWorld﹤/servlet-name﹥
- ﹤url-pattern﹥/servlet/helloworld﹤/url-pattern﹥
- ﹤/servlet-mapping﹥
Such a structure
- ﹤servlet﹥
- ﹤servlet-name﹥HelloWorld﹤/servlet-name﹥
- ﹤servlet-class﹥HelloWorld﹤/servlet-class﹥
- ﹤/servlet﹥
Indicates the included servlet class. The following structure:
- ﹤servlet-mapping﹥
- ﹤servlet-name﹥HelloWorld﹤/servlet-name﹥
- ﹤url-pattern﹥/servlet/HelloWorld﹤/url-pattern﹥
- ﹤/servlet-mapping﹥
Specifies the URL mode to which HelloServlet maps.
After modifying web. xml, restart the Server and then enter http: // localhost: 8080/servlet/HelloWorld. Then, a large Hello, World! Waiting for you.
After configuring the Servlet runtime environment, we can start our interesting Servlet programming journey.
- Multi-thread synchronization in Servlet and JSP
- Enhanced Servlet and JSP security without modifying code
- Configuration of JSP, Servlet, and Bean in Tomcat
- How to Improve Servlet and JSP Application Efficiency
- Functions and principles of several encodings in JSP and Servlet