提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用
一、共享数据
在这个servlet中共享数据,可以在另一个servlet中拿到
设置servlet中的属性值的类
package com.xxx.servlet01;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String username="去噗";
servletContext.setAttribute("username",username);
System.out.println("hello");
}
}
web.xml配置映射
<servlet>
<servlet-name>hello01</servlet-name>
<servlet-class>com.xxx.servlet01.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello01</servlet-name>
<url-pattern>/he</url-pattern>
</servlet-mapping>
获取servlet中保存的属性值的类
package com.xxx.servlet01;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class GetServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String username =(String) servletContext.getAttribute("username");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().print("名字:"+username);
}
}
web.xml配置映射
<servlet>
<servlet-name>get</servlet-name>
<servlet-class>com.xxx.servlet01.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>get</servlet-name>
<url-pattern>/he1</url-pattern>
</servlet-mapping>
注:先运行第一个Java类(路径为/he)先将数据保存在ServletContext 再运行第二个Java类(路径为/he1)获取ServletContext中保存的内容
运行结果
二、获取参数
1.在web.xml中设置参数
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
2.获取参数
package com.xxx.servlet01;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ServletDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String url = this.getServletContext().getInitParameter("url");
resp.getWriter().print(url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
三、请求转发
getRequestDispatche为请求转发的意思
package com.xxx.servlet01;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ServletDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext=this.getServletContext();
servletContext.getRequestDispatcher("/he2").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
四、资源配置文件的获取
在resource目录创建db.properties资源配置文件
username=root
password=123456
将资源配置文件打印到浏览器上
package com.xxx.servlet01;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class ServletDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream is= this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties properties=new Properties();
properties.load(is);
String user=properties.getProperty("username");
String pwd=properties.getProperty("password");
resp.getWriter().print(user+":"+pwd);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
|