๐Ÿ‘€ ์„œ๋ธ”๋ฆฟ(Servlet)์ด๋ž€?

  • ์ž๋ฐ”๋ฅผ ์ด์šฉํ•ด์„œ ์›น ๊ฐœ๋ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ์ˆ ๋กœ ๋™์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ํŽ˜์ด์ง€์ธ JSP ํŒŒ์ผ์„ ์ตœ์ข…์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์ž๋ฐ” ํด๋ž˜์Šค ํŒŒ์ผ๋กœ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š”๋ฐ ์„œ๋ธ”๋ฆฟ์€ ๊ทธ ์ค‘๊ฐ„ ๊ณผ์ •์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ฆ‰ JSP(.jsp) -> Servlet(.java) -> ํด๋ž˜์ŠคํŒŒ์ผ(.class) ์ด๋Ÿฐ ๊ณผ์ •์„ ๊ฑฐ์น˜๊ฒŒ ๋œ๋‹ค.

์„œ๋ธ”๋ฆฟ ์ž‘์„ฑ ๊ทœ์น™

1) javax.servlet.Servlet ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„
2) 1)์˜ ๊ตฌํ˜„์ด ์–ด๋ ค์šด ๊ฒฝ์šฐ javax.servlet.http.HttpServlet ํด๋ž˜์Šค ์ƒ์†(์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ)
3) doGet()/ doPost() ์ƒ์„ฑ(์˜ค๋ฒ„๋ผ์ด๋”ฉ), HttpServletRequest/ HttpServletResponse ๊ฐ์ฒด ๊ตฌํ˜„
4) IOException/ ServletException์„ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•จ
5) web.xml ํŒŒ์ผ์„ ํ†ตํ•ด์„œ ์ฃผ์†Œ ๋งคํ•‘ (์–ด๋…ธํ…Œ์ด์…˜ ์ฒ˜๋ฆฌ)

์„œ๋ธ”๋ฆฟ ์‹คํ–‰ ๊ตฌ์กฐ




์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค ๋งŒ๋“ค๊ธฐ

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
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;

import com.itwillbs.test.Student;

// ์„œ๋ธ”๋ฆฟ - jsp์ฝ”๋“œ + java์ฝ”๋“œ๋ฅผ ํ•จ๊ป˜ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ์ผ
// ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค ์ƒ์†๋ฐ›์œผ๋ฉด ์„œ๋ธ”๋ฆฟ์ด ๋จ

// http://localhost:8090/JSP6/ex1
@WebServlet("/ex1")
public class ExServlet extends HttpServlet 
{
    // ํผํƒœ๊ทธ์—์„œ get method๋ฅผ ์ผ์„ ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        System.out.println("doGet() ํ˜ธ์ถœ");
		
        // ์‘๋‹ต์ •๋ณด์˜ ๋‚ด์šฉ์˜ ํ˜•ํƒœ๋Š” html๋ฌธ์„œ๋กœ ํ‘œํ˜„ํ•˜๊ฒ ๋‹ค.
        response.setContentType("text/html; charset=UTF-8");

        // response๋ฅผ ํ†ตํ•ด์„œ ์ถœ๋ ฅ๊ฐ€๋Šฅํ•œ ํ†ต๋กœ๋ฅผ ์ƒ์„ฑํ•˜๊ฒ ๋‹ค.
//	    PrintWriter out = response.getWriter();
//
//	    // ์„œ๋ธ”๋ฆฟ์„ ์ด์šฉํ•œ JSP ํŽ˜์ด์ง€ ํ‘œํ˜„ - ์‚ฌ์šฉํ•˜์ง„ ์•Š์„ ๊ฒƒ์ž„
//	    out.print("<html>");
//	    out.print("<head>");
//	    out.print("</head>");
//	    out.print("<body>");
//	    out.print("<h1> ์„œ๋ธ”๋ฆฟ์„ ํ™œ์šฉํ•œ JSPํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ</h1>");
//	    out.print("</body>");
//	    out.print("</html>");
//	    out.close();

        // ํฌ์›Œ๋”ฉ ์ „ request ์˜์—ญ์— ์ •๋ณด๋ฅผ ์ €์žฅ
        request.setAttribute("itwill", "busan");

        // ๊ฐ์ฒด์ •๋ณด๋ฅผ ์ „๋‹ฌ
        Student kim = new Student();
        kim.setName("๊น€ํ•™์ƒ");
        kim.setKor(100);
        kim.setEng(90);
        kim.setMath(76);
        request.setAttribute("Student", kim);

        Student user = new Student();
        user.setName("์‚ฌ์šฉ์ž");
        user.setKor(45);
        user.setEng(70);
        user.setMath(98);

        ArrayList<Student> memberList = new ArrayList<Student>();
        memberList.add(kim);
        memberList.add(user);

        request.setAttribute("memberList", memberList);

        // ์„œ๋ธ”๋ฆฟ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•œ ํ™”๋ฉด ์ถœ๋ ฅ X
        // => ํฌ์›Œ๋”ฉ์„ ์‚ฌ์šฉํ•œ ํ™”๋ฉด ์ถœ๋ ฅ O

        // ์ž๋ฐ” ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•œ ํฌ์›Œ๋”ฉ ๋ฐฉ์‹ - ์™ธ์šฐ๊ธฐ
//	    <jsp:forward/> ์‚ฌ์šฉ๋ถˆ๊ฐ€ - JSP ํŽ˜์ด์ง€๊ฐ€ ์•„๋‹Œ ์ž๋ฐ”ํŒŒ์ผ์ด๋ผ์„œ ๋ชป ์”€
        // ์ด๋™ํ•  ๊ฒฝ๋กœ ์„ค์ •
        RequestDispatcher dis = request.getRequestDispatcher("/jstl/coreSet2.jsp");

        dis.forward(request, response);
	} // doGet()
}
  • ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค๋ฉด ๋˜๋Š”๋ฐ ์‚ฌ์šฉํ•  ํ•จ์ˆ˜๋“ค์„ ๋‹ค ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ผ์ผ์ด ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
  • HttpServlet์„ ์ƒ์†๋ฐ›๊ณ  ๋‚˜๋ฉด ์ดํด๋ฆฝ์Šค๊ฐ€ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ํ•จ์ˆ˜๋“ค์„ ๋‹ค ๋งŒ๋“ค์–ด ์ค€๋‹ค.
  • ์šฐ๋ฆฌ๋Š” ํ˜•ํƒœ๊ฐ€ ๋งŒ๋“ค์–ด์ง„ ํ•จ์ˆ˜์˜ ๋‚ด๋ถ€๋งŒ ์ฑ„์šฐ๋ฉด ๋œ๋‹ค.


JSP ํŽ˜์ด์ง€์—์„œ ์„œ๋ธ”๋ฆฟ์„ ํ™œ์šฉํ•œ ์ •๋ณด ์ „๋‹ฌ

<!-- ์„œ๋ธ”๋ฆฟ์—์„œ ์ €์žฅ๋œ ์ •๋ณด(request.setAttribute("itwill", "busan");) ์ถœ๋ ฅ -->
  itwill : <%=request.getAttribute("itwill") %><br>
  itwill : ${itwill }<br>
  itwill : ${requestScope.itwill }<br>
  
<!-- ์•ž์œผ๋กœ๋Š” ๋ณด์•ˆ์„ ์œ„ํ•ด ์ฃผ์†Œ์— ์ •๋ณด๊ฐ€ ์ฒ˜๋ฆฌ๋˜๋Š” jsp ํŽ˜์ด์ง€๋ฅผ ๋‚˜ํƒ€๋‚ด์ง€ ์•Š์„ ๊ฒƒ์ž„
  		์ž๋ฐ” ์„œ๋ธ”๋ฆฟ ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•ด jsp ํŽ˜์ด์ง€ ์ž‘์„ฑ(๊ฐ€์ƒ์ฃผ์†Œ ์ƒ์„ฑ) -->
  • ์ž๋ฐ” ์„œ๋ธ”๋ฆฟ ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•ด jsp ํŽ˜์ด์ง€๋ฅผ ์ž‘์„ฑํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ธ”๋ฆฟ ํด๋ž˜์Šค๋ฅผ ์‹คํ–‰ํ•˜๋ฉด jsp ํŽ˜์ด์ง€๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์•ก์…˜์„ ์ˆ˜ํ–‰ํ•˜์ง€๋งŒ ์ฃผ์†Œ์ฐฝ์—๋Š” jsp ํŽ˜์ด์ง€๊ฐ€ ๋‚˜ํƒ€๋‚˜์ง€ ์•Š๊ณ  ์–ด๋…ธํ…Œ์ด์…˜ @WebServlet("/ex1")์— ์“ด /ex1๊ฐ€ ๋งจ ๋’ค์— ๋ถ™์–ด ์ฃผ์†Œ์ฐฝ์—๋Š” http://localhost:8090/JSP6/ex1 ์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋œ๋‹ค.

${Student}<br>
ํ•™์ƒ ์ด๋ฆ„ : ${Student.getName() }<br>
๊ตญ์–ด : ${Student.getKor() }<br>
์˜์–ด : ${Student.getEng() }<br>
์ˆ˜ํ•™ : ${Student.getMath() }<br>
  
<%
  Student kim = (Student) request.getAttribute("Student");
%>
<%=kim.getName() %><br>
${Student.name }<br><!-- el ํ‘œํ˜„์‹์—์„œ๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ get/set ๋ฉ”์„œ๋“œ๋ฅผ ์ž๋™์œผ๋กœ ๊ตฌํ˜„ํ•ด ์‚ฌ์šฉํ•จ. ๊ทธ๋ž˜์„œ ๋ณ€์ˆ˜๋ช…์œผ๋กœ๋„ ์ ‘๊ทผ๊ฐ€๋Šฅ -->
  
<!-- ์ข€ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ์“ฐ๊ธฐ -->
<c:set var="kim" value="${Student }"/>
  
${kim.name }
  
<hr>
  
${requestScope.memberList }<br>
${requestScope.memberList[0].name }
  • ๊ทธ๋ฆฌ๊ณ  ์•„๊นŒ ์„œ๋ธ”๋ฆฟ์—์„œ ํ•จ๊ป˜ ์ €์žฅํ–ˆ๋˜ ํด๋ž˜์Šค ๊ฐ์ฒด๋„ ์œ„์™€ ๊ฐ™์ด el ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•ด์„œ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.

ํƒœ๊ทธ: ,

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: