• 0306 Tomcat服务器,Servlet


    Tomcat服务器

    配置服务器

    windows--preference

     

    next

     

     

     

     

    双击打开

     然后保存

     然后再右键 Add and Remove

     

     finish

     右键start 开启服务器,右键stop 关闭服务器

     地址栏输入http://localhost:8080/出来上述截图说明服务器配置成功

    或者输入刚刚添加进去的WEB01项目中的demo01 出来界面也说明配置成功

     

     上述截图中这两个web01项目不是同一个,左侧WEB01中是本地工作空间的项目,右侧服务器上的web01,是在配置服务器的时候将原本地WEB01复制了一份放在了服务器上,这两个文件目录都不一样

    Servlet

    Servlet 运行在服务端的Java小程序,是sun公司提供一套规范(接口),用来处理客户端请求、响应给浏览器的动态资源(可以理解为 这就是Controller层)

    那么想要通过浏览器地址栏访问我们java中写好的类,就要用到Servlet接口

     那我们需要创建一个类去实现这个接口 并且重写方法

    代码展示

    在src下创建一个类

    public class MyServlet implements Servlet{
    	public ServletConfig getServletConfig() {
    		// TODO Auto-generated method stub
    		return null;
    	}
    	public String getServletInfo() {
    		// TODO Auto-generated method stub
    		return null;
    	}
    	public void init(ServletConfig arg0) throws ServletException {
    		System.out.println("MyServlet创建了");
    	}
    	public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
    		System.out.println("MyServlet被访问了");
    	}
    	public void destroy() {
    		System.out.println("MyServlet被销毁了");
    	}
    
    }
    

      在xml文件中配置文件 web.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>WEB01</display-name> <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>com.oracle.demo01.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/MyServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>

      测试配置文件后,重启服务器,在地址栏输入

     

     当我们关闭服务器

    图解

     在Servlet接口中的方法

    init(ServletConfig config)servlet对象创建的时候执行

    service(ServletRequest request,ServletResponse response)每次请求都会执行(浏览器每刷新一次就执行一次)

    destroy() servlet销毁的时候执行

  • 相关阅读:
    perl -p -i -w -e
    s///|s()()i|/i|/g|U|u|L|l|Ul|split|join|匹配到hash|匹配到变量|`date`|$^I
    /^/m|/$/m||B|$&|$`|$'|变量捕获|()?|(?:pattern)|(?<LABEL>PATTERN)|$+{LABEL}|(|)|g{LABEL}
    ALG 4-3: Optimal Caching
    ALG 4-2: Scheduling to Minimize Lateness
    ALG 4-1: Interval Scheduling
    ALG 3-n: Practice
    ALG 3-6: Directed Acyclic Graphs and Topological Ordering (有向无环图与拓扑排序)
    ALG 3-5: Connectivity in Directed Graphs (有向图的连接性)
    ALG 3-4: Testing Bipartiteness
  • 原文地址:https://www.cnblogs.com/-gongxue/p/14491154.html
Copyright © 2020-2023  润新知