Getting Started with servlet programs

Source: Internet
Author: User
Tags xmlns tomcat tomcat server
Create a servlet program

In the directory D:\cn\itcast\firstapp\servlet directory, write a servlet
Because the Servlet interface is implemented directly, it is inconvenient to write the servlet, and many methods need to be implemented
Therefore, you can implement the Javax.servlet.GenericServlet of the Servlet interface by inheriting the implementation class.
Sample code

Helloworldservlet.java package
Cn.itcast.firstapp.servlet;
Import java.io.*;
Import javax.servlet.*;
public class Helloworldservlet extends genericservlet{public
    void Service (ServletRequest request, Servletresponse Response) 
    throws Servletexception, ioexception{
        //Get output Stream Printerwriter object, servlet uses output stream to generate response
        PrintWriter Out=response.getwriter ();
        Use the output stream object to send character data to the client
        out.println ("Hello World");}
}

As you can see, after the Helloworldservlet class inherits Genericservlet, only the service () method is implemented
Because the Genericservlet class has been implemented in addition to the service () method of the Servlet interface, other methods have
Thus, inheriting the Genericservlet analogy implements the Servlet interface more simply by compiling the program

Enter the directory where the Helloworldservlet.java is located, compile the Helloworldservlet.java file

Error message, package Javax.servlet does not exist
Because the Java compiler does not find the Javax.servlet package in the CLASSPATH environment variable, it needs to add the directory where the servlet-related jar package resides, to the CLASSPATH environment variable
Since the servlet program is a Java EE program, not a javase program, all jar files need to be manually added to the CLASSPATH environment variable, into the Lib directory under the Tomcat installation directory, Contains many jar files related to the Tomcat server, where the Servlet-api.jar file is a servlet-related jar file
Open a command-line window and add the directory containing the Servlet-api.jar file to the CLASSPATH environment variable through the set classpath command
Command
Set classpath=%classpath%;D: \tomcat\apache-tomcat-7.0.79\lib\servlet-api.jar

Compile complete
Under Tomcat's WebApps directory, create a directory chapter04
Chapter04 is the name of the WEB app, and then, under the Chapter04 directory, create the \web-inf\classes directory and copy the Helloworldservlet.class file to the classes directory
Attention
You need to copy the directory where the file resides, \cn\itcast\firstapp\servlet Copy the past Configuration Web. xml File

Go to directory Web-inf and write a Web. xml file
Web. xml file, refer to the Web. xml file under the Tomcat installation directory, located under the Examples\web-inf subdirectory
The code is as follows

<?xml version= "1.0" encoding= "iso-8859-1"?> <web-app xmlns= "http://java.sun.com/ Xml/ns/javaee "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" Http://java.sun.com/xml
        /ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "version=" 3.0 "> <servlet> <servlet-name>HelloWorldServlet</servlet-name> <servlet-class>cn.itcast.firstapp.servlet . helloworldservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hell Oworldservlet</servlet-name> <url-pattern>/HelloWorldServlet</url-pattern> </servlet-mapp Ing> </web-app> 

Configuration information
<servlet>, for registering servlets
Its two sub-elements <servlet-name> and <servlet-class>, respectively, to specify the servlet name and its full class name
<servlet-mapping>, which is used to map the virtual path of the servlet's external access, its child elements <servlet-name> values, must and <servlet> elements in the <servlet-name > Same
<URL-PATTERN>, which specifies the virtual path to access the servlet, which is preceded by a forward slash/start that represents the root directory of the current Web application
Start the Tomcat server and enter the URL address in the address bar of the browser
Http://localhost:8080/chapter04/HelloWorldServlet
Visit helloworldservlet

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.