servlet学习笔记(一)-servlet的创建

sevlet 的三种创建方式
package com.tsinghua;
import javax.servlet.*;
import java.io.*;
public class Hello implements Servlet{

	//销毁Servlet实例(释放内存)
	//1.reload 该serlvet(webApps) 2.关闭tocat 3关机
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("destroy!");
	}

	//得到Servlet配置文件,视频没详讲
	@Override
	public ServletConfig getServletConfig() {
		// TODO Auto-generated method stub
		return null;
	}

	//视频没讲
	@Override
	public String getServletInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	//该函数用于初始化该Servlet(类似于类的构造函数)
	//该函数只会被调用一次(当用户第一次访问该Servlet时被调用)
	@Override
	public void init(ServletConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("init it");
	}

	//这个韩式用于处理业务逻辑
	//程序员应当把业务逻辑代码写在这里
	//当用户每访问该Servlet时,都会被调用
	//req 用于获得客户端(浏览器)的信息
	//res 用于向客户端(浏览器)返回信息
	@Override
	public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("service it");
		//从res中得到PrintWriter
		PrintWriter pw=res.getWriter();
		pw.println("hello,world");
	}

}

package com.tsinghua;
import javax.servlet.*;
import java.io.*;
public class HelloGen extends GenericServlet{

	//
	@Override
	public void service(ServletRequest rep, ServletResponse res) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//返回hello.world!generic
		PrintWriter pw=res.getWriter();
		pw.println("hello.world!generic");
	}

}

package com.tsinghua;

import javax.servlet.http.*;
import java.io.*;

public class HelloHttp extends HttpServlet{
	public void doGet(HttpServletRequest req,HttpServletResponse res){
		
		try {
			PrintWriter pw = res.getWriter();
			pw.println("Hello,http");
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void doPose(HttpServletRequest req,HttpServletResponse res){
		this.doGet(req, res);
	}
}
web.xml中的配置:


  
    
    LoginClServlet
    
    com.controller.LoginClServlet
  


  
    
    UserClServlet
    
    /UserClServlet
  


你可能感兴趣的:(servlet)