클래스 생성
상속규칙2) javax.servlet.http.HttpServlet 클래스를 상속해서 사용
public class MyServlet extends HttpServlet{
}
상속규칙2) doGet(), doPost() 메서드를 선언 (오버라이딩)
// alt shift s v
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
상속규칙 4) java.io.IOException, java.servlet.ServletException을 통한 예외 처리(예외던지기)
// alt shift s v
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("MyServlet-doGet() 실행");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("MyServlet-doPost() 실행");
}
없으면 당황하지 말고 만들기~
이름 우클릭 java ee tools ➡️ generate deployment descriptor sub
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<display-name>JSP7</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- web.xml (배포서술자) : 서블릿을 매핑한다. 즉, 프로그램 시작할 때 빌리는 곳 -->
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.itwillbs.servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/myServlet</url-pattern>
</servlet-mapping>
</web-app>
package com.itwillbs.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet{
// <http://localhost:8088/JSP7/myServlet>
// alt shift s v
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("MyServlet-doGet() 실행");
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("MyServlet-doPost() 실행");
}
}
- [x] init()
// alt shift s v -> init()
@Override
public void init() throws ServletException { // init : initialized(초기화)
System.out.println("init()실행 - 서블릿 클래스(객체) 초기화!");
}
- [x] destory()
// alt shift s v -> destroy()
@Override
public void destroy() {
System.out.println("destroy() 호출 - 서블릿 객체 소멸");
}
package com.itwillbs.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet{
// 서버실행 : ctrl + 클릭
// <http://localhost:8088/JSP7/myServlet>
@Override
public void init() throws ServletException { // init : initialized(초기화)
System.out.println("init()실행 - 서블릿 클래스(객체) 초기화!");
}
// alt shift s v -> doget(), dopost()
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("MyServlet-doGet() 실행");
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("MyServlet-doPost() 실행");
}
// alt shift s v -> destroy(), init()
@Override
public void destroy() {
System.out.println("destroy() 호출 - 서블릿 객체 소멸");
}
}
이때까지 다른점. 페이지만 뜨고 안에 내용이 보이지않음
💡어떻게?
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("MyServlet-doPost() 실행");
response.setContentType("text/html; charset=UTF-8");
response.getWriter().append("안녕하세요!");
}
html 코드를 넣고싶으면
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("MyServlet-doPost() 실행");
response.setContentType("text/html; charset=UTF-8");
response.getWriter().append("안녕하세요!");
response.getWriter().append("<h1> ITWILL 서블릿수업 </h1>");
}
➡️ 직접적으로 태그를 작성해서 사용 (불편). 이를 해결하기 위해 JSP 페이지 사용
서블릿(서블릿주소 매핑) + JSP(화면) 같이 사용하는 방법은 다음 포스팅으로~
'JSP·Servlet' 카테고리의 다른 글
[JSP] EL표현식이란? (0) | 2023.09.25 |
---|---|
[JSP] 주소는 서블릿으로, 화면은 JSP로 구현하기 (0) | 2023.09.22 |
[JSP] 서블릿(Servlet)의 개념과 사용규칙 (0) | 2023.09.22 |
[JSP] 캡처본으로 쉽게 자바빈 공부하기 (0) | 2023.09.21 |
[JSP] JDBC - delete (0) | 2023.09.21 |