ServletConfig和ServletContext

ServletConfig对象

## 使用getServletConfig方法创建对象,并在配置文件进行初始化配置 ##
//ServletConfig程序,读取配置文件
`package com.heima.test;

import java.io.IOException;
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 ServletConfig01 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    ServletConfig config = getServletConfig();
    String username = config.getInitParameter("username");
    String password = config.getInitParameter("password");
    System.out.println(username + " " + password);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    doGet(request, response);
}
}`

//配置文件xml
`<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name></display-name>
  <servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ServletConfig</servlet-name>
<servlet-class>com.heima.test.ServletConfig01</servlet-class>
<init-param> <param-name>username</param-name><param-value>root</param-value> </init-param>
<init-param> <param-name>password</param-name><param-value>123</param-value> </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletConfig</servlet-name>
    <url-pattern>/demo1</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>`

ServletContext对象

## 使用getServletContext方法创建对象,配置文件xml ##
# 代码实现统计访问网站次数   #
//配置文件
`<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name></display-name>
    <servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ServletCount</servlet-name>
<servlet-class>com.heima.test.ServletCount</servlet-class>
  </servlet>
  <servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ServletShow</servlet-name>
<servlet-class>com.heima.test.ServletShow</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>ServletCount</servlet-name>
    <url-pattern>/count</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>ServletShow</servlet-name>
    <url-pattern>/show</url-pattern>
  </servlet-mapping>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
`

//统计次数代码
`package com.heima.test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletCount extends HttpServlet {


@Override
public void init() throws ServletException {
    ServletContext context = getServletContext();
    context.setAttribute("num", 0);
}


public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    ServletContext context = getServletContext();
    int num = (Integer) context.getAttribute("num");
    context.setAttribute("num", num+1);
    response.getWriter().write("welcome to website");
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    doGet(request, response);
}

}
`

//显示次数代码
`package com.heima.test;

import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletShow extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    ServletContext context = getServletContext();
    int num = (Integer) context.getAttribute("num");
    response.getWriter().write("times:" + num);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    doGet(request, response);
}

}
`
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值