如果不熟悉的读者,可以参考之前讲解的JSP 内置对象中的config 对象的使用
因为所有的初始化参数都必须在web.xml 文件之中进行配置
- public void init(ServletConfig config)
- throws ServletException
- package org.gz.servlet;
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class InitParamServlet extends HttpServlet {
- private String initParam = null; //用于保存初始化参数
- public void init(ServletConfig config) throws ServletException {
- this.initParam = config.getInitParameter("ref"); //接受的参数化名称,暂时为 ref
- }
- public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException {
- System.out.println("***初始化参数: " + this.initParam);
- }
- public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException {
- this.doGet(req,resp);
- }
- }
- <servlet>
- <servlet-name>initParam</servlet-name>
- <servlet-class>org.gz.servlet.InitParamServlet</servlet-class>
- <span style="color:#3333FF;"><init-param>
- <param-name>ref</param-name>
- <param-value>www.baidu.com</param-value>
- </init-param></span>
- </servlet>
- <servlet-mapping>
- <servlet-name>initParam</servlet-name>
- <url-pattern>/InitParamServlet</url-pattern>
- </servlet-mapping>
但是在进行操作的时候有一个注意点必须引起注意,如果现在在Servlet 之中同时覆写了两个 init() 方法,则只有取得初始化参数的 init() 方法起作用
从一般的角度而言,取得初始化参数的操作在一些系统的架构中比较常用的,例如,之后要讲解的Struts 开发框架,就使用此种操作。
之前在JSP 中讲解的config 对象的使用,也是利用了同样的原理完成的,所有的初始化参数必须通过映射路径才可以取得
可以从Servlet 中取得session 对象并进行操作
可以从Servlet 中取得Application 对象并进行操作
取得HttpSession 实例
1、取得session对象
在Servlet 程序中要想取得一个 session 对象,则可以通过 HttpServletRequest 接口完成,在此接口中提供了以下的操作方法
public HttpSession getSession();
public HttpSession getSession(boolean create)
Servlet 本身提供的只有request 和 response 两个对象,所有如果要想取得session 对象,则只能依靠request 对象,因为session 属于HTTP 协议范畴,而且在每次发送请求的时候,服务器都会自动为客户端设置一个Cookie 所以,很自然的,Session 中要使用得到了Cookie 的机制,但是Cookie 又只能通过request 取得,那么自然session 也就只能通过request 取得了。
- package org.gz.servlet;
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class HttpSessionServlet extends HttpServlet {
- public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException {
- HttpSession hs = req.getSession();
- System.out.println("Session ID ---> " + hs.getId()); // 取得session ID
- hs.setAttribute("username","大哥大"); //设置session 属性
- System.out.println("username属性内容: " + hs.getAttribute("username"));
- }
- public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException {
- doGet(req,resp);
- }
- }
- /*
- <servlet>
- <servlet-name>sessiondemo</servlet-name>
- <servlet-class>org.gz.servlet.HttpSessionServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>sessiondemo</servlet-name>
- <url-pattern>/httpsession</url-pattern>
- </servlet-mapping>
- */
2、取得application 对象
在讲解如果取得application对象之前,先来回顾应一个问题,在之前讲解内置对象的时候一直强调公,对于application 对象可以通过 this.getServletContext() 方法取得
- public ServletContext getServletContext()
- package org.gz.servlet;
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class ServletContextServlet extends HttpServlet {
- public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException {
- ServletContext app = super.getServletContext();
- System.out.println("真实路径: " + app.getRealPath("/")); //真实路径: E:\webdemo\
- }
- public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException {
- doGet(req,resp);
- }
- }
通过getSession() 方法可以直接取得HttpSession 的实例化对象
通过getServletContext() 方法可以取得ServletContext 的实例化对象
取得了 session 之后就意味着可以进行登录验证了
通过getServletContext() 方法可以取得application 对象,而之前调用的 this.getServletContext() 方法完成的也是这样的功能。