1. 프로젝트에 Spring Web 서비스가 포함되어 있어야 함.
2. 서블릿 파일은 src/main/java 밑 패키지에 추가.
- Application 파일과 동일 레벨??
3. 서블릿 작성.
샘플)
package com.example.myhome2;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(urlPatterns = "/helloworld")
public class HelloServelt extends HttpServlet {
@Override
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String requestName = req.getParameter("name");
String servletName = getServletConfig().getServletName();
StringBuilder html = new StringBuilder();
html.append("<html>");
html.append(" <head>");
html.append(" <title> 서블릿 기초 </title>");
html.append(" </head>");
html.append(" <body>");
html.append(" <h1>" + requestName + "님 안녕!!! </h1>");
html.append(" <h1>저는 " + servletName + "이예요!!! </h1>");
html.append(" </body>");
html.append("<html>");
res.setStatus(200);
res.setContentType("text/html; charset=UTF-8");
PrintWriter writer = res.getWriter();
writer.write(html.toString());
}
}
4. 서블릿을 검색할수 있도록 어노테이션 추가
package com.example.myhome2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class MyHome2Application {
public static void main(String[] args) {
SpringApplication.run(MyHome2Application.class, args);
}
}
'IT > Spring Boot' 카테고리의 다른 글
Axis 강좌 (0) | 2022.09.29 |
---|---|
SpringBoot프로젝트에 JSP 프로그래밍 추가 하기 (0) | 2022.09.29 |
Axis 관련 글 모음...(WSDL 관련 라이브러리) (0) | 2022.09.29 |
Springboot excel 파일 다운로드 기능 예제 (0) | 2022.09.26 |
ResponseEntity, (0) | 2022.09.26 |
댓글