码迷,mamicode.com
首页 > 编程语言 > 详细

Java Web开发入门之Servlet代码篇(三)

时间:2015-01-18 02:01:37      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

1. ServletConfig获取参数&测试实例化/初始化/销毁方法

public class Servlet01_ServletConfig extends HttpServlet {

	public Servlet01_ServletConfig() {
		System.out.println("实例化~~~~~~~~~~");
	}
	
	@Override
	public void init() throws ServletException {
		System.out.println("初始化啦-~~~~~~~~~~");
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		ServletConfig config = getServletConfig();
		response.getOutputStream().write(config.getInitParameter("cgx").getBytes());
		response.getOutputStream().write("<br/>hahawoshi 01 <br/>".getBytes());
		
	}

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

		doGet(request, response);
	}
	
	@Override
	public void destroy() {
		System.out.println("销毁了~~~~~~~~~~~");
	}

}

 

2. 熟悉ServletContext域

public class Servlet02_ServletContext extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		Enumeration<String> names = getServletContext().getInitParameterNames();
		while(names.hasMoreElements()) {
			String name = names.nextElement();
			String value = getServletContext().getInitParameter(name);
			System.out.println(name + ">>>" + value);
		}
		
		//COMM--setAttribute---------ServletContext域间访问-------------------
		ServletContext context = getServletContext();
		context.setAttribute("web", "haha....");
	}

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

		doGet(request, response);
	}

}

-------------------------getAttribute()-------------------------------
public class Servlet02_GetServletContext extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		//Context域通信
		String value = (String)getServletContext().getAttribute("web");
		response.getOutputStream().write(value.getBytes());
	}

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

		doGet(request, response);
	}

}

 

3. 转发与包含

public class Servlet03_Dispatcher extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		RequestDispatcher relay = getServletContext().getRequestDispatcher("/servlet/Servlet01_ServletConfig");
//		relay.forward(request, response);
		
		relay.include(request, response);
	}

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

		doGet(request, response);
	}

}

 

4. 访问资源路径

public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//		方式1,通过ServletContext获取资源路径
//		testGetRealPath(response);
		
//		方式2,通过ResourceBoudle获取资源路径,只能获取src资源,一般只用写基名即可
//		testResourceBundle(response);

//		方式3,通过ClassLoader类加载器获取资源路径
		testClassLoader(response);
	}

	/**
	 * @param response
	 * @throws IOException
	 */
	private void testClassLoader(HttpServletResponse response)
			throws IOException {
		ClassLoader loader = Servlet04_GetResource.class.getClassLoader();
//		InputStream is = loader.getResourceAsStream("com/itheima/day07Home/cfg.properties");
//		InputStream is = loader.getResourceAsStream("cfg2.properties");
		InputStream is = loader.getResourceAsStream("../cfg3.properties");
		
		Properties prop = new Properties();
		prop.load(is);
		response.getOutputStream().write(prop.getProperty("p").getBytes());
	}

	/**
	 * @param response
	 * @throws IOException
	 */
	private void testResourceBundle(HttpServletResponse response)
			throws IOException {
//		ResourceBundle rb = ResourceBundle.getBundle("com.itheima.day07Home.cfg");
		ResourceBundle rb = ResourceBundle.getBundle("cfg2");
		response.getOutputStream().write(rb.getString("p").getBytes());
	}

	private void testGetRealPath(HttpServletResponse response)
			throws IOException, FileNotFoundException {
//		String absolutePath = getServletContext().getRealPath("WEB-INF/classes/com/itheima/day07Home/cfg.properties");
//		String absolutePath = getServletContext().getRealPath("WEB-INF/classes/cfg2.properties");
		String absolutePath = getServletContext().getRealPath("WEB-INF/cfg3.properties");
		Properties prop = new Properties();
		prop.load(new FileInputStream(absolutePath));
		response.getOutputStream().write(prop.getProperty("p").getBytes());
		System.out.println(prop.getProperty("p"));
	}

 

5. 文件下载

public class Servlet05_Download extends HttpServlet {

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

		String path = getServletContext().getRealPath("WEB-INF/classes/1.jpg");
		response.setHeader("Content-Disposition", "attachment;filename=2.jpg");
		response.setHeader("Content-Type", "application/octet-stream");
		InputStream is = new FileInputStream(path);
		OutputStream os = response.getOutputStream();
		
		byte buf[] = new byte[1024];
		int len = -1;
		while((len=is.read(buf)) != -1) {
			os.write(buf, 0, len);
		}
		os.close();
		is.close();
	}

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

		doGet(request, response);
	}

}

Java Web开发入门之Servlet代码篇(三)

标签:

原文地址:http://www.cnblogs.com/codingpark/p/4231381.html

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