码迷,mamicode.com
首页 > 其他好文 > 详细

servlet_3

时间:2017-09-22 00:55:50      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:初始化   localhost   except   etc   .config   下载   generated   response   地址   

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

}

}

servlet_3

标签:初始化   localhost   except   etc   .config   下载   generated   response   地址   

原文地址:http://www.cnblogs.com/fanzhengzheng/p/7572128.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!