A Servlet
Sun provides a dynamic Web Resources development Technology, essentially a Java applet, you can add sevlet to the Servlet in the container
*servlet Container -- the environment that can run the Servlet is called the sevlet container. --Tomcat
*web Container - - The environment that can run a Web application is called a Web container --Tomcat
Write a class to implement the Servlet interface defined by Sun Company
Configure the written class to Tomcat
Defines a class Myservlet inherits the genericservlet class, implementing its abstract method service ()
public void Service (ServletRequest req, servletresponse Res) throws Servletexception, Java.io.IOException
Pass in a servletresponse object that calls the servletresponse object's getwriter (). Write () method, Parameters: Text
Import the related package java.io Javax.servlet
Command-line compilation Javac Myservlet.java An error is reported andtheservlet package cannot be found because this is a package under Tomcat
command line setting environment variable,set Classpath=%classpath%;tomcat the location of the servlet package under Lib
Command-line compilation javac-d. Myservlet.java, with package compilation
Configure the compiled package into a Web application under Tomcat
Create a new web-inf directory under the Web app directory
Create a new directory under the web-inf directory classes, directory lib, file Web. xml
Put the class in the classes directory
Configure the Web. XML file, configuration <web-app> node,
Configuring servlets <servlet>
Configure the Servlet name <servlet-name>
Full path name of the configuration class <servlet-class>
Configuration mapping <serlet-mapping>
Configure virtual paths <url-pattern>
Myservlet.java
Package com.tsh; import java.io.*; import javax.servlet.*; Public class extends genericservlet{ publicvoidthrows servletexception, java.io.ioexception{ Res.getwriter (). Write ("Taoshihan");} }
Xml
<?XML version= "1.0" encoding= "Iso-8859-1"?><Web-appxmlns= "Http://xmlns.jcp.org/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version= "3.1"><servlet> <Servlet-name>Myservlet</Servlet-name> <Servlet-class>Com.tsh.MyServlet</Servlet-class> </servlet> <servlet-mapping> <Servlet-name>Myservlet</Servlet-name> <Url-pattern>/index</Url-pattern> </servlet-mapping></Web-app>
[Java EE] Manual configuration of the servlet