zoukankan      html  css  js  c++  java
  • Java Web之Servlet代码篇(三)

    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);
    	}
    
    }
    
  • 相关阅读:
    test
    男神zyh的青睐
    HH的项链
    PAT刷题经验
    LaTeX常用数学符号
    Battle Over Cities Hard Version
    Cut
    文本生成器
    Explorer Space
    2021.04.21
  • 原文地址:https://www.cnblogs.com/codingpark/p/4231381.html
Copyright © 2011-2022 走看看