π’ μ¬μ©μλ‘λΆν° νμΌμ μ λ ₯λ°μ μΉνλ‘κ·Έλ¨μ μμ²νμ¬ μ λ¬νκΈ° μν΄μλ λ°λμ method μμ±κ°μ [POST]λ‘ μ€μ νκ³ enctype μμ±κ°μ [multipart/form-data]λ‘ μ€μ
π enctype μμ± : [application/x-www-form-urlencoded] λλ [multipart/form-data] μ€ νλλ₯Ό μμ±κ°μΌλ‘ μ€μ
β [application/x-www-form-urlencoded] : μ λ ₯κ°μ μΈμ½λ© μ²λ¦¬λ λ¬Έμκ°μΌλ‘ μ λ¬(κΈ°λ³Έ) - νμΌ μ λ¬ λΆκ°λ₯
β [multipart/form-data] : λͺ¨λ μ λ ₯κ°μ μμλ°μ΄ν(λ©ν°ννΈ νΌλ°μ΄ν)λ‘ μ λ¬ - νμΌ μ λ¬ κ°λ₯πfile_view.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <!-- μ¬λ¦°μ΄μ νμΌμ μ λ ₯λ°μ μ²λ¦¬νμ΄μ§(view.itwill)λ₯Ό μμ²νμ¬ μ λ¬νλ μΉλ¬Έμ --> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Servlet</title> <link rel="stylesheet" href="css/common.css" type="text/css"/> </head> <body> <h1>νμΌ μ λ‘λ-1</h1> <hr/> μ λ ₯ λ°μ΄ν λ° μ λ ₯ νμΌμ μΉλΈλΌμ°μ μ μΆλ ₯<br/> <hr/> <!-- μ¬μ©μλ‘λΆν° νμΌμ μ λ ₯λ°μ μΉνλ‘κ·Έλ¨μ μμ²νμ¬ μ λ¬νκΈ° μν΄μλ λ°λμ method μμ±κ°μ [POST]λ‘ μ€μ νκ³ enctype μμ±κ°μ [multipart/form-data]λ‘ μ€μ --> <!-- enctype μμ± : [application/x-www-form-urlencoded] λλ [multipart/form-data] μ€ νλλ₯Ό μμ±κ°μΌλ‘ μ€μ --> <!-- => [application/x-www-form-urlencoded] : μ λ ₯κ°μ μΈμ½λ© μ²λ¦¬λ λ¬Έμκ°μΌλ‘ μ λ¬(κΈ°λ³Έ) - νμΌ μ λ¬ λΆκ°λ₯ --> <!-- => [multipart/form-data] : λͺ¨λ μ λ ₯κ°μ μμλ°μ΄ν(λ©ν°ννΈ νΌλ°μ΄ν)λ‘ μ λ¬ - νμΌ μ λ¬ κ°λ₯ --> <form action="view.itwill" method="post" enctype="multipart/form-data"> <table border="0" cellpadding="0" cellspacing="0" width="500" align="center"> <tr> <td valign="top" bgcolor="#000000" align="center"> <table border="0" cellpadding="1" cellspacing="1" width="500"> <tr bgcolor="#556b2f" class="menu"> <td align="center" colspan="2"> λ©ν°ννΈ/νμΌμ λ‘λ(μ λ‘λ λ°μ΄ν° 보기) </td> </tr> <tr bgcolor="#f4f4f4"> <td width="100" align="center" class="t1"> μ¬λ¦°μ΄ </td> <td width="400" align="left"> <input type="text" name="name" size="10" maxlength="10" class="TXTFLD"> </td> </tr> <tr bgcolor="#f4f4f4"> <td width="100" align="center" class="t1"> νμΌμ ν </td> <td width="400" align="left"> <input type="file" name="fileone" size="40" class="TXTFLD"> </td> </tr> <tr bgcolor="#f4f4f4"> <td align="center" colspan="2"> <input type="submit" value="μ¬λΌκ°λΌ.."> </td> </tr> </table> </td> </tr> </table> </form> </body> </html>
β μ λ ₯νμ΄μ§μμ μ λ¬λ μμλ°μ΄ν°(λ©ν°ννΈ νΌλ°μ΄ν°)λ₯Ό ν΄λΌμ΄μΈνΈμκ² μ λ¬νμ¬ μλ΅ μ²λ¦¬
π’ λΉμ μμ μΈ μμ²μ λν μ²λ¦¬
if(request.getMethod().equals("GET")) { response.sendRedirect("file_view.html"); return; }
/-------------------------------------------------------------------
π’ 리νμ€νΈ λ©μΈμ§μ λͺΈμ²΄λΆμ μ μ₯κ°μ λν μΊλ¦ν°μ λ³κ²½request.setCharacterEncoding("utf-8");
π’ μ λ¬κ°(μ λ¬νμΌ)μ λ°νλ°μ μ μ₯
β μμλ°μ΄ν°(λ©ν°ννΈ νΌλ°μ΄ν°)λ‘ μ λ¬λλ κ° λλ νμΌμ HttpServletRequest κ°μ²΄μ getParameter() λ©μλλ₯Ό νΈμΆνμ¬ κ° λλ νμΌμ λ°ν λΆκ°λ₯String name=request.getParameter("name"); String fileone=request.getParameter("fileone");
π’ μλ΅κ²°κ³Ό μμ±
out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<meta charset='UTF-8'>"); out.println("<title>Servlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>μ λ ₯κ°κ³Ό μ λ ₯νμΌ</h1>"); out.println("<hr>"); out.println("<p>μ¬λ¦°μ΄ = "+name+"</p>"); out.println("<p>νμΌλͺ = "+fileone+"</p>"); out.println("</body>"); out.println("</html>")
-------------------------------------------------------------------/
π HttpServletRequest.getInputStream() : 리νμ€νΈ λ©μΈμ§μ λͺΈμ²΄λΆμ μ μ₯λμ΄ μ λ¬λλ μμλ°μ΄ν°λ₯Ό μ½κΈ° μν μ λ ₯μ€νΈλ¦Ό(ServletInputStream κ°μ²΄)μ λ°ννλ λ©μλ
ServletInputStream in=request.getInputStream();
πFileViewServlet.java
package xyz.itwill.servlet; // import java.io.IOException; import java.io.PrintWriter; // import javax.servlet.ServletException; import javax.servlet.ServletInputStream; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; // //μ λ ₯νμ΄μ§(file_view.html)μμ μ λ¬λ μ λ ₯κ°(μ¬λ¦°μ΄)κ³Ό μ λ ₯νμΌμ λ°νλ°μ ν΄λΌμ΄μΈνΈμκ² μ λ¬νλ μλΈλ //β μ λ ₯νμ΄μ§μμ μ λ¬λ μμλ°μ΄ν°(λ©ν°ννΈ νΌλ°μ΄ν°)λ₯Ό ν΄λΌμ΄μΈνΈμκ² μ λ¬νμ¬ μλ΅ μ²λ¦¬ @WebServlet("/view.itwill") public class FileViewServlet extends HttpServlet { private static final long serialVersionUID = 1L; // protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out=response.getWriter(); // //λΉμ μμ μΈ μμ²μ λν μ²λ¦¬ if(request.getMethod().equals("GET")) { response.sendRedirect("file_view.html"); return; } // /* //리νμ€νΈ λ©μΈμ§μ λͺΈμ²΄λΆμ μ μ₯κ°μ λν μΊλ¦ν°μ λ³κ²½ request.setCharacterEncoding("utf-8"); // //μ λ¬κ°(μ λ¬νμΌ)μ λ°νλ°μ μ μ₯ //β μμλ°μ΄ν°(λ©ν°ννΈ νΌλ°μ΄ν°)λ‘ μ λ¬λλ κ° λλ νμΌμ HttpServletRequest κ°μ²΄μ getParameter() λ©μλλ₯Ό νΈμΆνμ¬ κ° λλ νμΌμ λ°ν λΆκ°λ₯ String name=request.getParameter("name"); String fileone=request.getParameter("fileone"); // //μλ΅κ²°κ³Ό μμ± out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<meta charset='UTF-8'>"); out.println("<title>Servlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>μ λ ₯κ°κ³Ό μ λ ₯νμΌ</h1>"); out.println("<hr>"); out.println("<p>μ¬λ¦°μ΄ = "+name+"</p>"); out.println("<p>νμΌλͺ = "+fileone+"</p>"); out.println("</body>"); out.println("</html>"); */ // //HttpServletRequest.getInputStream() : 리νμ€νΈ λ©μΈμ§μ λͺΈμ²΄λΆμ μ μ₯λμ΄ μ λ¬λλ μμλ°μ΄ν°λ₯Ό μ½κΈ° μν μ λ ₯μ€νΈλ¦Ό(ServletInputStream κ°μ²΄)μ λ°ννλ λ©μλ ServletInputStream in=request.getInputStream(); // out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<meta charset='UTF-8'>"); out.println("<title>Servlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>μ λ ₯κ°κ³Ό μ λ ₯νμΌ</h1>"); out.println("<hr>"); out.println("<p>리νμ€νΈ λ©μΈμ§μ λͺΈμ²΄λΆμ μ μ₯λμ΄ μ λ¬λ μμλ°μ΄ν°(λ©ν°ννΈ νΌλ°μ΄ν°)</p>"); out.println("<hr>"); out.println("<pre>"); while(true) { int readByte=in.read(); if(readByte==-1) break; out.write(readByte); } out.println("</pre>"); out.println("</body>"); out.println("</html>"); } }