image.png

3.1 WAS (톰켓)

<aside> 💡

자바 코드로 HTML을 직접 출력한다. css같은 그림 그리기가 너무 힘들다!!!ㅠㅠㅠㅠ

</aside>

image.png

image.png

package com.example.v2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.server.servlet.context.ServletComponentScan;

@ServletComponentScan
@SpringBootApplication
public class V2Application {

	public static void main(String[] args) {
		SpringApplication.run(V2Application.class, args);
	}

}

package com.example.v2;

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

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

@WebServlet("*.do")
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 1. 요청 받기
        // localhost:8080/hello.do?cmd=a
        String cmd = req.getParameter("cmd");
        System.out.println(req.getQueryString()); // cmd=a
        System.out.println(cmd); // a

        // 2. 요청 받은 논문 찾기
        String html = """
                <h1>${data} 논문입니다</h1>
                """;
        // Route (길 안내자)
        if (cmd.equals("a")) {
            html = html.replace("${data}", "a");
        } else if (cmd.equals("b")) {
            html = html.replace("${data}", "b");
        } else {
            html = "찾는 문서가 없습니다";
        }

        // 3. 응답해주기
        resp.setHeader("Content-Type", "text/html; charset=utf-8");
        PrintWriter pw = resp.getWriter(); // 버퍼BW이다.
        pw.println(html);
    }
}