19.05.23 고급 서블릿 파라미터 활용 (model 2 동영상 8강)

Back-End/JSP 2019. 5. 23. 11:38
728x90
반응형

-(Bean 클래스를 활용) 서블릿 파라미터 처리-


use bean은 서블릿에서 사용불가 (자바코드이기 때문에)


requset.getParameter를 사용하지 않고 


빈클래스를 만들어 jsp로 자료를 한번에 넘기기




-예제 및 출력 결과-


Memberjoin2.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
42
43
44
45
<%@ 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>
<body>
    <center>
        <h2>회원 가입 양식</h2>
        <form action="MProc2" method="post">
            <!--서블릿 이름 (맵핑할) 클래스 이름이 아님-->
            <table width="400" border="1" bordercolor="gray">
                <tr height="40">
                    <td width="150" align="center">아이디</td>
                    <td width="250"><input type="text" name="id"></td>
                </tr>
 
                <tr height="40">
                    <td width="150" align="center">패스워드</td>
                    <td width="250"><input type="password" name="password"></td>
                </tr>
 
                <tr height="40">
                    <td width="150" align="center">이메일</td>
                    <td width="250"><input type="email" name="email"></td>
                </tr>
 
                <tr height="40">
                    <td width="150" align="center">전화번호</td>
                    <td width="250"><input type="tel" name="tel"></td>
                </tr>
 
                <tr height="40">
                    <td width="150" align="center">주소</td>
                    <td width="250"><input type="text" name="address"></td>
                </tr>
 
                <tr height="40">
                    <td width="40" align="center" colspan="2"><input type="submit"
                        value="회원가입"></td>
                </tr>
 
            </table>
        </form>
    </center>
</body>
</html>
cs



MemberJoinProc2.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package control;
 
import java.io.IOException;
 
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 model.MemberBean;
 
/**
 * Servlet implementation class MemberJoinProc2
 */
@WebServlet("/MProc2"// Memberjoin2 jsp파일에서 MProc2를 호출하고 있으므로 url도 그거에 맞게끔 변경 (맵핑 한다)
public class MemberJoinProc2 extends HttpServlet {
 
    // doGet와 doPost는 값을 2개 밖에 받지못한다 . (request, response 두개만 받을수 있다. 개수가 넘어갈시에는
    // 넘겨지지 않음)
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        reqPro(request, response);
    }
 
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        reqPro(request, response);
    }
 
    protected void reqPro(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
 
        // 유즈빈은 자바코드이므로 사용이 불가하다 그러므로 멤버빈 클래스를 활용한다.
 
        // 멤버빈을 사용하기 위해 멤버빈 객체를 생성
        MemberBean bean = new MemberBean();
 
        // 값들을 하나씩 받아와 빈 클래스에 저장함
        bean.setId(request.getParameter("id"));
        bean.setPassword(request.getParameter("password"));
        bean.setEmail(request.getParameter("email"));
        bean.setTel(request.getParameter("tel"));
        bean.setAddress(request.getParameter("address"));
 
        // request 객체에 bean 클래스를 추가
        request.setAttribute("bean", bean);
 
        RequestDispatcher dis = request.getRequestDispatcher("MemberView.jsp");
        dis.forward(request, response);
 
    }
}
 
cs



MemberBean.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package model;
 
public class MemberBean {
 
    private String id;
    private String password;
    private String email;
    private String tel;
    private String address;
 
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }
 
    public String getTel() {
        return tel;
    }
 
    public void setTel(String tel) {
        this.tel = tel;
    }
 
    public String getAddress() {
        return address;
    }
 
    public void setAddress(String address) {
        this.address = address;
    }
 
}
 
cs



MemberView.jsp (빈클래스에서 자료를 넘겨받아서 출력하는 페이지)

1
2
3
4
5
6
7
8
9
10
11
12
13
<%@ 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>당신의 아이디는 ${bean.id } 당신의 패스워드는 ${bean.password }
 
 
</body>
</html>
cs




728x90
반응형
: