Note
Setup
https://blog.csdn.net/hushui/article/details/82732412
Configuration
https://blog.csdn.net/hushui/article/details/83859847
1. Envrionment Centos/RHEL7 with Java 1.8
lake@localhost:~/java/servlet/MyServlet$ javac -version
javac 1.8.0_65
2. lib of tomcat/servlet
lake@localhost:~/java/servlet/MyServlet$ rpm -ql tomcat-lib-7.0.54-2.el7_1.noarch |grep servlet
/usr/share/java/tomcat/tomcat-servlet-3.0-api.jar
lake@localhost:~/java/servlet/MyServlet$ rpm -ql tomcat-lib-7.0.54-2.el7_1.noarch
/usr/share/java/tomcat
/usr/share/java/tomcat/annotations-api.jar
/usr/share/java/tomcat/catalina-ant.jar
/usr/share/java/tomcat/catalina-ha.jar
/usr/share/java/tomcat/catalina-tribes.jar
/usr/share/java/tomcat/catalina.jar
/usr/share/java/tomcat/commons-collections.jar
/usr/share/java/tomcat/commons-dbcp.jar
/usr/share/java/tomcat/commons-pool.jar
/usr/share/java/tomcat/jasper-el.jar
/usr/share/java/tomcat/jasper-jdt.jar
/usr/share/java/tomcat/jasper.jar
/usr/share/java/tomcat/log4j.jar
/usr/share/java/tomcat/tomcat-api.jar
/usr/share/java/tomcat/tomcat-coyote.jar
/usr/share/java/tomcat/tomcat-i18n-es.jar
/usr/share/java/tomcat/tomcat-i18n-fr.jar
/usr/share/java/tomcat/tomcat-i18n-ja.jar
/usr/share/java/tomcat/tomcat-jdbc.jar
/usr/share/java/tomcat/tomcat-jsp-2.2-api.jar
/usr/share/java/tomcat/tomcat-juli.jar
/usr/share/java/tomcat/tomcat-servlet-3.0-api.jar
/usr/share/java/tomcat/tomcat-util.jar
/usr/share/java/tomcat/tomcat7-websocket.jar
/usr/share/java/tomcat/websocket-api.jar
.....................
/usr/share/tomcat/bin/tomcat-juli.jar
lake@localhost:~/java/servlet/MyServlet$
3. First Servlet based on HttpServlet
lake@localhost:~/java/servlet/MyServlet$ cat MyServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");
}
}
lake@localhost:~/java/servlet/MyServlet$
lake@localhost:~/java/servlet/MyServlet$ javac MyServlet.java
MyServlet.java:2: error: package javax.servlet does not exist
import javax.servlet.*;
^
MyServlet.java:3: error: package javax.servlet.http does not exist
import javax.servlet.http.*;
^
.........................
lake@localhost:~/java/servlet/MyServlet$ javac -classpath /usr/share/java/tomcat/tomcat-servlet-3.0-api.jar MyServlet.java
lake@localhost:~/java/servlet/MyServlet$ ls -l
total 8
-rw-r--r-- 1 lake sys 865 Nov 9 03:34 MyServlet.class
-rw-r--r-- 1 lake sys 634 Nov 9 03:29 MyServlet.java
lake@localhost:~/java/servlet/MyServlet$
4. Deploy
lake@localhost:/var/lib/tomcat/webapps$ pwd
/var/lib/tomcat/webapps
lake@localhost:/var/lib/tomcat/webapps$ mkdir -p MyWebApps/WEB-INF/classes
lake@localhost:/var/lib/tomcat/webapps$ find MyWebApps/
MyWebApps/
MyWebApps/WEB-INF
MyWebApps/WEB-INF/web.xml
MyWebApps/WEB-INF/classes
MyWebApps/WEB-INF/classes/MyServlet.class
MyWebApps/index.html
lake@localhost:/var/lib/tomcat/webapps$ chown tomcat.tomcat -R MyWebApps
lake@localhost:/var/lib/tomcat/webapps$ ls -l
total 24
drwxr-xr-x 8 tomcat tomcat 4096 Sep 15 10:44 examples
drwxr-xr-x 5 root tomcat 4096 Sep 15 09:59 host-manager
drwxr-xr-x 5 root tomcat 4096 Sep 15 09:59 manager
drwxr-xr-x 3 tomcat tomcat 4096 Nov 9 04:33 MyWebApps
drwxr-xr-x 3 tomcat tomcat 4096 Sep 15 10:44 ROOT
drwxr-xr-x 5 tomcat tomcat 4096 Sep 15 10:44 sample
lake@localhost:/var/lib/tomcat/webapps$ cat MyWebApps/WEB-INF/web.xml
<?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"
metadata-complete="true">
<description>
MyServlet Samples.
</description>
<display-name>MyServlet and JSP Examples</display-name>
<!-- Define servlets that are included in the example application -->
<servlet>
<servlet-name>MyServletName</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServletName</servlet-name>
<url-pattern>/MyServletUrl</url-pattern> <<<<<<<<<<<<!!!! Note this is /MyWebApps/MyServletUrl
</servlet-mapping>
</web-app>