First, the Servlet
1.sun provides a dynamic web resource development technology. Essentially, it's a Java applet. You can add servlets to the servlet container to run.
*servlet container-an environment that can run a servlet is called a servlet container. ---Tomcat
*web Container--the environment that can run a Web application is called the Web container---Tomcat
2.
Write a class to implement the Servlet interface defined by Sun Company
Configure a well-written class to the Web application's XML in tomcat (configure the external access path)
Next, we write a servlet ourselves and put it under Tomcat to run.
1. Write a class Firstservlet, in order to facilitate our direct inheritance of Genericservlet abstract class, directly implement the service method.
Package Com.itheima;import javax.servlet.*;import Java.io.*;class Firstservlet extends Genericservlet {public void Service (ServletRequest req, servletresponse Res) throws Servletexception, ioexception{Res.getwriter (). Write ("Hello World! ");}}
2. Compile into A. class file with a package. We need the servlet jar package.
E:\code>set classpath=%classpath%; E:\apache-tomcat-6.0.14\lib\servlet-api.jar; E:\code>javac Firstservlet.javafirstservlet.java:6: Error: Unable to find Symbol response.getwriter (). Write ("Hello world!"); ^ symbol: variable response Position: Class FirstServlet1 error E:\code>javac firstservlet.javae:\code>javac-d. Firstservlet.javae:\code>
At the end of the run, there is a Com-->itheima-->firstservlet file. Put this folder under Tomcat's WebApp to run.
Then build web-inf/classes lib Web. xml
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" 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_2_5.xsd "><servlet> <servlet-name>firstservlet</ servlet-name> <servlet-class>com.itheima.FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/servlet/ firstservlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file >index.jsp</welcome-file> </welcome-file-list></web-app>
Running results can be seen in the browser Hello world!
Black Horse day03 servlet Quick Start