servlet_3

package com.atguigu.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class BServlet implements Servlet {
private ServletConfig config;

//@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
/*
* ServletContext
* 代表整个web应用,每个web应用都有他唯一对应的ServletCentext对象,该对象在项目启动时创建,在项目销毁时卸载
* 获取:通过ServletConfig获取
* 功能:1,获得整个WEB应用的初始化参数
* 2,可以获取到资源的真实路径
* 虚拟路径:http://localhost:8888/9_14_servlet/BServlet
* 真实路径:资源在硬盘中的地址
*
*
*/

this.config=config;
}

//@Override
public ServletConfig getServletConfig() {
// TODO Auto-generated method stub
return this.config;
}

//@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException {
//获取全局的初始化参数
ServletContext context = this.getServletConfig().getServletContext();
String url = context.getInitParameter("url");
System.out.println("url="+url);
//获取index.html的真实地址
//主要用于做上传和下载
String realPath=context.getRealPath("/index.html");
System.out.println("realPath");

// TODO Auto-generated method stub

}

//@Override
public String getServletInfo() {
// TODO Auto-generated method stub
return null;
}

//@Override
public void destroy() {
// TODO Auto-generated method stub

}

}

时间: 2024-10-27 07:33:02

servlet_3的相关文章