栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JavaWeb-02

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaWeb-02

1、Servlet 1.1、Servlet简介

Servlet是sun公司开发动态web的一门技术
sun在这些API中提供一个借口叫做:Servlet,如果你想开发一个Servlet程序,只需要完成两个小步骤:
1、编写一个类,实现Servlet接口
2、把开发好的Java类部署到web服务器中。
把实现了Servlet接口的Java程序叫做,Servlet

1.2、HelloServlet

1、构建一个普通的Maven项目,删掉里面的src目录,以后我们的学习就在这个项目里面建立Moudel;这个空的工程就是Maven主工程;
2、关于Maven父子工程的理解:
父项目中会有

    
        servlet
    

子项目会有

    
        javaweb-servlet
        com.dandelion
        1.0-SNAPSHOT
    

3、Maven环境优化
1,修改web.xml为最新的
2,将maven的结构搭建完整
4、编写一个Servlet程序
1,编写一个普通类
2,实现Servlet接口,这里我们直接继承HttpServlet

public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入doGet方法");
        PrintWriter writer = resp.getWriter();
        writer.print("Hello Servlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

5、编写Servlet的映射
Java程序要通过浏览器访问,而浏览器需要连接web服务器,所以我们需要在web服务器中注册我们写的Servlet,还需要给他一个浏览器能够访问的路径;

  
    hello
    com.dandelion.servlet.HelloServlet
  
  
    hello
    /hello
  

6、配置Tomcat
配置发布的路径就可以了
7、启动测试

1.3、Mapping问题

1、一个Servlet可以指定一个映射路径

  
    hello
    /hello
  

2、一个Servlet可以指定多个映射路径

  
    hello
    /hello
  
  
    hello
    /hello1

3、一个Servlet可以指定通用映射路径

  
    hello
    /hello/*
  

4、默认请求路径

  
    hello
    /*
  

5、指定一些后缀或者前缀等等

  
  
    hello
    *.dandelion
  

6、优先级问题
指定了固有映射路径优先级最高,如果找不到就会默认的处理请求

public class ErrorServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");

        PrintWriter writer = resp.getWriter();
        writer.print("404");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
  
    error
    com.dandelion.servlet.ErrorServlet
  
  
    error
    /*
  
1.4、ServletContext

1、共享数据

public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        String username = "dandelion";
        context.setAttribute("username",username);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
public class GetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        String username = (String) context.getAttribute("username");
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().print("名字"+username);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
  
    getc
    com.dandelion.servlet.GetServlet
  
  
    getc
    /getc
  

2、获取初始化参数

public class InitpServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        String url = context.getInitParameter("url");
        resp.getWriter().print(url);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
  
  
    url
    jdbc:mysql://localhost:3306/mybatis
  
  
    initp
    com.dandelion.servlet.InitpServlet
  
  
    initp
    /initp
  

3、请求转发

public class RfServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
//        RequestDispatcher requestDispatcher = context.getRequestDispatcher("/initp");//转发的请求路径
//        requestDispatcher.forward(req,resp);//调用forward实现请求转发
        context.getRequestDispatcher("/initp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
  
    rf
    com.dandelion.servlet.RfServlet
  
  
    rf
    /rf
  

4、读取资源文件
Properties
在Java目录下新建properties
在resources目录下新建properties
发现:都被打包到了同一个路径下:classes,称为classpath;
思路:需要一个文件流;

public class GetpServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
        Properties prop = new Properties();
        prop.load(is);
        String user = prop.getProperty("username");
        String pwd = prop.getProperty("password");
        resp.getWriter().print(user+":"+pwd);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
  
    getp
    com.dandelion.servlet.GetpServlet
  
  
    getp
    /getp
  
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/880379.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号