본문 바로가기

회고록(TIL&WIL)

WIL 2023.01.25 ~ 2023.01.27 java JSP, vagrant

JSP

jsp를 view로 쓸 경우
<%@ include file="ex02.jspf" %> 를 많이 사용함

하나의 기능을 상품화해야한다. 보안적인 부분에서의 꼼꼼함이 중요.

민감한 정보들의 경우 컴파일 한 class 파일만 build폴더에 집어 넣게 되면 사용은 가능하나 볼 수는 없도록 할 수 있다.

Vagrant - centos8 가상화 배포

Vagrant init 후 Vagrantfile 수정

config.vm.network "forwarded_port", guest: 8080, host: 7070   
config.vm.synced_folder "./data", "/vagrant_data"   

vagrant up -> vagrant ssh

sudo yum update
sudo yum install -y java-1.8.0-openjdk-devel.x86_64

wget https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.85/bin/apache-tomcat-8.5.85.tar.gz
tar -zxvf apache-tomcat-8.5.85.tar.gz

cd ~
nano .bash_profile

export CATALINA_HOME=/home/vagrant/apache-tomcat-8.5.85   
export PATH=$PATH:$CATALINA_HOME/bin   

source .bash_profile
sh catalina.sh start

sudo firewall-cmd --permanent --zone=public --add-port=8080/tcp
sudo firewall-cmd --reload

sudo yum install mysql-server.x86_64
sudo systemctl start mysqld
sudo systemctl status mysqld

create user scott@localhost identified by 'tiger';
grant all privileges on . to 'scott'@'localhost';
flush privileges;

web.xml

자바 패키징할때 없으면 오류가 나는 경우도 많다.
프로젝트에서 작성한 web.xml 파일은 tomcat에 web.xml파일을 오버라이딩 한다.
여러가지 빌드도구들이 web.xml을 요구하기 때문에 프로젝트 생성 시 web.xml파일을 생성하는 것이 좋다.

jsp Action 태그 - include

jsp:include는 독립적으로 컴파일하기 때문에 각 jsp 파일에 영향을 주지 못함 컴파일을 개별로하기 때문에 확장자도 jsp 여야하고 오류도 없어야함
<%@ include%> 하나로 합친 뒤 한번에 컴파일하기 때문에 서로 영향을 줄 수 있음

jsp:param include 태그사이에서 파라미터를 보낼 수 있다. uri에 노출되지않는다.

<jsp:include page="template/menu.jsp">
	<jsp:param value="." name="path"/>
  <!-- <jsp:param value=".." name="path"/> -->
</jsp:include>

JavaBean

Bean, DTO, VO, Entity
정보의 은닉, 캡슐화 getter & setter

jsp Action 태그 - useBean & setProperty & getProperty

<%request.setCharacterEncoding("utf-8");%>
<jsp:useBean id="bean" class="com.bit.BbsBean"/>
<jsp:setProperty property="sub" name="bean"/>
<jsp:setProperty property="id" name="bean"/>
<jsp:setProperty property="content" name="bean"/>
<jsp:setProperty property="*" name="bean"/> 와일드문자 * 을 사용하면 일일이 적어줄 필요 없이 다 받을 수 있다.

<h1><jsp:getProperty property="num" name="bean"/>번 글</h1>

<%
String sub = bean.getSub();
String id = bean.getId();
String content = bean.getContent();
%>

injection 공격 방지

<%!
String checkMsg(String msg){
	//query 문장에서 공격 당하지 않기 위해 특수문자로 변환
	msg = msg.replace("--", "­­"); 
	msg = msg.replace("'", "'"); 
	msg = msg.replace(",", ","); 
	msg = msg.replace("<", "<"); 
	msg = msg.replace(">", ">");
	msg = msg.replace("(", "(");
	msg = msg.replace(")", ")");
	msg = msg.replace("%", "%");
	return msg;
}
%>

jsp Action 태그 - forward

response.sendRedirect() -> request - response - request - response (url이 바뀜)
jsp:forward -> request - response (url이 바뀌지 않음)

jsp Action 태그 - useBean의 scope

<jsp:useBean id="login" class="com.bit.UserBean" scope="session"/>
scope 옵션

  1. page - 해당 페이지내에서 유지 (default)
  2. request - 동일 request내에서 유지
  3. session - 세션 유지 기간동안(서버에서 지정)
  4. application - 서버를 열어서 닫기 전까지
    tomcat Servers 폴더의 web.xml에서 설정할 수 있는 session 유지 시간
<jsp:useBean id="login" class="com.bit.UserBean" scope="session"/>
<img alt="" src="/day35/imgs/logo.png">
<%if(login.isResult()){ %>
<p align="right"><jsp:getProperty property="id" name="login"/>님 접속중...</p>
<%} else {%>
<p align="right">비회원으로 접속중...</p>
<%} %>

Vagrant - ubuntu 가상화 배포

data 파일 생성 -> ROOT.war 파일 붙여넣기
cloud -- https://app.vagrantup.com/ubuntu/boxes/focal64

vagrant init ubuntu/focal64 하여 Vagrantfile 생성
vagrant up 으로 이미지 다운로드

Vagrantfile 수정
config.vm.network "forwarded_port", guest: 8080, host: 7070
config.vm.synced_folder "./data", "/vagrant_data"

vagrant halt
vagrant up
vagrant ssh

sudo update apt
sudo apt -y install openjdk-8-jdk
sudo apt -y install tomcat9

sudo systemctl status tomcat9
sudo find / -name webapps
ls /var/lib/tomcat9

ROOT 안의 내용 확인
curl http://localhost:8080

rm -rf /var/lib/tomcat9/webapps/ROOT
sudo rm -rf ROOT
sudo cp /vagrant_data/ROOT.war ROOT.war

압축이 풀리게 되면 배포완료
브라우저 localhost:7070으로 접속

sudo apt -y install mysql-server-8.0
cd ~
systemctl status mysql
sudo mysql -u root -p

create user 'scott'@'localhost' identified by 'tiger';
grant all privileges on . to 'scott'@localhost;
flush privileges;
exit

mysql -u scott -ptiger
create database lecture;
use lecture;
exit

ls -la
nano .profile

# user add path
export MYSQL_USER='scott'
export MYSQL_PW='tiger'
export PATH=$PATH:$MYSQL_USER:$MYSQL_PW

source .profile
echo $MYSQL_USER $MYSQL_PW

'회고록(TIL&WIL)' 카테고리의 다른 글