Servlet实例代码

测试代码

package servlet1;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class servlet_test extends HttpServlet {

    String message;
    //构造函数
    public servlet_test() {
        super();
    }
    public void init() throws ServletException{
        
        message = "this is message test";
    }
    //get方法请求
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println(message);
        
    }

    
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
    }
        public void destroy(){
            //nouthon
        }

}

测试效果如图:

Servlet实例代码_第1张图片
运行结果

你可能感兴趣的:(Servlet实例代码)