- 어노테이션을 이용해서 URL을 Mapping 후 브라우저에 출력
- HttpServlet을 상속받은 클래스 위에
@WebServlet("/lesson01/quiz03") 한 줄만 입력해주면 web.xml과 같다.
둘다 쓰면 에러가 난다. 한 프로젝트 안에서는 주소가 같으면 안된다.
- response.setContentType("text/html") 사용
package com.test.lesson01;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/lesson01/quiz03")
public class UrlMappingQuiz03 extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
//한글 깨짐 방지
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
PrintWriter out = response.getWriter();
out.println("<html><head><title></title></head><body>");
out.print("<h1>[단독] 고양이가 야옹해</h1><p>기사 입력 시간:" + sdf.format(now) + "</p><hr> 끝");
out.print("</body></html>");
}
}
package com.test.lesson01;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/lesson01/quiz04")
public class UrlMappingQuiz04 extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
//한글 깨짐 방지
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>리스트 출력</title></head><body><ul>");
for (int i = 1; i <= 30; i++) {
out.println("<li>" + i + "번째 리스트" + "</li>");
}
out.print("</ul></body></html>");
}
}