RHEL/Centos7/Tomcat MyServlet

5 篇文章 0 订阅
3 篇文章 0 订阅

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> 
 

http://localhost:8080/MyWebApps/MyServletUrl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值