inblog logo
|
sson17
    스프링부트(Spring Boot)

    [스프링부트(Spring Boot)]4.request 객체

    손영민's avatar
    손영민
    Apr 14, 2025
    [스프링부트(Spring Boot)]4.request 객체
     
    1.
     
     
     
    이미지
     
     
    @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Enumeration<String> headerNames = req.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName = headerNames.nextElement(); String headerValue = req.getHeader(headerName); System.out.println(headerName + ": " + headerValue); } }
    이미지
     
    @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String path = req.getRequestURI(); System.out.println("path: " + path); String method = req.getMethod(); System.out.println("method: " + method); String QueryString = req.getQueryString(); System.out.println("QueryString: " + QueryString); // 구체적 질의 =sql String username = req.getParameter("username"); System.out.println("username: " + username); String password = req.getParameter("password"); System.out.println("password: " + password);
     
    notion image
    notion image
     
     
    2.포스트
     
    텍스트로 보내기
    @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String body = ""; BufferedReader br = req.getReader(); while (true) { String line = br.readLine(); if (line == null) break; body = body + line; } System.out.println("body: " + body); } }
    notion image
    notion image
     
     
     
    json으로 보내기
    notion image
    notion image
     
    @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String body = ""; BufferedReader br = req.getReader(); while (true) { String line = br.readLine(); if (line == null) break; body = body + line; } String Username = req.getParameter("username"); String Password = req.getParameter("password"); System.out.println("Username: " + Username); System.out.println("Password: " + Password); }
    notion image
     
     
    x.www.-form-urlencoded 으로 보내기
    @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("username"); String password = req.getParameter("password"); System.out.println("username: " + username); System.out.println("password: " + password); }
     
    notion image
     
    notion image
     
     
     
     
    커서 켜서 내용보내서 받기
    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body> <form action="http://localhost:8080/hello.do" method="post" enctype="application/x-www-form-urlencoded" > <input type="text" placeholder="Enter your username" name="username" /> <input type="text" placeholder="Enter your password" name="password" /> <button type="submit">Login</button> </form> </body> </html>
    notion image
    notion image
     
     
     
    jsp를 지우고 똑같이
    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>Home page</h1> <hr> <% String name = "홍길동"; %> <%=name%> </body> </html>
    notion image
     
    package org.example.demo7; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/home.jsp") public class HomeServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setHeader("Content-Type", "text/html"); String name = "홍길동"; String body = """ <html> <head> <title>Title</title> </head> <body> <h1>Home page</h1> <hr> ${name} </body> </html> """.replace("${name}", name); resp.getWriter().println(body); } }
     
     
     
     
     
     
    ds를 통해 여러 jsp를
    package org.example.demo8; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; // localhost:8080/hello.do?path=a @WebServlet("*.do") public class FrontController extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("common logic"); String path = req.getParameter("path"); if (path.equals("a")) { req.getRequestDispatcher("a.jsp").forward(req, resp); } else if (path.equals("b")) { req.getRequestDispatcher("b.jsp").forward(req, resp); } else if (path.equals("c")) { req.getRequestDispatcher("c.jsp").forward(req, resp); } else { } } }
     
    a.jsp
    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>A view</h1> </body> </html>
     
    b.jsp
    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>B view</h1> </body> </html>
     
    c.jsp
    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h1>C view</h1> </body> </html>
     
     
    package org.example.second.controller; import jakarta.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller // PrintWriter 응답 @Controller -> requestDispatcher public class DemoController { @GetMapping("/haha") public @ResponseBody String haha(){ return "haha"; } @GetMapping("/home") public void home(HttpServletResponse response){ response.setStatus(302); response.setHeader("Location", "/haha"); } }
    Share article

    sson17

    RSS·Powered by Inblog