jdk8
tomcat8.5
Dynamic Web Project 3.1
IDE : 이클립스 javaEE
package examples;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
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("/LifecycleServlet")
public class LifecycleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// Run 서버 시작시,
// LifecycleServlet 생성!
// init 호출!
// service 호출!
public LifecycleServlet() {
System.out.println("LifecycleServlet 생성!");
}
@Override
protected void doGet(HttpServletRequest reqest, 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'>"); // submit버튼이 눌렸을 때 post 메서드로 요청하라는 뜻
out.println("name : <input type='text' name='name'><br>"); // input칸 넣어주는 타입은 text
out.println("<input type='submit' value='ok'><br>"); // submit버튼
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");
out.println("<h1> hello " + name + "</h1>");
out.close();
}
public void init(ServletConfig config) throws ServletException {
System.out.println("init test 호출!");
}
public void destroy() {
System.out.println("destroy 호출!"); // was가 종료되거나 서버가 돌아가는 중에 코드 수정시 호출
}
//
// protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// System.out.println("service 호출!"); // 새로고침이나 다시 접속시 service 호출
// }
}
참고 강의
https://www.boostcourse.org/web326/lecture/258505/?isDesc=false
'프로그래밍 > java' 카테고리의 다른 글
클라이언트 ip 주소 및 프로젝트 uri url contentPath 받아오기 (0) | 2021.12.21 |
---|---|
클라이언트 헤더 정보 읽어오기 (0) | 2021.12.21 |
Servlet 생성주기와 라이프 사이클 (0) | 2021.12.21 |
java_IntelliJ(IDE) 설치_Windows (0) | 2021.09.06 |
java_IntelliJ(IDE) 설치_Mac (0) | 2021.09.06 |