Context的用途
提示:这里所用的编译器为idea,其tomcat和maven都已配置完成。
提示:以下是本篇文章正文内容,下面案例可供参考
一、共享数据
示例:创建一个helloservlet.java通过调用context.setAttribute()方法,将数据保存到了ServletContext中,然后再创建一个GetContext.java从ServletContext中读取里面的存过内容。
代码示例:
helloservlet.java如下
public class helloservlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context=this.getServletContext();
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
String name="大大眼";
context.setAttribute("name",name);//将数据保存到了ServletContext中,名字为:name,值为:name
resp.getWriter().print("进行存入操作");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
在web.xml中配置servlet和mapping
<!--web.xml中是配置我们web的核心应用-->
<!--注册Servlet-->
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.llp.servlet.helloservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/maintext</url-pattern>
</servlet-mapping>
GetContext.java代码如下
public class GetContext extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context=this.getServletContext();
String name=(String) context.getAttribute("name");
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
resp.getWriter().print("姓名:"+name);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
在web.xml中配置servlet和mapping
<servlet>
<servlet-name>getContext</servlet-name>
<servlet-class>com.llp.servlet.GetContext</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getContext</servlet-name>
<url-pattern>/get</url-pattern>
</servlet-mapping>
运行结果展示(提示:先存入在读取)
二、获取初始化参数
代码示例
ContextDm1.java代码
public class ContextDm1 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);
}
}
在web.xml中配置servlet和mapping(这里的mysql://localhost:3306/databaseName是一个随机设置的)
<context-param>
<param-name>url</param-name>
<param-value>mysql://localhost:3306/databaseName</param-value>
</context-param>
<servlet>
<servlet-name>ContextDm1</servlet-name>
<servlet-class>com.llp.servlet.ContextDm1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ContextDm1</servlet-name>
<url-pattern>/Dm1</url-pattern>
</servlet-mapping>
运行结果展示
三、请求转发
请求转发,是服务器的行为,请求由服务器转发给另外一个页面处理,如何转发,何时转发,转发几次,客户端是不知道的。
请求转发时,从发送第一次到最后一次请求的过程中,web容器创建一次request和response对象,新的页面继续处理同一个请求。也可以理解为服务器将request对象在页面之间传递。
代码示例
ContextDm2.java代码
public class ContextDm2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context=this.getServletContext();
context.getRequestDispatcher("/Dm1").forward(req,resp);//转发请求路径、调用forward请求转发
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
在web.xml中配置servlet和mapping
<servlet>
<servlet-name>ContextDm2</servlet-name>
<servlet-class>com.llp.servlet.ContextDm2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ContextDm2</servlet-name>
<url-pattern>/Dm2</url-pattern>
</servlet-mapping>
运行结果展示
四、读取资源文件
Properties
在java目录下新建properties
在resources目录下新建properties
发现:都被打包到了同一个路径下:classes,我们俗称这个路径为classpath
思路:需要一个文件流;
首先在resources创建一个文件明和格式如下:
文件里面内容如下
代码示例
ContextDm3.java代码
public class ContextDm3 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 username=prop.getProperty("username");
String password=prop.getProperty("password");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
resp.getWriter().println("<h1>username : "+username+"</h1>");
resp.getWriter().println("<h1>password : "+password+"</h2>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
在web.xml中配置servlet和mapping
<servlet>
<servlet-name>ContextDm3</servlet-name>
<servlet-class>com.llp.servlet.ContextDm3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ContextDm3</servlet-name>
<url-pattern>/Dm3</url-pattern>
</servlet-mapping>
重要语句分析
运行结果展示
总结
希望以上例子能增加大家对Context的认识与理解。