[JavaEE] Servlet manual configuration and javaeeservlet Configuration
1. Servlet
A Dynamic web Resource Development technology provided by sun is essentially a java applet that can add Sevlet to the Servlet container.
* Servlet container -- the environment that can run Servlet is called Sevlet container -- tomcat
* Web Container -- the environment that can run web applications is called web Container -- tomcat
Write a class to implement the Servlet interface defined by sun
Configure the written class to tomcat
Define a class MyServlet to inherit the GenericServlet class and implement its abstract method service ()
Public void service (ServletRequest req, ServletResponse res) throws ServletException, java. io. IOException
A ServletResponse object is passed in, and the getWriter (). write () method of the ServletResponse object is called. Parameter: Text
Import related packages java. io javax. servlet
When compiling javac MyServlet. java through the command line, an error is reported. The servlet package cannot be found because this is the package under tomcat.
Set the environment variable in the command line, set classpath = % classpath %; position of the servlet package in lib under tomcat
Compile javac-d. MyServlet. java with a package
Configure the compiled package to the web application under tomcat
Create a WEB-INF directory under the web application directory
Create directory classes, directory lib, file web. xml under WEB-INF directory
Put the class in the classes directory
Configure the web. xml file and the <web-app> node,
Configure servlet <servlet>
Configure the Servlet name <servlet-name>
Configure the full path name of the class <servlet-class>
Configure the Sering <serlet-mapping>
Configure the virtual path <url-pattern>
MyServlet. java
package com.tsh;import java.io.*;import javax.servlet.*;public class MyServlet extends GenericServlet{ public void service(ServletRequest req, ServletResponse res) throws ServletException, java.io.IOException{ res.getWriter().write("taoshihan"); }}
Web. xml
<?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="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>