Christmas Pikachu JSP & Servlet - Servlet(기본) 1
개발일지/JSP

JSP & Servlet - Servlet(기본) 1

ZI_CO 2022. 11. 23.

서블릿 으로 만들었을 대 WAS 동작을 확인해 보자.

 

 

 

 

1. 서블릿 파일 생성하기

서블릿에 접근하려면 제일먼저 URL Mapping을 사용하자

 

 

 

응용 예제 1

get 요청을해서 css활용

 

 

 

 

서버 실행하기

 

 

 

 

 

 

 

package com.project.test;

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

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

/**
 * Servlet implementation class TestServlet
 */
@WebServlet("/tservlet")
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public TestServlet() {
		super();
	}

// request, response 객체는 웹 컨테이너가 먼저 만들어 주는 녀셕이다.
// get 요청시 콜백 반응
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.print("<html>");
		out.print("<head>");
		out.print("</head>");
		out.print("<body>");
		out.print("<section>");
		out.print("<p style=\"color: red\">");
		out.print("Hello Fisrt Servlet ~ by boot ");
		out.print("</p>");
		out.print("</section>");
		out.print("</body>");
		out.print("</html>");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
// doGet(request, response);
		System.out.println("여기는 post 방식으로 접근했어요!!! ");
	}
}

 

댓글