๐ป
JSP ํ๋ก๊ทธ๋๋ฐ - [Servlet] Servlet ๊ฐ์ฒด ๋ฒ์ ๋ณธ๋ฌธ
๊ฐ์ฒด ๋ฒ์
page < request < session < application
๊ฐ์ฒด ๋ฒ์ ์ข ๋ฅ
- page ์์ญ
- JSP ํ์ผ์๋ pageContext๊ฐ ๋ด์ฅ๋์ด ์์ผ๋ฉฐ, ์ด ๊ฐ์ฒด๋ page ์์ญ์์๋ง ์ ํจ
- JSP ํ์ผ์ <% %> ์์ ๋ณ์๋ฅผ ์ฌ์ฉํ๋ฉด ์ด ๋ณ์๋ ํด๋น JSP ํ์ผ ๋ด์์๋ง ์ ํจํ๋ฐ, page ์ค์ฝํ์ ์ ์๋ ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ด๋ค.
- ํ ๋ฒ์ ํด๋ผ์ด์ธํธ ์์ฒญ์ด ์ค๋ฉด, ํ๋์ JSP ํ์ด์ง๊ฐ ์๋ต
- page ์์ญ์ ์ด ๋ ํ๋์ JSP ํ์ด์ง ๋ด์์๋ง ๊ฐ์ฒด๋ฅผ ๊ณต์ ํ๋ ์์ญ์ ์๋ฏธ
- request ์์ญ
- Servlet์์๋ reqeust.setAttribute("์ด๋ฆ", ๊ฐ์ฒด); ๋ก ์ ๋ฌํ๊ณ , JSP์์๋ Obejct ob j = request.getAttribute("์ด๋ฆ"); ์ผ๋ก ๋ฐ๋๋ค. ์ด ๋ ์ฌ์ฉํ๋ request ๊ฐ์ฒด๊ฐ request ์์ญ์ด๋ค.
- ์์ฒญ์ ๋ฐ์์ ์๋ตํ๊ธฐ๊น์ง ๊ฐ์ฒด๊ฐ ์ ํจํ ์์ญ
- Servlet์์ forward ๋๋ include๋ฅผ ์ฌ์ฉํ๋ฉด, request ์์ฒญ ๊ฐ์ฒด๊ฐ ๊ณต์ ๋์ด์ request ์์ญ์ด ๋๋ค.
- forward๋ฅผ ๋ ๋ง์ด ์ฌ์ฉํ๋ค.
- ์ง๊ธ๊น์ง Servlet์์ JSP๋ก ๊ฐ์ฒด๋ฅผ ๋ณด๋ผ ๋ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
- session ์์ญ
- ๊ฐ์ ๋ธ๋ผ์ฐ์ ๋ด์์ ์์ฒญ๋๋ ํ์ด์ง๋ค์ ๊ฐ์ ๊ฐ์ฒด๋ฅผ ๊ณต์ ํ๊ฒ ๋๋๋ฐ, ์ด๋ฅผ ์ธ์ ์์ญ์ด๋ผ๊ณ ํ๋ค.
- ํ๋์ ๋ธ๋ผ์ฐ์ ๋น 1๊ฐ์ session ๊ฐ์ฒด ์์ฑ
- ์ธ์ ์ข ๋ฃ ํ ๊ฐ์ฒด ๋ฐํ
- requset.getSession() ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ์ธ์ ์์ญ์ ๊ฐ์ฒด๋ฅผ ์ป๋๋ค.
- application ์์ญ
- ๊ฐ์ ์ ํ๋ฆฌ์ผ์ด์ ๋ด์์ ์์ฒญ๋๋ ํ์ด์ง๋ค์ ๊ฐ์ ๊ฐ์ฒด๋ฅผ ๊ณต์ ํ๊ฒ ๋๋๋ฐ ์ด๋ฅผ ์ ํ๋ฆฌ์ผ์ด์ ์์ญ์ด๋ผ๊ณ ํ๋ค.
- ํ๋์ ์ ํ๋ฆฌ์ผ์ด์ ๋น 1๊ฐ์ application ๊ฐ์ฒด ์์ฑ
- ์ ํ๋ฆฌ์ผ์ด์ ์ข ๋ฃ ํ ๊ฐ์ฒด ๋ฐํ
- request.getServletContext() ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ๊ฐ์ฒด๋ฅผ ์ป๋๋ค.
โป redirect, forward, include ์ ์์๋๊ธฐ!
-> response๊ฐ ๊ฐ์ง๊ณ ์๋ sendRedirect(ํ์ด์ง ์ด๋), ๊ฐ์ ๋ก ์ด๋ํด์ค๋ ์ฌ์ฉํ๋ค.
-> forward include(๋ ๋ค ๋ฐ์ดํฐ ๋์ ธ์ค) ์ฐจ์ด์ (include๋ ์ ์ด๊ถ์ด ๋์์ค๊ณ , forward๋ ๋ค์ ๋์์ค์ง ์๋๋ค.)
-> forward๋ฅผ ์ ์ผ ๋ง์ด ์ฌ์ฉํ๋ค.
08_example.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>include</h3>
<form action="http://localhost:8181/webTesting/com/java/servlet/Example08" method="get">
<input type="text" name="message"/>
<input type="submit" value="์ ์ก"/>
<input type="reset" value="์ทจ์"/>
</form>
<br /><br /><br />
<h3>forward</h3>
<form action="http://localhost:8181/webTesting/com/java/servlet/Example08" method="post">
<input type="text" name="message"/>
<input type="submit" value="์ ์ก"/>
<input type="reset" value="์ทจ์"/>
</form>
<br /><br /><br />
</body>
</html>
Example08.java
package com.java.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
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 Example08
*/
public class Example08 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Example08() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//include
request.setCharacterEncoding("utf-8");
String message = request.getParameter("message");
System.out.println(message);
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("<html>");
out.print("<head><title></title></head>");
out.print("<body>");
request.setAttribute("name", "ํ๊ธธ๋");
request.setAttribute("phone", "010-1234-5678");
RequestDispatcher rd = request.getRequestDispatcher("/com/java/servlet/Example08_Sub");
rd.include(request, response);
out.print("<hr color='red' width='400px'/>");
out.print("<h3> include๋ ๋ค์ ๋์์จ๋ค. ์ ์ด๊ถ์ ๋๊ฒจ์ฃผ์ง ์๋๋ค. </h3>");
out.print("<h3> ์๋ธ๋ฆฟ์์ ๋ฐ์ดํฐ ์๋ช
์ฃผ๊ธฐ pageScope, requestScope, sessionScope, applicationScope </h3>");
out.print("</body>");
out.print("</html>");
out.close();
//close() include๋ฐฉ์์์๋ ์คํธ๋ฆผ์ด ๋ซํ์ ์ถ๋ ฅ ์๋๋ค. forward๋ ๋๋ค.
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//forward
request.setCharacterEncoding("utf-8");
String message = request.getParameter("message");
System.out.println(message);
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("<html>");
out.print("<head><title></title></head>");
out.print("<body>");
request.setAttribute("name", "ํ๊ธธ๋");
request.setAttribute("phone", "010-1234-5678");
RequestDispatcher rd = request.getRequestDispatcher("/com/java/servlet/Example08_Sub");
rd.forward(request, response);
out.print("<hr color='red' width='400px'/>");
out.print("<h3> include๋ ๋ค์ ๋์์จ๋ค. ์ ์ด๊ถ์ ๋๊ฒจ์ฃผ์ง ์๋๋ค. </h3>");
out.print("<h3> ์๋ธ๋ฆฟ์์ ๋ฐ์ดํฐ ์๋ช
์ฃผ๊ธฐ pageScope, requestScope, sessionScope, applicationScope </h3>");
out.print("</body>");
out.print("</html>");
out.close();
}
}
Example08_Sub.java
package com.java.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Example08_Sub
*/
public class Example08_Sub extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Example08_Sub() {
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
request.setCharacterEncoding("utf-8");
String message = request.getParameter("message");
String name = (String) request.getAttribute("name"); //upCasting
String phone = (String) request.getAttribute("phone"); //upCasting
System.out.println("Example08_Sub" +message);
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("<html>");
out.print("<head><title></title></head>");
out.print("<body>");
out.print("<h3 style = 'color:blue' width='400px'>" + message + "</h3>");
out.print("<h3 style = 'color:blue' width='400px'>" + name + "</h3>");
out.print("<h3 style = 'color:blue' width='400px'>" + phone + "</h3>");
out.print("</body>");
out.print("</html>");
// close๋ฅผ ์ฐ๋ฉด ํ์ด์ง๋ฅผ ๋ค์ ๋์๊ฐ ์ ์๊ธฐ ๋๋ฌธ์ ์ฐ๋ฉด ์๋๋ค.
}
/**
* @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);
}
}
< include ๋ฐฉ์ ๊ฒฐ๊ณผ >
< forward ๋ฐฉ์ ๊ฒฐ๊ณผ >
๋ฐ์ํ
'KITRI > JSP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JSP ํ๋ก๊ทธ๋๋ฐ - JSTL(JSP Standard Tag Library) ๋ค์ด๋ก๋ / c:set, c:remove (0) | 2020.06.10 |
---|---|
JSP ํ๋ก๊ทธ๋๋ฐ - EL, JSTL (0) | 2020.06.10 |
JSP ํ๋ก๊ทธ๋๋ฐ - [Servlet] Cookie(์ฟ ํค)/ Session(์ธ์ ) (0) | 2020.06.09 |
JSP ํ๋ก๊ทธ๋๋ฐ - [Servlet] Servlet(์๋ธ๋ฆฟ) (2) (0) | 2020.06.08 |
JSP ํ๋ก๊ทธ๋๋ฐ - [Servlet] Servlet(์๋ธ๋ฆฟ) (1) (0) | 2020.06.05 |
Comments