서버07_ MVC 1

charl hi·2021년 12월 15일
0

서버

목록 보기
7/15

sendRedirect()

index.jsp 로

resp.sendRedirect("/mvc/index.jsp");



포워드 VS. 리다이렉트

포워딩

  • req가 오면 서버 -> 서블릿 -> 다른 서블릿 -> 클라

리다이렉트

  • req가 오면 서버 -> 서블릿 -> 클라에게 "클라가 다시 요청하라고" resp -> 클라의 새로운 req, 여기엔 데이터가 없다



view 페이지 숨기기

  • ✨✨WEB-INF 안에 넣으면, 클라가 직접 요청을 못한다!!
package com.kh.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("/test")
public class TestServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.getRequestDispatcher("/WEB-INF/views/test.jsp").forward(req, resp);
		//view 페이지 숨기기
		//WEB-INF 안에 넣으면, 클라가 직접 요청을 못한다!!
	}
}




MVC 1

<!-- # MVC1 : Controller, Model, View -->

<!-- 자바 코드만
	## Controller
 -->

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
	<%
		String userId = request.getParameter("userId");
		String userPwd = request.getParameter("userPwd");
		String userName = request.getParameter("userName");
		String userAge = request.getParameter("userAge");
		
		String model = "";
		if(Integer.parseInt(userAge) >= 20){
			model = "성인";
		}else{
			model = "미성년";
		}
	%>
<!-- 
	## model
 -->


<!-- html 
	## View
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

	<h1>id : <%=userId %></h1>
	<h1>pwd : <%=userPwd %></h1>
	<h1>name : <%=userName %></h1>
	<h1>age : <%=userAge %></h1>
	<h1>성인 여부 : <%=model %></h1>

</body>
</html>




MVC 2

컨트롤러 : jsp -> 서블릿 으로

✨ 컨트롤러의 나머지 일들을 service와 dao 로

데이터, 모델 : request

service :

dao : 디비와의 전달


  • MyServlet
package com.kh.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;

import com.kh.service.MyService;
import com.kh.vo.Member;

@WebServlet("/my")
public class MyServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("Controller > doGet called...");
		
		String id = request.getParameter("id");
		String pwd = request.getParameter("pwd");
		Member member = new Member(id, pwd);
		
		//컨트롤러(my)가 서비스 호출
		MyService service = new MyService();
		service.sss(member);
	}

}

  • MyServlet
package com.kh.service;

import com.kh.dao.MyDao;
import com.kh.vo.Member;

public class MyService {

	public void sss(Member m) {
		System.out.println("service > sss called...");
		
		//service에서 dao 호출
		MyDao dao = new MyDao();
		dao.ddd(m);
	}
}

  • MyDao
package com.kh.dao;

import com.kh.vo.Member;

public class MyDao {

	public void ddd(Member m) {
		System.out.println("DAO > ddd called...");
		System.out.println(m);
	}
}

  • Member
package com.kh.vo;

public class Member {
	private String id;
	private String pwd;
	
	public Member(String id, String pwd) {
		this.id = id;
		this.pwd = pwd;
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	@Override
	public String toString() {
		return "Member [id=" + id + ", pwd=" + pwd + "]";
	}
	
	
}

  • mvc2/my

0개의 댓글