ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 서블릿 - 사용자 입력을 통한 GET, POST 요청
    servlet/servlet 2021. 11. 26. 03:16
    728x90
    반응형
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<div>
    		<form action ="hello">	
    			<div>
    				<label>"안녕하세요"를 몇번?</label>
    			</div>
    			<div>
    				<input type="text" name="cnt"/>
    				<input type="submit" name="출력"/>
    			</div>
    		</form>
    	
    	</div>
    </body>
    </html>

    GET 요청 (값이 1개)

    <form action ="hello">                 //     Annotation에 매핑된 hello 로 간다    
    <input type="text" name="cnt"/>   //     cnt 값입력            /hello?cnt= 값이 전달

     

    -----------------------------------------------------------------------------------------------------------------------------------

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    	<div>
    		<form action="notice-reg" method="post">
    			<div>
    				<label>제목:</label><input name="title" type="text" >
    			</div>
    			<div>
    				<label>내용:</label>
    				<textarea name="content"></textarea>
    			</div>
    			<div>
    				<input type="submit" value="등록"/>
    			</div>
    		</form>
    	</div>
    </body>
    </html>

     

     

    POST 요청 (url에 아무것도 표시안된다)

    <form action="notice-reg" method="post">       // content 처럼 내용이 많은 경우  POST 해줌
                                                                    // 보안상이유도 포함(url에 나오지않으므로)

     

     

    @WebServlet("/notice-reg")
    public class NoticeReg extends HttpServlet{
    	
    	@Override
    	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		
    		resp.setCharacterEncoding("UTF-8");
    		resp.setContentType("text/html; charset=UTF-8");
    		PrintWriter out = resp.getWriter();
    		req.setCharacterEncoding("UTF-8");    // 입력받은 문자를 UTF-8로 읽음 이거 없으면 이상한 문자가 나옴
            
    		String title = req.getParameter("title");
    		String content = req.getParameter("content");
    		
    		out.println(title);
    		out.println(content);
    	}
    }

     

     

     

     

     

    728x90
    반응형

    댓글

Designed by Tistory.