天天看点

Servlet 第三方jar包使用1servlet创建方式三(模板 设计模式)(重点)

创建一个Dynamic  Web  Project工程
然后在他的,工程下的src文件夹下.创建.Class类

利用Servers 添加apache-tomcat-.M26.zip
就可以运行了.
           
apache-tomcat-.M26文件夹下的文件是什么含义
backu: 是第一次配置文件成功后生成的文件夹
bin文件夹:可执行文件(打开关闭服务器文件)可能用到的shutdown.sh文件  startup.sh 
cinf文件夹: 服务器的配置文件 可能用到的web.xml
lib文件夹: 服务器使用(依赖)的Jar包
logs文件夹: 日志文件
temp 文件夹 : tomcat产生的临时文件
webapps 文件夹:系统自带的 默认的存放项目的工程(应用程序)文件夹 
work 文件夹 : 服务器自己的工作空间
wtpwebapps : 与 Eclipse 关联成功后 产生的存放工程的文件夹
           

使用终端

使用终端进入tomcat 服务器下单bin文件夹下.

在终端中 输入:cd //Users/lanou/apache-tomcat-.M26/bin
获取文件夹权限
sudo chmod *.sh
输入密码;电脑密码
password
执行启动服务器
sudo sh ./startup.sh
访问网址(url:全球网址)
http://localhost:(tomcat 的主页)
协议://本地地址:端口号/项目名(应用程序名)/访问的资源
           

用户如何从浏览器访问到Servlet类???

Servlet 小服务程序
Servlet 是个Java类
Servlet 是个接口
用户用浏览器访问服务器(tomcat)
服务器通过网址可以找到 对应项目的 web.XML文件
解析XML文件获取到对应的实现类,利用实现类创建对象.
    通过网址找到对象的servlet-name
    通过servlet-name找到对应的 servlet类
    创建servlet对象
执行生命周期的几个方法
实例化-->初始化-->service服务-->销毁
           

创建servlet的方式一 利用实现类

声明周期方法以下四个

public class Demo01 implements Servlet{
 //1.实例化方法
 public Demo01(){
     super();
     System.out.println("我是实例化方法");
 }
 //2.初始化方法
 public void init(ServletConfig arg0)throws ServletException{
      System.out.println("我是初始化方法");
 }
 //3.服务方法Service
 public void service(ServletRequest arg0 , ServletResponse arg)throws  ServletException, IOException{
     System.out.println("我是服务方法Service");
 }
 //4.销毁方法destory
 public void destroy(){
     System.out.println("我是销毁方法destory");
 }
           

创建Servlet的方式二(适配器模式创建 遥控器)

使用那个方法就重写那个方法

用不上的方法可以选择性的不重写

public class Demo02 extends GenericServlet {
        @Override
        public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        System.out.println("我是demo02");

        }
}
           

servlet创建方式三(模板 设计模式)(重点)

常用方法(必须会)

public class Demo03 extends HttpServlet{
        //声明一个ServletConfig对象 当做成员变量
        private ServletConfig config;

    //初始化方法(通过该方法的参数  获取ServletConfig对象)
    //ServletConfig对象保存的是servlet中的配置信息
        @Override
        public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
        super.init(config);
        //接收一下ServletConfig
        this.config =config;
        }
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取servle的配置信息
//          getInitParameter(name);
            //参数是配置时的name(相当于key)
            //用key获取对应的value
            String value = this.config.getInitParameter("encoding");
            // TODO Auto-generated method stub
            //调用父类的doGet方法 怎么着都会报错  
            //不是400  就是405
            //所以重写 doGet方法 注意:不要调 super.doGet(req, resp);  要记得删掉
//          super.doGet(req, resp);
            System.out.println("我是Demo03");
            //写要操作的逻辑(重点)

        }
        //接收post 请求
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
            //调用父类的doGet方法 怎么着都会报错  
            //不是400  就是405
            //所以重写 doGet方法 注意:不要调 super.doPost(req, resp); 要记得删掉
//      super.doPost(req, resp);

        }
}
           

获取ServletConfig方式一

public class Demo04  extends HttpServlet{
            @Override
            protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                // TODO Auto-generated method stub
                //通过父类中的方法获取配置信息(获取ServletConfig对象)
                String config = this.getServletConfig().getInitParameter("encoding");
                System.out.println(config);
            }
            @Override
            protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // TODO Auto-generated method stub
                doGet(req, resp);
            }
}
           

获取ServletConfig方式一

/*
 * 域对象
 * 存储(在一定范围内,信息的对象)
 * ServletContext范围: 是整个程序中 都可以访问到 并且只有一个 单例对象
 * 每个servlet都可以访问到这个域对象
 * 如何获取ServletContext 对象?
 * 方式1:从SerletConfig对象中获取
 * 方式2:从父类中直接或区域
 * 注意:所有的域对象  都有/设置/获取/删除的方法
 * 
 */
public class Demo05 extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从ServletConfig对象中获取
    ServletContext application = this.getServletConfig().getServletContext();
    //添加一个数据到context域中
    //相当于添加一个键值对  key  和 value 
    application.setAttribute("username", "wanglong");
    }

}
           
public class Demo06 extends  HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取域对象
            Object object = this.getServletContext().getAttribute("username");
            System.out.println(object);
        }
}
           

Servlet 第三方jar包使用2

https://blog.csdn.net/qq_36390044/article/details/79720942

继续阅读