Servlet入门

Posted 跳动de手指

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet入门相关的知识,希望对你有一定的参考价值。

package cn.itcast.Aservlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 */
@WebServlet("/Aservlet")
public class Aservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Aservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.sendError(200);
        response.setDateHeader("expires", 1000*60*60*24);
        response.setIntHeader("Conten-Length", 492);
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
package cn.itcast.Aservlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
//servlet中的方法大多数不由我们调用而是服务器,并且对象也是由服务器创建
public class Bservlet implements Servlet {
    ServletConfig conf;
//生命周期方法,Servlet即将结束时Tomcat自动调用,只运行一次,目的释放非内存资源比如关闭IO流等;
    @Override
    public void destroy() {
        System.out.println("destroy");

    }
//获取servlet配置信息
    @Override
    public ServletConfig getServletConfig() {
        
        return conf;
    }
//获取servlet的信息
    @Override
    public String getServletInfo() {
        
        return "快乐的servlet";
    }
//生命周期方法,出生时自动运行,tomcat调用只运行一次
    @Override
    public void init(ServletConfig conf) throws ServletException {
        this.conf=conf;

    }
//生命周期方法,tomcat调用
    @Override
    public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
        

    }

}

 

以上是关于Servlet入门的主要内容,如果未能解决你的问题,请参考以下文章

Servlet入门总结及第一个Servlet程序

Servlet入门总结及第一个Servlet程序

Servlet入门总结及第一个Servlet程序 (转载)

Servlet快速入门

Atom编辑器入门到精通 Atom使用进阶

java web 开发入门 --- tomcat/servlet/jsp