- http://localhost:8080/edu/htmlexam/first.html (http url string)
- url : uniform resource locator
- protocol (http통신 기반)
- serveraddress
- localhost == 127.0.0.1
- port number (8080)
- URI
- uniform resource identity
- 서버에 가는 것 (요청하는 것)
- /edu/htmlexam/first.html
- /edu : context path
- 역할 : 어떠한 웹 프로젝의 자원을 요청할 것인가?
- edu 폴더에 가서 자원을 찾는다.
- 한 덩어리의 context로 본다.
- /htmlexam/first.html 등록된 얘가 있나 확인
- 없으면 web app 폴더 찾아감
- document root라고 보면 됨
- 최상위 폴더
- 없으면 web app 폴더 찾아감
- /edu : context path
- status : 200 -> 성공적으로 받아왔다.
- get방식으로 들고옴
- default 방식임
servlet 실습
- 클래스 명이 아닌, mapping명과 연결
- 특정 servlet 요청할 때, 전체 경로 대신 간단하게 경로 표현해주는 것
- 맵핑 방법
- xml
- annotaion
- 클래스명과 mapping명 다르게 하는게 보안적으로 좋음
'/' 빼먹으면 안 된다.
import java.io.IOException;
import java.io.PrintWriter;
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("/first")
public class FirstServelt extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.print("<h1>First Servlet 수행 완료 <h1>");
out.close();
System.out.println("서블릿에서의 표준 출력은 어디로 나가나?");
}
}
- /first 로 매핑
- 톰캣 재 기동 걸어줘야함
- 새로 추가한 파일은 인식해도 소스 내용은 인식 못 함
- 자동으로 인식하지 못함 -> 자동으로 인식하면 서버성능 떨어뜨림
- 반 정도 프레임워크 기술
- 누구한테 내보낼 것인가 명시해줘야한다.
- 개행문자를 개행문자로 인식 X
- 여러 개 블랭크 하나로 인식함 -> println쓰는게 소용 없
MIME 타입
response.setContentType("text/plain; charset=UTF-8");
- 이렇게 하면 일반 텍스트로
- html으로 하면 랜더링 할 준비를 함
- html에 알맞게
- 절대로 오타 입력하면 안 된다.
- 한글 깨질 가능성 있다.
PrintWriter out = response.getWriter();
out.print("<h1>First Servlet 수행 완료 </h1>");
out.close();
- 출력객체 받아와서 출력해야한다.
out.print("<h2>반가워요...: " + request.getParameter("name") + "님!!</h2>");
이렇게해서 주소창에
http://localhost:8080/edu/first?name=myname
myname부분에 요청하면 된다.
'study > JAVA 전문가' 카테고리의 다른 글
[HTML] 연습 (0) | 2021.12.28 |
---|---|
[Servlet & JSP] 1 (0) | 2021.12.27 |
[Web programming] (0) | 2021.12.27 |
[mongoDB] 날짜 데이터 (0) | 2021.12.24 |
[mongoDB] 2 (0) | 2021.12.23 |