当前位置:   article > 正文

Servlet基础(续集)

Servlet基础(续集)

Servlet原理

Servlet是由Web服务器调用,Web服务器在收到浏览器请求之后,会:

Mapping问题

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

  1. <servlet-mapping>
  2. <servlet-name>hello</servlet-name>
  3. <url-pattern>/hello</url-pattern>
  4. </servlet-mapping>

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

  1. <servlet-mapping>
  2. <servlet-name>hello</servlet-name>
  3. <url-pattern>/hello</url-pattern>
  4. </servlet-mapping>
  5. <servlet-mapping>
  6. <servlet-name>hello</servlet-name>
  7. <url-pattern>/hello2</url-pattern>
  8. </servlet-mapping>
  9. <servlet-mapping>
  10. <servlet-name>hello</servlet-name>
  11. <url-pattern>/hello3</url-pattern>
  12. </servlet-mapping>
  13. <servlet-mapping>
  14. <servlet-name>hello</servlet-name>
  15. <url-pattern>/hello4</url-pattern>
  16. </servlet-mapping>
  17. <servlet-mapping>
  18. <servlet-name>hello</servlet-name>
  19. <url-pattern>/hello5</url-pattern>
  20. </servlet-mapping>

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

  1. <servlet-mapping>
  2. <servlet-name>hello</servlet-name>
  3. <url-pattern>/hello/*</url-pattern>
  4. </servlet-mapping>

 默认请求路径映射

  1. <servlet-mapping>
  2. <servlet-name>hello</servlet-name>
  3. <url-pattern>/*</url-pattern>
  4. </servlet-mapping>

 自定义后缀实现请求映射,注意点,*前面不能加映射的路径

  1. <servlet-mapping>
  2. <servlet-name>hello</servlet-name>
  3. <url-pattern>*.zzc</url-pattern>
  4. </servlet-mapping>

errorservlet例子:

  1. package com.kuang.servlet;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.http.HttpServlet;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import java.io.IOException;
  7. import java.io.PrintWriter;
  8. public class ErrorServlet extends HttpServlet {
  9. @Override
  10. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  11. resp.setContentType("text/html");
  12. resp.setCharacterEncoding("UTF-8");
  13. PrintWriter writer = resp.getWriter();
  14. writer.print("<h1>404</h1>");
  15. }
  16. @Override
  17. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  18. super.doPost(req, resp);
  19. }
  20. }
  21. //在servlet请求文件中写上:
  22. <servlet>
  23. <servlet-name>error</servlet-name>
  24. <servlet-class>com.kuang.servlet.ErrorServlet</servlet-class>
  25. </servlet>
  26. <servlet-mapping>
  27. <servlet-name>error</servlet-name>
  28. <url-pattern>/*</url-pattern>
  29. </servlet-mapping>

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



ServletContext

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

  • 共享数据:我在这个Servlet中保存的数据,可以在另外一个servlet中拿到;

  1. @Override
  2. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  3. // this.getInitParameter() 初始化参数
  4. // this.getServletConfig() Servlet配置
  5. // this.getServletContext() Servlet上下文
  6. ServletContext context = this.getServletContext();
  7. String username = "zzc";//数据
  8. context.setAttribute("username", username);//将一个数据保存在了ServletContext中,名字为:username、值:username
  9. System.out.println("Hello");
  10. }
  11. @Override
  12. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  13. //该步骤用于获取servlet01存放的context
  14. ServletContext context = this.getServletContext();
  15. String username = (String) context.getAttribute("username");
  16. //下面两个是用来识别中文汉字,以至于不会出现乱码
  17. resp.setContentType("text/html");
  18. resp.setCharacterEncoding("UTF-8");
  19. //注意在获取context前,首先需要让servlet01放入数据,我们再访问servlet02的网络
  20. resp.getWriter().println("名字"+username);
  21. }
  22. <!--先访问hello-->
  23. <servlet>
  24. <servlet-name>hello</servlet-name>
  25. <servlet-class>com.kuang.servlet.HelloServlet</servlet-class>
  26. </servlet>
  27. <servlet-mapping>
  28. <servlet-name>hello</servlet-name>
  29. <url-pattern>/hello</url-pattern>
  30. </servlet-mapping>
  31. <!--再访问getc-->
  32. <servlet>
  33. <servlet-name>getc</servlet-name>
  34. <servlet-class>com.kuang.servlet.GetServlet</servlet-class>
  35. </servlet>
  36. <servlet-mapping>
  37. <servlet-name>getc</servlet-name>
  38. <url-pattern>/getc</url-pattern>
  39. </servlet-mapping>

测试访问结果:

  • 获取初始化参数
  1. <!--配置一些web应用初始化参数-->
  2. <context-param>
  3. <param-name>url</param-name>
  4. <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
  5. </context-param>
  6. @Override
  7. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  8. //该步骤用于获取servlet01存放的context
  9. ServletContext context = this.getServletContext();
  10. String url = context.getInitParameter("url");
  11. resp.getWriter().print(url);
  12. }
  • 请求转发

 他路径是sd4,但是请求了/gp,所以就显示了gp的内容

  1. <servlet>
  2. <servlet-name>sd4</servlet-name>
  3. <servlet-class>com.kuang.servlet.ServletDome04</servlet-class>
  4. </servlet>
  5. <servlet-mapping>
  6. <servlet-name>sd4</servlet-name>
  7. <url-pattern>/sd4</url-pattern>
  8. </servlet-mapping>
  9. @Override
  10. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  11. ServletContext context = this.getServletContext();
  12. RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");//转发的请求路径
  13. //调用forward实现请求转发;
  14. requestDispatcher.forward(req, resp);
  15. }
  16. @Override
  17. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  18. super.doPost(req, resp);
  19. }
  20. }

转发概念:

重定向概念:

  • 读取资源文件

Properties

  • 在java目录下新建properties
  • 在resources目录下新建properties

发现:都被打包到同一路径下:classes,我们俗称这个路径为classpath

思路:构建一个 文件流

  1. <servlet>
  2. <servlet-name>sd5</servlet-name>
  3. <servlet-class>com.kuang.servlet.ServletDome05</servlet-class>
  4. </servlet>
  5. <servlet-mapping>
  6. <servlet-name>sd5</servlet-name>
  7. <url-pattern>/sd5</url-pattern>
  8. </servlet-mapping>
  9. @Override
  10. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  11. //该步骤用于获取所需东西,然后把资源变成一个流
  12. InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");//第一个/表示模块路径(当前web项目)
  13. Properties prop= new Properties();
  14. //load()加载数据
  15. prop.load(is);
  16. String user = prop.getProperty("username");
  17. String pwd = prop.getProperty("password");
  18. resp.getWriter().print(user+":"+pwd);
  19. }
  20. @Override
  21. protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  22. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/719389
推荐阅读
相关标签
  

闽ICP备14008679号