@WebServlet("/lifeCycleServlet")
public class lifeCycleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public lifeCycleServlet() {
System.out.println("LifeCycleServlet 생성");
}
public void init(ServletConfig config) throws ServletException {
System.out.println("init호출");
}
public void destroy() {
System.out.println("destory호출");
}
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("service호출");
}
}
/lifeCycleServlet
이 주소에 접속하면 서버는 해당 객체가 있는지 확인한다서블릿은 객체를 여러개 생성하지 않는다 요청이 여러번 들어오면 서비스만 호출한다.
service는 response request를 담당한다.
init은 리소스를 로드한다
destory는 로드한 리소스를 다시 언로드 한다.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>form</title></head>");
out.println("<body>");
out.println("<form method='post' action='/firstweb/LifecycleServlet'>");
out.println("name : <input type='text' name='name'><br>");
out.println("<input type='submit' value='ok'><br>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
out.close();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
//get에서 보낸 name값을 request로 받음
out.println("<h1> hello " + name + "</h1>");
out.close();
}