221205 MVC2 Servlet Controller
CSS
여러 파일들을 include할 때
각 파일들의 위치가 다르므로
공용으로 쓰는 header에 있는 경로들은 전부 절대경로로 적는게 좋다.
1./JSPWeb/-> contextpath를 넣을 수 있지만 바뀔수도 있다! 그래서 명령어로 사용
2.<%=request.getcontextPath()%>/ 또는
EL태그로
3.${pageContext.request.contextPath}/
이렇게 하면 서버의 모듈에서 edit으로 path를 지워도 깨지지 않는다. 1의 방식으로 하면 깨짐
header에 있는 경로에도 ${pageContext.request.contextPath}를 넣고 경로를 지정=>절대경로로.
그러나, MVC2에서 경로를 직접 적는 것은 틀린 방식이다.
MVC2는 바로 화면으로 키는 게 없다. 화면도 controller를 타고 나가야 한다.
MVC2에서는 무조건 forward
->많은 데이터를 보내야 함
->새로고침하면 다시 데이터를 들고 나가야 할 수도. redirect면 새로고침할 때 데이터도 다 버리고 주소경로도 바뀌어버린다.
switch구문의 경로는 주소 경로,
포워드 구문의 경로는 실제 파일 경로
화면에서 클릭 or url입력하면 forward를 타고 다시 화면으로 나간다
모든 화면이! 그래서 다 servlet으로 연결해야.
header
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-idth, initial-scale=1">
<title>Welcome to MyWorld</title>
<!-- Bootstrap Core CSS -->
<link href="${pageContext.request.contextPath}/css/bootstrap.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="${pageContext.request.contextPath}/css/business-casual.css" rel="stylesheet">
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Josefin+Slab:100,300,400,600,700,100italic,300italic,400italic,600italic,700italic" rel="stylesheet" type="text/css">
<!-- jQuery -->
<script src="${pageContext.request.contextPath}/js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="${pageContext.request.contextPath}/js/bootstrap.min.js"></script>
<script>
$('.carousel').carousel({
interval: 2000 //changes the speed
})
</script>
<style>
.abc {
position: sticky;
top: 0px;
width: 100%;
z-index: 10;
}
</style>
</head>
<body>
<!-- header -->
<div class="brand">My Web</div>
<div class="address-bar">Welcome to MyWorld</div>
<nav class="navbar navbar-default abc" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/hong">My First Web</a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a href="${pageContext.request.contextPath}/index.main">HOME</a>
</li>
<li>
<a href="${pageContext.request.contextPath}/member/member.main">Member</a>
</li>
<li>
<a href="${pageContext.request.contextPath}/board/board.user">BOARD</a>
</li>
<li>
<a href="${pageContext.request.contextPath}/login/login.user">LOGIN</a>
</li>
<li>
<a href="${pageContext.request.contextPath}/user/user_join.user" style="color:red">JOIN</a>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<!-- end header -->
index(home)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="include/header.jsp" %>
<div class="container">
<div class="row ">
<div class="box">
<div class="col-lg-6 text-center">
<h2>coding404</h2>
<p>강의가 궁금하다면?</p>
<hr>
<h3><a href="https://www.youtube.com/channel/UCXiyuCYo4dUqM556XpgqAcQ">둘러보기</a></h3>
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel"><!-- data-ride="carousel" 자동 슬라이드 기능 -->
<ol class="carousel-indicators hidden-xs">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="item active">
<img class="img-responsive img-full" src="img/1.png">
</div>
<div class="item">
<img class="img-responsive img-full" src="img/2.png"> <!-- img-full 옵션 class -->
</div>
<div class="item">
<img class="img-responsive img-full" src="img/3.png">
</div>
</div>
<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev">
<span class="icon-prev"></span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" data-slide="next">
<span class="icon-next"></span>
</a>
</div>
</div>
<div class="col-lg-6 text-center" >
<h2>hello world</h2>
<p>
깜빡하신게 있는것 같아요!
</p>
<hr>
<h3>구독과 좋아요</h3>
<iframe width="100%" height="351px" src="https://www.youtube.com/embed/nOTpuof2YG8?list=PLK7AWkPYwus6SdPJyhK9SNnKJybIC5qCs" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>
</div>
</div>
<div class="row">
<div class="box">
<div class="col-lg-12">
<hr>
<h2 class="intro-text text-center">hello World
<strong>유의 사항!</strong>
</h2>
<hr>
<div class="inner" align="center">
<img class="img-responsive img-center" src="img/intro.png" alt=""><!-- img-border -->
<hr class="visible-xs">
<p>1. 모든 자료는 회원 가입 후에 이용하실 수 있습니다.</p>
<p>2. ------------------------------------------------------</p>
<p>3. ------------------------------------------------------</p>
</div>
</div>
</div>
</div>
<!-- /.container -->
</div>
<%@ include file="include/footer.jsp" %>
member
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="../include/header.jsp" %>
<div class="container">
<div class="row">
<div class="box">
<div class="col-lg-12">
<hr>
<h2 class="intro-text text-center">
<strong>Profile</strong>
</h2>
<hr>
</div>
<div class="col-sm-4 text-center">
<img class="img-responsive" src="../img/member7.jpg" alt="">
<h3>Actor
<small>한지민</small>
</h3>
</div>
<div class="col-sm-4 text-center">
</div>
<div class="col-sm-4 text-center">
<img class="img-responsive" src="../img/member8.jpg" alt="">
<h3>Age
<small>37</small>
</h3>
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="row">
<div class="box">
<div class="col-lg-12">
<hr>
<h2 class="intro-text text-center">
<strong>화보</strong>
</h2>
<hr>
</div>
<div class="col-sm-4 text-center">
<img class="img-responsive" src="../img/member1.jpg" alt="">
<h3>
<small></small>
</h3>
</div>
<div class="col-sm-4 text-center">
<img class="img-responsive" src="../img/member2.jpg" alt="">
<h3>
<small></small>
</h3>
</div>
<div class="col-sm-4 text-center">
<img class="img-responsive" src="../img/member3.jpg" alt="">
<h3>
<small></small>
</h3>
</div>
<div class="clearfix"></div>
</div>
</div>
<div class="row">
<div class="box">
<div class="col-sm-4 text-center">
<img class="img-responsive" src="../img/member4.jpg" alt="">
<h3>
<small></small>
</h3>
</div>
<div class="col-sm-4 text-center">
<img class="img-responsive" src="../img/member5.jpg" alt="">
<h3>
<small></small>
</h3>
</div>
<div class="col-sm-4 text-center">
<img class="img-responsive" src="../img/member6.jpg" alt="">
<h3>
<small></small>
</h3>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<!-- /.container -->
<%@ include file="../include/footer.jsp" %>
user_join
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="../include/header.jsp"%>
..........................................
<%@ include file="../include/footer.jsp"%>
footer
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<footer>
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<p>Copyright © By Park 2018.XX.XX~</p>
</div>
</div>
</div>
</footer>
</body>
</html>
Servlet
UserController
package com.example.controller;
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("*.user")
public class UserController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doAction(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doAction(request, response);
}
//get, post 하나로 묶음
protected void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//한글처리
request.setCharacterEncoding("utf-8");
//요청분기
String uri = request.getRequestURI();
String path = request.getContextPath();
String command =uri.substring(path.length());
System.out.println("요청경로:"+command);
switch (command) {
case "/user/user_join.user":
request.getRequestDispatcher("user_join.jsp").forward(request, response);
break;
default:
break;
}
}
}
MainController
package com.example.controller;
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;
/**
* Servlet implementation class MainController
*/
@WebServlet("*.main")
public class MainController extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doAction(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doAction(request, response);
}
protected void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String uri = request.getRequestURI();
String path = request.getContextPath();
String command =uri.substring(path.length());
System.out.println("요청경로:"+command);
//main화면
//and
//member화면
switch (command) {
case "/index.main":
request.getRequestDispatcher("index.jsp").forward(request, response);
break;
case "/member/member.main":
request.getRequestDispatcher("member.jsp").forward(request, response);
break;
default:
break;
}
}
}
header와 footer를 include하기.
이미지의 url주소를 확인할 것. servlet에 따라 주소가 다르다.
header에서 주소를 지정해주어야한다. 어노테이션에 맞춰서.
switch문에 있는 경로는 uri경로, servlet에 있는 forward경로는 파일경로로
index.main으로 주소를 입력하거나 새로고침을하면 controller를 타고 나간다.
새로고침하거나 경로이동을 할 때마다 콘솔창에 sysout구문이 뜬다. 경로를 이동할 때 servlet으로 이동하기 때문