• 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);
    	}
    
    }
    
  • 相关阅读:
    Struts2的%,#,$的区别,UI标签及其表单radio,checkbox,select回显数据(七)
    Struts2的控制标签库和数据标签库(六)
    Struts2从后端向前端传递数据和OGNL访问用户自定义静态方法(五)
    两个小例子登录和显示全部用户信息的模块(四)
    Struts2的ServletAPI的获取和各种类型的数据获取(三)
    Action的三种实现方式,struts.xml配置的详细解释及其简单执行过程(二)
    Struts2的 两个蝴蝶飞,你好 (一)
    虚拟机安装Centos7系统后优化操作
    Java进程故障排查(CPU资源占用高,接口响应超时,功能接口停滞等)
    zabbix企业微信报警实现
  • 原文地址:https://www.cnblogs.com/codingpark/p/4231381.html
Copyright © 2020-2023  润新知