KITRI/JSP
JSP νλ‘κ·Έλλ° - [Servlet] Servlet κ°μ²΄ λ²μ
λν¨λ
2020. 6. 9. 10:41
κ°μ²΄ λ²μ
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 λ°©μ κ²°κ³Ό >
λ°μν