zoukankan      html  css  js  c++  java
  • servlet学习

    demo:http://pan.baidu.com/s/1nuTzMkT

    package com.ws.study.servlet.huoquziyuan;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.apache.commons.io.IOUtils;
    
    public class servlethuoqu2 extends HttpServlet {
    	private int count;
    
    	/**
    	 * Constructor of the object.
    	 */
    	public servlethuoqu2() {
    		super();
    	}
    
    	/**
    	 * The doGet method of the servlet. <br>
    	 *
    	 * This method is called when a form has its tag value method equals to get.
    	 * 
    	 * @param request the request send by the client to the server
    	 * @param response the response send by the server to the client
    	 * @throws ServletException if an error occurred
    	 * @throws IOException if an error occurred
    	 */
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		ServletContext srvcon=super.getServletContext();
    		if("".equals(count)){
    			
    			count=1;
    		}else{
    			count++;
    		}
    		System.out.println(srvcon.getAttribute("username"));
    		System.out.println("调用了"+count+"次");
    		/*
    		 * 用Classloader载入到WEB-INFclasses目录内然后获取
    		 */
    		
    //		ClassLoader classl=this.getClass().getClassLoader();
    //		InputStream input=classl.getResourceAsStream("a.txt");
    //		System.out.println(IOUtils.toString(input));
    		/*
    		 * 用Class载入到当前类路径下 加斜杠载入到WEB-INFclasses下。
    		 */
    //		Class clas=this.getClass();
    //		InputStream input=clas.getResourceAsStream("/../../index.jsp");
    //		System.out.println(IOUtils.toString(input));
    		
    		/*
    		 * 用servletContext获取文件内容
    		 */
    		InputStream input=srvcon.getResourceAsStream("/index.jsp");
    		System.out.println(IOUtils.toString(input));
    	//	System.out.println(srvcon.getResourcePaths("/"));
    		
    		
    		response.setContentType("text/html");
    		response.setCharacterEncoding("UTF-8");
    		PrintWriter out = response.getWriter();
    		out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
    		out.println("<HTML>");
    		out.println("  <HEAD>" +
    				"<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>" +
    				"<TITLE>A Servlet</TITLE></HEAD>");
    		out.println("  <BODY>");
    		out.println("test 测试");
    		out.print(this.getClass());
    		out.println("  </BODY>");
    		out.println("</HTML>");
    		out.flush();
    		out.close();
    	}
    	
    	
    	@Override
    	public void init() throws ServletException {
    		// TODO Auto-generated method stub
    		System.out.println("我诞生了,我要setAttribute");
    		super.getServletContext().setAttribute("username", "hackermi");
    	}
    	
    	
    	
    
    }
    

      

    package com.ws.study.servlet;
    
    import java.io.IOException;
    
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    
    public class servletC extends servletAdd {
    
    	@Override
    	public void service(ServletRequest req, ServletResponse res)
    			throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		System.out.println("service调用了一次");
    		this.getInitParameterNames();
    	}
    
    	@Override
    	protected void init() {
    		// TODO Auto-generated method stub
    		System.out.println("servlet诞生了");
    	}
    	
    	public void getInitParameterNames(){
    		System.out.println("获取参数");
    		enat=servletconfig.getInitParameterNames();
    		while (enat.hasMoreElements()) {
    			String canshu = (String) enat.nextElement();
    			System.out.println(canshu);
    		}
    	}
    
    	
    	
    	
    
    }
    

      

    package com.ws.study.servlet;
    
    import java.io.IOException;
    import java.util.Enumeration;
    
    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 servletAdd implements Servlet {
    
    	public Enumeration<String> enat;
    	protected ServletConfig servletconfig;
    	public servletAdd() {
    		// TODO Auto-generated constructor stub
    	}
    
    	@Override
    	public void destroy() {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public ServletConfig getServletConfig() {
    		// TODO Auto-generated method stub
    		return this.servletconfig;
    	}
    
    	@Override
    	public String getServletInfo() {
    		// TODO Auto-generated method stub
    		return null;
    	}
    
    	@Override
    	public void init(ServletConfig servletConfig) throws ServletException {
    		// TODO Auto-generated method stub
    		this.servletconfig=servletConfig;
    		enat=servletConfig.getInitParameterNames();
    		while (enat.hasMoreElements()) {
    			String canshu = enat.nextElement();
    			System.out.println(servletConfig.getInitParameter(canshu));
    		}
    		init();
    	}
    
    	@Override
    	public void service(ServletRequest req, ServletResponse res)
    			throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		System.out.println("servletAdd被调用了一次");
    		enat=servletconfig.getInitParameterNames();
    		while (enat.hasMoreElements()) {
    			String canshu = enat.nextElement();
    			System.out.println(canshu);
    		}
    		
    		System.out.println(this.getServletContext());
    	}
    	
    	public ServletContext getServletContext(){
    		return this.servletconfig.getServletContext();
    	}
    	
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    
    	}
    	
    	protected void init() {
    		// TODO Auto-generated method stub
    
    	}
    
    }
    

      

  • 相关阅读:
    import和include的区别
    $sformat用法
    如何快速理解DUT
    vim_basic
    UVM——寄存器模型相关的一些函数
    AMBA——总线仲裁
    Cache的写回策略(转)
    Cache直接映射、组相连映射以及全相连映射(转载)
    一起学IC验证:推荐资料合集,收藏专用(转载)
    VCS仿真流程
  • 原文地址:https://www.cnblogs.com/hackermi/p/5363656.html
Copyright © 2011-2022 走看看