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

Servlet学习记录

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

Servlet学习记录

Servlet
  • 1.Servlet简介
  • 2.HellowServlet
    • (1).构建项目
    • (2).关于`maven`父子工程:
    • (3).maven环境优化
    • (4).编写一个Servlet程序
    • (5).编写`servlet`的映射
    • (6).配置`tomcat`
    • (7).启动测试
  • 3.Servlet原理
  • 4.Mapping
    • (1).一个Servlet可以指定一个映射路径
    • (2).一个Servlet可以指定多个映射路径
    • (3).一个Servlet可以指定通用映射路径
    • (4).默认请求路径
    • (5).指定后缀或者前缀
    • (6).优先级问题
  • 5.ServletContext
    • (1)共享数据
    • (2)获取初始化参数
    • (3)请求转发
    • (4)读取资源文件

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

servlet接口sun公司有两个默认的实现类:HttpServlet和GenericServlet

(1).构建项目

构建一个普通的maven项目,删除src目录,之后在该项目离建立module;这个空的工程是主工程

(2).关于maven父子工程:

父项目中:

	
		HelloServlet
	

子项目会有:

    
        servlet
        com.young
        1.0-SNAPSHOT
    

父项目中的java子项目可以直接使用
son extends father

(3).maven环境优化

①.修改web.xml为最新的
②.将maven的结构搭建完整

(4).编写一个Servlet程序

①.编写一个普通类
②.实现servlet接口,直接继承HttpServlet.

public class HelloServlet extends HttpServlet {
    //由于get和post只是请求实现的不同的方式,可以相互调用,业务逻辑一样
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();//响应流
        writer.print("hello servlet.");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
(5).编写servlet的映射

我们写的是java程序,但是要通过浏览器访问,而浏览器需要连接web服务器,所以我们需要在web服务中注册我们写的servlet,还需要给他一个能够访问的路径;

 
    
        hello
        com.young.servlet.HelloServlet
    
    
    
        hello
        /hello
    
(6).配置tomcat (7).启动测试 3.Servlet原理

servlet是由web服务器调用,web服务器在收到浏览器请求后,

4.Mapping (1).一个Servlet可以指定一个映射路径
	
        hello
        /hello
    
(2).一个Servlet可以指定多个映射路径
	
	
        hello
        /hello1
    
    
    
        hello
        /hello2
    
    
    
        hello
        /hello3
    
(3).一个Servlet可以指定通用映射路径
	
        hello
        /hello/*
    
(4).默认请求路径
	
        hello
        /*
    
(5).指定后缀或者前缀
	
	
        hello
        *.yangyi
    
(6).优先级问题

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

	
    
        error
        com.young.servlet.ErrorServlet
    
    
    
        error
        /*
    
5.ServletContext

web容器在启动的时候,它会为每个web程序都创建一个对应ServletContext对象,它代表了当前的web应用:

(1)共享数据

在一个servlet中保存的数据,可以在另一个servlet中拿到

//存放数据
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        writer.print("sc_hello servlet");
        ServletContext servletContext = this.getServletContext();//上下文
        String username = "杨一";//数据
        servletContext.setAttribute("username",username);//将一个数据保存在了servletcontext中
    }
}
//获取存放的数据
public class GetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        ServletContext servletContext = this.getServletContext();
        String username = (String) servletContext.getAttribute("username");
        resp.getWriter().print("用户名:"+username);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
    
        hello
        com.young.servlet.HelloServlet
    
    
        hello
        /hello
    

    
        getname
        com.young.servlet.GetServlet
    
    
        getname
        /getname
    

测试访问结果:
①、先进入http://localhost:8080/sc/hello存放数据,否则http://localhost:8080/sc/getname页面获取不到数据会显示为空
②、获取数据后,进入http://localhost:8080/sc/getname,显示:

(2)获取初始化参数
    
    
        url
        jdbc:mysql://localhost:3306/servlet
    
    
        gp
        com.young.servlet.GPServlet
    
    
        gp
        /gp
    
 public class GPServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        ServletContext servletContext = this.getServletContext();
        String url = servletContext.getInitParameter("url");
        resp.getWriter().print(url);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
(3)请求转发
public class RDServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        System.out.println("查看是否进入该路径/rd");
        RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/gp");//转发的请求路径
        requestDispatcher.forward(req,resp);//调用forward方法实现请求转发
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
(4)读取资源文件

Properties

  • 在java目录下新建properties
  • 在resources目录下新建properties
    都被打包到同一路径下:classes,俗称这个路径为类路径;
    思路:需要一个文件流;
public class PServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //java目录下相对应的类路径
        InputStream resourceAsStream = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        String username = properties.getProperty("username");
        String password = properties.getProperty("password");
        resp.getWriter().print(username);
        resp.getWriter().println(password);
        //properties目录下相对应的类路径
        InputStream rs = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/young/servlet/aa.properties");
        Properties ps = new Properties();
        ps.load(rs);
        String uname = ps.getProperty("username");
        String pword = ps.getProperty("password");
        resp.getWriter().print(uname);
        resp.getWriter().print(pword);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
    
        p
        com.young.servlet.PServlet
    
    
        p
        /p
    

运行测试:启动项目,进入路径http://localhost:8080/sc/p,成功获取到properties文件下的数据。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1035135.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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