一.建立项目servlet01

在入门Servlet项目中建立一个子项目模块(此处不再赘述如何建立),补全maven项目中的java和resources文件夹,添加类HelloServlet.java,添加web.xml中的映射关系,添加tomcat (注意保持只有一个jar包方式把所有项目的jar都打进来了,删除其他项目的jar包,添加自己的jar包),运行验证是否搭建正确!

二.ServletContext对象

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

ServletContext对象的几个重点所用:

  1. 共享数据:对于几个不同的Servlet之间可以相互传递数据

  2. 获取初始化参数:在web.xml中配置的参数,可以被或得到

  3. 请求转发:获得请求转发给别的Servlet

  4. 读取资源文件:此处需要注意配置资源过滤防止读取不了资源文件

1.共享数据: 在servlet中保存的数据可以在另一个servlet中被访问

例子:

在HelloServlet中代码

 @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // this.getInitParameter(); 初始化参数
// this.getServletConfig(); Servlet配置
// this.getServletContext(); Servlet上下文 ServletContext context = this.getServletContext(); String username = "小王";
context.setAttribute("username",username); //将数据存入ServletContext中 }

在GetServlet中的代码

 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"); //获取别的Servlet所保存的参数,实现数据共享 resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().println("名字"+ username); } @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}

配置web.xml

 <!--注册Servlet-->
<servlet>
<servlet-name>getc</servlet-name>
<servlet-class>ustc.wzh.servlet.GetServlet</servlet-class>
</servlet> <!--Servlet的请求路径-->
<servlet-mapping>
<servlet-name>getc</servlet-name>
<url-pattern>/getc</url-pattern>
</servlet-mapping>

启动tomcat执行程序

先执行http://localhost:8080/servlet01/hello 此时已经存入数据了,在执行http://localhost:8080/servlet01/getc 显示数据

执行成功!

2.获取初始化参数配置

新建Java类为GetParam

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

在web.xml中编写初始化配置项和映射路径

     <!--配置一些web应用初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param> <!--注册Servlet-->
<servlet>
<servlet-name>getParam</servlet-name>
<servlet-class>ustc.wzh.servlet.GetParam</servlet-class>
</servlet> <!--Servlet的请求路径-->
<servlet-mapping>
<servlet-name>getParam</servlet-name>
<url-pattern>/getParam</url-pattern>
</servlet-mapping>

重新部署tomcat,获取参数成功!

3.请求转发

新建Java类为GetRequestForward

 public class GetRequestForward extends HttpServlet {

     @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("开始请求转发!");
ServletContext context = this.getServletContext(); //设置请求转发路径,再调用forward实现请求转发
context.getRequestDispatcher("/getParam").forward(req,resp); System.out.println("转发成功!");
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}

在web.xml中编写映射路径

     <!--注册Servlet-->
<servlet>
<servlet-name>getReqFor</servlet-name>
<servlet-class>ustc.wzh.servlet.GetRequestForward</servlet-class>
</servlet> <!--Servlet的请求路径-->
<servlet-mapping>
<servlet-name>getReqFor</servlet-name>
<url-pattern>/getReqFor</url-pattern>
</servlet-mapping>

重新部署tomcat,转发成功!

4.读取资源文件

预处理:

由于我们在java目录下建立的properties和resources目录下建立的properties都被打报道同一路径下:classes,我们俗称这个路径为classpath;

1.建立properties文件:在resources目录下建立db.properties,在java目录下建立aa.properties

username=wzh password=123456

username=wzh02 password=123456890

2.修改当前项目下的pom.xml文件,添加配置

         <!--在build中配置resources,来防止我们资源导出失败的问题-->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>

3.编写读取配置文件的类:GetText

 public class GetText extends HttpServlet {

     @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //读取resources目录下的db.properties内容
InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties"); Properties properties = new Properties();
properties.load(in);
String username = properties.getProperty("username");
String password = properties.getProperty("password"); resp.getWriter().println(username + ":" + password); //读取java目录下的aa.properties内容
InputStream in2 = this.getServletContext().getResourceAsStream("/WEB-INF/classes/ustc/wzh/servlet/aa.properties"); Properties properties2 = new Properties();
properties2.load(in2);
String username2 = properties2.getProperty("username");
String password2 = properties2.getProperty("password"); resp.getWriter().println(username2 + ":" + password2); } @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}

注意:两个文件名是Rebuild project之后产生的

修改web.xml中的映射关系

     <!--注册Servlet-->
<servlet>
<servlet-name>getText</servlet-name>
<servlet-class>ustc.wzh.servlet.GetText</servlet-class>
</servlet> <!--Servlet的请求路径-->
<servlet-mapping>
<servlet-name>getText</servlet-name>
<url-pattern>/getText</url-pattern>
</servlet-mapping>

重新部署tomcat,读取资源文件成功!

最新文章

  1. ASP.NET MVC5+EF6+EasyUI 后台管理系统(30)-本地化(多语言)
  2. css之定位
  3. Linux文件权限和访问模式
  4. ArrayList常用操作
  5. SilverLight抛出 System.InvalidOperationException: 超出了2083 的最大URI
  6. iOS-Xcode使用技巧
  7. Redis学习笔记8--Redis发布/订阅
  8. [转载]tslib1.4与Qt4.8.6的交叉编译与移植
  9. [转]android访问网络:java.net.ConnectException: localhost/127.0.0.1:8888 - Connection refused
  10. 20145218《Java程序设计》第一周学习总结
  11. cocos2d-x 添加背景音乐和音效-SimpleAudioEngine
  12. POJ 2559 Largest Rectangle in a Histogram -- 动态规划
  13. [BZOJ 1033] [ZJOI2008] 杀蚂蚁antbuster 【模拟!】
  14. oracle存储过程 --1
  15. js void运用
  16. 区域及分离、Js压缩、css、jquery扩展
  17. 移动端 -webkit-user-select:text; ios10 bug 解决方案
  18. HttpURLConnection用法
  19. js原型与原型链探究
  20. SpringBoot学习笔记&lt;二&gt;注解

热门文章

  1. IDEA开发React环境配置
  2. MySQL之简介以及数据类型(一)
  3. k8s+jenkins(DevOps全流程)
  4. 2019 学而思java面试笔试题 (含面试题解析)
  5. 【转载】使用宝塔Linux面板屏蔽某些IP访问你的服务器
  6. Unity编辑器扩展中,使用Unity自带的GUIStyle
  7. 大数据:Hadoop(JDK安装、HDFS伪分布式环境搭建、HDFS 的shell操作)
  8. Hexo 文章图片添加水印,不用云处理
  9. Ansible--Ansible之Roles
  10. go语言学习笔记(一):*和&amp;的区别