Brief Introduction to Servlet runtime environment Configuration

Source: Internet
Author: User

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:

 
 
  1. JAVA_HOME: C:\JDK  
  2. TOMCAT_HOME: C:\Tomcat  
  3. 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.

 
 
  1. import java.io.*;  
  2. import javax.servlet.*;  
  3. import javax.servlet.http.*;  
  4. public class HelloWorld extends HttpServlet  
  5. {  
  6. public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException  
  7. {  
  8.  
  9. response.setContentType("text/html");  
  10. PrintWriter out = response.getWriter();  
  11. out.println("﹤h﹤tml﹥﹤head﹥﹤title﹥");  
  12. out.println("This is my first Servlet");  
  13. out.println("﹤/title﹥﹤/head﹥﹤body﹥");  
  14. out.println("﹤h1﹥Hello,World!﹤/h1﹥");  
  15. out.println("﹤/body﹥﹤/html﹥");  
  16.  
  17. }  

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:

 
 
  1. ﹤servlet﹥  
  2. ﹤servlet-name﹥HelloWorld﹤/servlet-name﹥  
  3. ﹤servlet-class﹥HelloWorld﹤/servlet-class﹥  
  4. ﹤/servlet﹥  
  5. ﹤servlet-mapping﹥  
  6. ﹤servlet-name﹥HelloWorld﹤/servlet-name﹥  
  7. ﹤url-pattern﹥/servlet/helloworld﹤/url-pattern﹥  
  8. ﹤/servlet-mapping﹥  

Such a structure

 
 
  1. ﹤servlet﹥  
  2. ﹤servlet-name﹥HelloWorld﹤/servlet-name﹥  
  3. ﹤servlet-class﹥HelloWorld﹤/servlet-class﹥  
  4. ﹤/servlet﹥ 

Indicates the included servlet class. The following structure:

 
 
  1. ﹤servlet-mapping﹥  
  2. ﹤servlet-name﹥HelloWorld﹤/servlet-name﹥  
  3. ﹤url-pattern﹥/servlet/HelloWorld﹤/url-pattern﹥  
  4. ﹤/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.

  1. Multi-thread synchronization in Servlet and JSP
  2. Enhanced Servlet and JSP security without modifying code
  3. Configuration of JSP, Servlet, and Bean in Tomcat
  4. How to Improve Servlet and JSP Application Efficiency
  5. Functions and principles of several encodings in JSP and Servlet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.