init() 👉 service() 👉 destroy()
@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 test 호출!!");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
//html을 응답으로 보내줌
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 req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
//요청결과 name값을 응답결과로 출력
String name = req.getParameter("name");
out.println("<h1> hello " + name + "</h1>");
out.close();
}
// protected void service(HttpServletRequest request, HttpServletResponse response)
// throws ServletException, IOException {
// System.out.println("service 호출");
// }
public void destroy() {
System.out.println("destroy 호출");
}
}
📍 실행 하기
클라이언트 요청 - GET 👉 doGet(request, response) 메소드 호출
해당 서블릿에 URL 주소를 직접 입력, 링크 클릭
out.println
에 문자열로 넣어주었던 코드들이 그대로 응답 결과로 출력된 것을 확인
<html>
<head><title>form</title></head>
<body>
<form method='post' action='/firstweb/LifecycleServlet'>
name : <input type='text' name='name'><br>
<input type='submit' value='ok'><br>
</form>
</body>
</html>
form 태그 속성 알고가기
속성명 | 속성값 | 설명 |
---|---|---|
method | GET, POST | 폼을 서버에 전송할 http 메소드 지정 |
action | URL | 폼 데이터를 전송할 서버 스크립트 파일 지정 |
URL에서 직접 요청했을 때 method 값은 GET
클라이언트 요청 - POST 👉 doPost(request, response) 메소드 호출
request 객체로부터 name
파라미터 값을 가져와 출력