해결된 질문
작성
·
593
1
[질문 내용]
여기에 질문 내용을 남겨주세요.
제가 sts4버전을 사용해서일까요??
HelloServlet .java
package hello.servlet.basic;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "helloServlet", urlPatterns = "/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("HelloServlet.service");
System.out.println("request = " + request);
System.out.println("response = " + response);
String username = request.getParameter("username");
System.out.println("username = " + username);
response.setContentType("text/plain");
response.setCharacterEncoding("utf-8");
response.getWriter().write("hello " + username);
}
}
ServletApplication .java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@ServletComponentScan
@SpringBootApplication
public class ServletApplication {
public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
}
답변 1
1
sts 문제가 아닙니다.
강의와 동일하게 패키지 구조를 가져가셔야 하며, 코드 또한 동일하게 작성해주세요. 영한님은 강의 중 의미없는 코드를 작성하지 않습니다.
ServletApplication .java
package com.example.demo; <<-- 패키지 위치
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@ServletComponentScan
@SpringBootApplication
public class ServletApplication {
public static void main(String[] args) {
SpringApplication.run(ServletApplication.class, args);
}
}
ServletApplication .java
의 위치를
package hello.servlet;
로 옮겨주세요.
실제 물리적인 파일 위치 뿐만 아니라, package~뒤의 경로도 변경이 되어야합니다.
실행결과
.
감사합니다.
매번 감사합니다