웹 개발환경 설정
Apache Tomcat
- 웹 어플리케이션을 실행하기 위해서는 WAS가 필요하고, 그 중 하나가 Apache Tomcat이다.
- 가장 많이 사용되는 WAS(Web Application Server)로, 무료이다.
- 컴퓨터에 OS를 설치해야만 사용할 수 있는 것처럼, 자바로 작성된 웹 어플리케이션은 WAS가 있어야만 실행할 수 있다.
설치
- brew로 설치 (버전 8)
-
brew install tomcat@8
Configuration files: /opt/homebrew/etc/tomcat@8
tomcat@8 is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.
If you need to have tomcat@8 first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/tomcat@8/bin:$PATH"' >> ~/.zshrc
To restart tomcat@8 after an upgrade:
brew services restart tomcat@8
Or, if you don't want/need a background service you can just run:
/opt/homebrew/opt/tomcat@8/bin/catalina run
- .sh 실행권한설정 (/opt/homebrew/opt/tomcat@8/bin/*.sh)
chmod +x /opt/homebrew/opt/tomcat@8/bin/*.sh
- 나는 bin 디렉토리 안에 catalina밖에 없음 🤔.. 이미 실행권환 3x라 패스
실행
- 명령
./catalina [명령]
commands:
debug Start Catalina in a debugger
debug -security Debug Catalina with a security manager
jpda start Start Catalina under JPDA debugger
run Start Catalina in the current window
run -security Start in the current window with security manager
start Start Catalina in a separate window
start -security Start in a separate window with security manager
stop Stop Catalina, waiting up to 5 seconds for the process to end
stop n Stop Catalina, waiting up to n seconds for the process to end
stop -force Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running
stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still running
configtest Run a basic syntax check on server.xml - check exit code for result
version What version of tomcat are you running?
Note: Waiting for the process to end and use of the -force option require that $CATALINA_PID is defined
- 시작
./catalina start
- 혹은 cmd
catalina start
- path 설정해주기
export PATH=/opt/homebrew/opt/tomcat@8/bin:$PATH
http://localhost:8080/
로 접속하면, 톰캣 기본화면이 뜰 것!
- 8080포트가 열린 것이다. 서버 작동!
서블릿 컴파일 및 실행
웹 어플리케이션 프로젝트 생성
-
brew install tomcat@8
Configuration files: /opt/homebrew/etc/tomcat@8 tomcat@8 is keg-only, which means it was not symlinked into /opt/homebrew, because this is an alternate version of another formula. If you need to have tomcat@8 first in your PATH, run: echo 'export PATH="/opt/homebrew/opt/tomcat@8/bin:$PATH"' >> ~/.zshrc To restart tomcat@8 after an upgrade: brew services restart tomcat@8 Or, if you don't want/need a background service you can just run: /opt/homebrew/opt/tomcat@8/bin/catalina run
chmod +x /opt/homebrew/opt/tomcat@8/bin/*.sh
- 나는 bin 디렉토리 안에 catalina밖에 없음 🤔.. 이미 실행권환 3x라 패스
./catalina [명령]
commands: debug Start Catalina in a debugger debug -security Debug Catalina with a security manager jpda start Start Catalina under JPDA debugger run Start Catalina in the current window run -security Start in the current window with security manager start Start Catalina in a separate window start -security Start in a separate window with security manager stop Stop Catalina, waiting up to 5 seconds for the process to end stop n Stop Catalina, waiting up to n seconds for the process to end stop -force Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still running configtest Run a basic syntax check on server.xml - check exit code for result version What version of tomcat are you running? Note: Waiting for the process to end and use of the -force option require that $CATALINA_PID is defined
./catalina start
- 혹은 cmd
catalina start
- path 설정해주기
export PATH=/opt/homebrew/opt/tomcat@8/bin:$PATH
- path 설정해주기
http://localhost:8080/
로 접속하면, 톰캣 기본화면이 뜰 것!- 8080포트가 열린 것이다. 서버 작동!
웹 어플리케이션 프로젝트 생성
이클립스 기반 강의라... 조금 헤메며 찾아가는 중
모듈 생성 (프로젝트 생성)
- Module이 이클립스의 프로젝트인듯. 모듈 생성, 별다른 옵션 x
- 모듈 우클릭 → add Framework Support
- Web Application 클릭 (xml파일은 선택)
- 여기까지 하면 기본적인 웹 프로젝트의 구조가 완성된다.
- 이클립스의 WebContent web폴더!
톰캣 서버 설정
- 오른쪽 상단에 Add Configuration 클릭
- 왼쪽 상단 + 아이콘 → Tomcat → Local 선택
- cof파일 찾아서 연결해주기 (자동으로 연결이 안되면 손수 입력해주면 된다)
- 내 경로:
/opt/homebrew/opt/tomcat@8/libexec/conf
- 내 경로:
- 밑에 Warning 오른쪽에 Fix 눌러서 Deployment에 추가한 후 Apply→OK
ref- [Intellij] Servlet 프로젝트 생성하기(JSP, Web Dynamic Project 생성)
HelloWorld 서블릿 컴파일 및 실행
목표
[http://localhost:8080/firstweb/HelloServlet](http://localhost:8080/firstweb/HelloServlet)
을 브라우저에 요청하면, Hello Servlet 으로 응답해주는 서블릿 파일 만들기
Intellij에서 웹 어플리케이션 만들기
- ref
- project 생성할 때, Java Enterprise → Web application 선택하기!
(그래야 서블릿 사용할 수 있다)![image](https://user-images.githubusercontent.com/75327385/154581264-fcd59075-c33e-43dd-b227-e6b5f0e1faab.png)
웹 어플리케이션에 서블릿을 등록하기
서블릿이란?
- URL 요청을 처리하는 프로그램이다.
아래 규칙과 같은 URL로 서블릿을 실행한다.
[http://localhost:8080/firstweb/HelloServlet](http://localhost:8080/firstweb/HelloServlet)
을 브라우저에 요청하면, Hello Servlet 으로 응답해주는 서블릿 파일 만들기(그래야 서블릿 사용할 수 있다)
![image](https://user-images.githubusercontent.com/75327385/154581264-fcd59075-c33e-43dd-b227-e6b5f0e1faab.png)
http://localhost:8080/{프로젝트이름}/{URL_MAPPING값}
http
: 프로토콜localhost
: IP나 서버 도메인8080
: 포트 번호
doGet
- 웹브라우저가 http프로토콜에서 get메서드로 요청을 보낼 때, 서블릿의 doGet 메서드가 호출이 된다. (*get: 웹 브라우저가 서버에게 문서를 요청할 때 방식)
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType("text/html;charset=UTF=8"); PrintWriter out = response.getWriter(); out.println("<html><body>"); out.println("<h1>" + message + "</h1>"); out.println("</body></html>"); }
response 객체
- 응답할 내용들을 모아서 추상화해놓은 객체
response.setContentType("text/html;charset=UTF=8");
실행
- firstweb_war_exploded 으로 될 때?
- → Edit configuration탭 다시 들어가서 Tomcat → deployment → war_exploded를 war로 바꿔준다, 밑에 url 원하는 대로 설정
Author And Source
이 문제에 관하여(웹 개발환경 설정), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@yeawonbong/웹-개발환경-설정저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)