본문 바로가기

JSP/기본다지기

JSP 2일차 필기 (get방식, post방식으로 호출하기)

새로운 프로젝트 L03ServletMethod를 생성후 WebContent에 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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</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


그다음 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
39
40
41
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>L01MethodServlet을 호출하자</title>
</head>
<body>
    <h1>L01MethodServlet을 get방식으로 호출하자</h1>
    <h3>
        <a href="./method?id=jsp&pass=1234">[id와 pw를 doGet()에 전달]</a>
    </h3>
    <hr>
    <h1>L01MethodServlet을 post방식으로 호출하자</h1>
    <form action="./method" method="post">
        <p>
            <label for="userId">ID: </label>
            <input id="userId" name="id" value="jsp" type="text">
        </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


<p>

<label for="userId">ID: </label>

<input id="userId" name="id" value="jsp" type="text">

</p>


input tag는 파라미터를 post방식으로 전달하는 도구이다.

name 속성은 파라미터의 key값이 된다.


id 속성은 html page에 유일한 값(DB의 primary key와 비슷한 개념)이다.

for 속성은 label에만 존재하고 어떤 input의 label인지 표시한다.

for id는 똑같이 맞춰주어야 한다.


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


input type = "submit"을 서브밋 버튼이라고 부른다.

form 태그 내부에 존재하며, 이 버튼을 누르면 form 태그 내부의 파라미터를 action에 표시된 위치에 제출한다.


값을 넘겨주기 위한 준비는 마무리 되었다.


Java Resources의 src에 com.jsp.method 패키지를 생성하고


그 안에 L01MethodServlet.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
32
33
34
package com.jsp.method;
 
import java.io.IOException;
import java.io.PrintWriter;
 
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 L01MethodServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //id/pw 님 환영합니다.
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print("<html><body>");
        out.print("<h1 style = 'color:blue;'>"+request.getParameter("id")+"/"+request.getParameter("pass")+"님 환영합니다.");
        out.print("</h1></body></html>");
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print("<html><body>");
        out.print("<h1 style = 'color:red;'>"+request.getParameter("id")+"/"+request.getParameter("pass")+"님 환영합니다.");
        out.print("</h1></body></html>");
    }
}
 
cs