Servlet取得初始化配置信息:
通过config对象可以读取web.xml中的配置的初始化参数,此对象实际上是ServletConfig借口的实例。可以通过init()方法找到ServletConfig接口实例。
读取初始化配置信息------InitParamServlet.Java
- package com.ls.servlet_01;
-
- import java.io.IOException;
- import java.io.PrintWriter;
-
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class InitParamServlet extends HttpServlet {
-
- String initParam=null;
- public void init(ServletConfig config) throws ServletException {
- this.initParam=config.getInitParameter("ref");
- }
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- System.out.println("初始化参数:"+this.initParam);
-
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- this.doGet(request, response);
- }
- }
在web.xml中配置初始化信息
- "1.0" encoding="UTF-8"?>
- "2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http:
- http:
-
- InitParamServlet
- class>com.ls.servletd_01.InitParamServletclass>
-
- ref
- liusheng
-
-
-
- InitParamServlet
- /InitParamServlet
-
-
- index.jsp
-
-
程序运行结果:在Tomcat后台打印
初始化参数:liusheng
用直接配置初始化
表示参数名称
表示参数的内容
注意:
在Servlet中初始化方法一共有init()和init(ServletConfig config)两个,如果两个初始化方法同时出现,则调用的是init(ServletConfig config)方法。
Servlet取得其他内置对象:
1、取得HttpSession实例
在servlet程序中要想取得一个session对象,则可以通过HttpServletRequest接口完成,有以下操作方法:
public HttpSession getSession() 返回当前的session
public HttpSession getSession(boolean create) 返回当前的session,如果没有则创建一个新的session对象返回
取得HttpSession对象---HttpSessionservlet.java
- package com.ls.servlet_02;
-
- import java.io.IOException;
- import java.io.PrintWriter;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
-
- public class HttpSessionservlet extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- HttpSession ses = request.getSession();
- System.out.println("SESSION-->" + ses.getId());
- ses.setAttribute("username", "刘胜");
- System.out.println("username属性内容是:" + ses.getAttribute("username"));
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- this.doGet(request, response);
-
- }
-
- }
配置web.xml文件
- "1.0" encoding="UTF-8"?>
- "2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http:
- http:
-
- HttpSession
- class>com.ls.servlet_02.HttpSessionservletclass>
-
-
- HttpSession
- /servlet_HttpSession
-
-
- index.jsp
-
-
程序运行结果: Tomcat后台显示
本程序通过getSession()方法取得了一个HttpSession对象后,输出了Session Id以及属性的设置及取得操作。
2、取得ServletContext实例
application内置对象是ServletContext 接口的实例,表示的是Servlet上下文。如果要在一个servlet中使用此现象,直接通过GenericServlet类提供的方法即可:
public ServletContext getServletContext() 取得ServletContext对象
取得application对象-----ServletContext.java
- package com.ls.servlet_03;
-
- import java.io.IOException;
- import java.io.PrintWriter;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class ServletContext extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- javax.servlet.ServletContext app =super.getServletContext();
- System.out.println("真实路径:"+app.getRealPath("/"));
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- this.doGet(request, response);
- }
-
-
- }
配置web.xml文件
- "1.0" encoding="UTF-8"?>
- "2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http:
- http:
-
- ServletContext
- class>com.ls.servlet_03.ServletContextclass>
-
-
- ServletContext
- /servlet_ServletContext
-
-
- index.jsp
-
-
程序运行结果: Tomcat后台输出
本程序通过getServletContext()方法取得ServletContext实例后,将虚拟目录所对应的真实路径输出。