L03 서블릿 메소드(1/2)

2016. 10. 25. 17:37Programming/JSP

**배운 내용**

1) doGet vs. doPost

doGet: URL 방식으로 정보가 전달되므로 보안에 취약(검색에 이용됨)

doPost: header 이용해 정보 전달되므로 보안에 강함(로그인에 주로 이용됨)


2) doGet, doPost 호출: 호출 주소/방식 지정 + submit 버튼 추가

<form action="./method" method="post">

<p>
                      <label for="userId">ID: </label>
                      <input id="userId" name="id" value="jsp" type="text">

<input type="submit" value="제출">


3) 제출시 전송된 parameter 받기

String id = request.getParameter("id");


4) response, request 객체 메소드 정리


 Request

Response 

 getContextPath()

 getSession()

 getProtocol()

 getRequestURL()

 getQueryString()

 getCharacterEncoding()

 addCookie(쿠키명): 쿠키 지정

 sendRedirect(URL): 지정한 URL로 이동


5) 서블릿 리스너 이용해 생명주기 감시


1) @WebListener 태그





2) web.xml 파일 이용







<Index.jsp>



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Servlet의 Method 알아보기</title>
</head>
<body>
           <h1>서블릿의 동작원리</h1>
           <h3>
               <a href="L01CallMethod.jsp">doGet과 doPost를 호출해보자</a>
           </h3>
           <hr>
           <h3>
               <a href="L02SignupForm.jsp">회원가입 form을 만들어보자</a>
           </h3>
           <hr>
           <h3>
               <a href="L03LifeCycle.jsp">서블릿의 생명주기 확인</a>
           </h3>
           <hr>
</body>
</html>
 
cs



<1번 클릭시: L01CallMethod.jsp>




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
           <h1>L01CallMethod 서블릿을 get방식으로 호출하기</h1>
           <h3>
              <a href="./method?id=jsp&pass=1234">id와 pass를 doGet()에 전달</a>
           </h3>
           <hr>
           <h1>L01CallMethod 서블릿을 post방식으로 호출하기</h1>
           <form action="./method" method="post">
                      <p>
                      <label for="userId">ID: </label>
                      <input id="userId" name="id" value="jsp" type="text">

                      <!-- 
                      input 태그: 파라미터를 post 방식으로 전달하는 도구
                      name 속성: 파라미터의 key값이 됨
                      for 속성: label에만 존재하고 어떤 input의 label인지 표시함
                      id 속성: html page에서 유일한 값(pk와 비슷)
                       -->

                      </p>
                      <p>
                      <label for="userPW">PW: </label>
                      <input id="userPW" name="pass" value="1234" type="password">
                      </p>
                      <input type="submit" value="제출">
           </form>
</body>
</html>
 
cs



<제출 누르면: L01CallMethod.java(서블릿)>





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
@WebServlet("/method")
public class L01CallMethod extends HttpServlet {
           private static final long serialVersionUID = 1L;
           protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
           //doGet로 id/pw 출력
           response.setContentType("text/html; charset=utf-8");
           String id = request.getParameter("id");
           String pw = request.getParameter("pass");
           response.getWriter()
           .append("<html><body>")
           .append("<h1>"+id+"/"+pw+" 환영합니다."+"</h1>")
           .append("</body></html>");
           }
           
           protected void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
           //doPost로 id/pw 출력
           response.setContentType("text/html; charset=utf-8");
           String id = request.getParameter("id");
           String pw = request.getParameter("pass");
           response.getWriter().append(id+"/"+pw+" 환영합니다");
           }
}
 
cs



'Programming > JSP' 카테고리의 다른 글

L04 Redirect(페이지 넘기기)  (0) 2016.10.26
L03 서블릿 메소드(2/2)  (0) 2016.10.25
L02 JSP 작성  (0) 2016.10.25
L01 서블릿 작성  (0) 2016.10.25
Day 6(9/22) 업데이트 화면& 기능 구현  (0) 2016.09.27