Model - 화면에 출력하고자 하는 정보를 담고 있음(bean)
View - 화면. (html,jsp)
Controller - 정보제어. 데이터를 가공하는 역할(java-class=서블릿:웹페이지가 될 수 있는 클래스)
-> 프론트와 벡을 분답하여 분업하기 좋음
model2를 사용할때는 web.xml 를 프로젝트 단위로 만들어줘야 한다
다이나믹 웹 프로젝트 생성시 next 2번 하고 체크박스 체크해주면 된다
만들고 나면 webapp -> WEB-INF 밑에 web.xml 이 있다
FirstController.java
package jspservlet.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 FirstController
*/
@WebServlet("/first.do") //가상경로 지정
public class FirstController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public FirstController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
서블릿 실행할때 클래스에서 실행하는게 아니라 프로젝트에서 마우스 오른쪽 클릭 후 run on server
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h2>서블릿 연습 프로젝트!</h2><hr>
<h3>가상경로 연습하기</h3>
<a href="<%= request.getContextPath() %>/seconde.do">seconde.do로 이동하기</a>
<!-- 화면에 seconde.do servlet page!! 출력 -->
|
<a href="<%= request.getContextPath() %>/board/list.do">/board/list.do로 이동하기</a>
<!-- 화면에 board/list.do servlet page!! 출력 -->
</body>
</html>
SecondeController.java
package jspservlet.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 SecondeController
*/
@WebServlet("/seconde.do")
public class SecondeController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SecondeController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("seconde.do servlet page!!");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
BoardListController.java
package jspservlet.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 BoardController
*/
@WebServlet("/board/list.do")
public class BoardListController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public BoardListController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("board/list.do servlet page!!");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package jspservlet.controller;
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;
/**
* Servlet implementation class SecondeController
*/
@WebServlet("/seconde.do")
public class SecondeController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SecondeController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("seconde.do servlet page!!");
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head>");
pw.println("<title>seconde 페이지</title>");
pw.println("</head>");
pw.println("<body>");
pw.println("<h2>가상경로 연습 두번째 페이지</h2>");
pw.println("</body>");
pw.println("</html>");
pw.flush();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
redirect
새로운 경로로 이동
request 새로 생성됨(상단의 경로 변경됨)
forward
새로운 경로로 이동
request가 전부 전달됨(url그대로)
response 사용하지 않음
SecondeController.java
package jspservlet.controller;
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;
/**
* Servlet implementation class SecondeController
*/
@WebServlet("/seconde.do")
public class SecondeController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SecondeController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//화면 포워드
request.getRequestDispatcher("/secondeView.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
secondeView.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>seconde View</title>
</head>
<body>
<h2>서블릿 연습 두번째 페이지</h2><hr>
</body>
</html>
BoardListController.java
package jspservlet.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 BoardController
*/
@WebServlet("/board/list.do")
public class BoardListController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public BoardListController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/board/list.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
BoardListController
board 폴더의 list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>board/list</title>
</head>
<body>
<h2>게시글 목록페이지</h2><hr>
<!--
/board/list.do
가상경로 요청시 해당 페이지가 포워드 될 수 있도록 컨트롤러를 수정하세요
-->
</body>
</html>
값 보내기
SecondeController.java
request의 attribute를 사용하면 컨트롤러에서 가공한 데이터를 view로 전달할 수 있다
map과 유사하게 키와 값
키는 무조건 String, 값은 object타입
package jspservlet.controller;
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;
/**
* Servlet implementation class SecondeController
*/
@WebServlet("/seconde.do")
public class SecondeController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SecondeController() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("seconde.do servlet page!!");
/*
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head>");
pw.println("<title>seconde 페이지</title>");
pw.println("</head>");
pw.println("<body>");
pw.println("<h2>가상경로 연습 두번째 페이지</h2>");
pw.println("</body>");
pw.println("</html>");
pw.flush();
*/
String data = "전송데이터";
/*
request의 attribute를 사용하면 컨트롤러에서 가공한 데이터를 view로 전달할 수 있다
*/
//map과 유사하게 키와 값
//키는 무조건 String, 값은 object타입
request.setAttribute("key", data);
//화면 포워드
request.getRequestDispatcher("/secondeView.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
secondeView.jsp
request의 attribute 값은 object타입이므로 강제 형변환 필요
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String data = (String)request.getAttribute("key");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>seconde View</title>
</head>
<body>
<h2>서블릿 연습 두번째 페이지</h2><hr>
<%= data %>
</body>
</html>
'Java' 카테고리의 다른 글
[Servlet] 로그인 처리 (0) | 2024.10.09 |
---|---|
[JSP] 회원정보 조회하고 수정하기 (0) | 2024.10.08 |
[JSP] 모델1로 회원가입하기 (0) | 2024.10.06 |
[JSP]모델 1 (0) | 2024.10.05 |
[Java] 람다식 (2) | 2024.08.27 |