반응형
250x250
Notice
Recent Posts
Recent Comments
Link
관리 메뉴

Yeonee's Story

이클립스 Servlet (doPost 방식-insert) 본문

⋆ 。゜☁︎ 。⋆ 。゜☾゜。⋆⋆ 。゜☁︎ 。⋆ 。゜☾゜。⋆/JSP

이클립스 Servlet (doPost 방식-insert)

yeonee 여니 2023. 7. 5. 08:29
728x90
반응형
SMALL

안녕하세요.
https://blog.naver.com/sysysy0302 여니입니다 :)

 

MemberInsertController

/**
 * Servlet implementation class MemberInsertController
 */
@WebServlet("/insert.me")
public class MemberInsertController extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MemberInsertController() {
        super();
        // TODO Auto-generated constructor stub
    }

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//1) 인코딩작업
request.setCharacterEncoding("UTF-8");

//2) 요청시 전달값을 뽑아서 변수 및 객체에 기록
String userId = request.getParameter("userId");
String userPwd = request.getParameter("userPwd");
String userName = request.getParameter("userName");
String phone = request.getParameter("phone");
String email = request.getParameter("email");
String address = request.getParameter("address");
String[] interestArr = request.getParameterValues("interest");

String interest = "";

if(interestArr != null) {
interest = String.join(", ", interestArr);
}

// 매개변수있는 생성자를 이용해서 Member객체에 담기
Member m = new Member(userId, userPwd, userName, phone, email, address, interest);

// 3) 요청처리(서비스 매서드 호출 및 결과 받기)
int result = new MemberService().insertMember(m);


// 4) 처리결과를 가지고 사용자가 보게될 응답 뷰 지정
if(result > 0) { // 성공 -> 메인페이지로 url재요청보내기
request.getSession().setAttribute("alertMsg", "회원가입에 성공했습니다.");
response.sendRedirect(request.getContextPath());
}else { // 에러페이지로 포워딩
request.setAttribute("errorMsg", "회원가입에 실패했습니다.");
request.getRequestDispatcher("views/common/errorPage.jsp").forward(request, response);
}
}


MemberService

public int insertMember(Member m) {

Connection conn = getConnection();

int result = new MemberDao().insertMember(conn, m);

if(result > 0) {
commit(conn);
}else {
rollback(conn);
}

close(conn);

return result;
}


MemberDao

public int insertMember(Connection conn, Member m) {

// Insert문 => 처리된 행의 갯수를 반환
int result = 0;

PreparedStatement pstmt = null;

String sql = prop.getProperty("insertMember");

try {
pstmt = conn.prepareStatement(sql);

pstmt.setString(1, m.getUserId());
pstmt.setString(2, m.getUserPwd());
pstmt.setString(3, m.getUserName());
pstmt.setString(4, m.getPhone());
pstmt.setString(5, m.getEmail());
pstmt.setString(6, m.getAddress());
pstmt.setString(7, m.getInterest());

result = pstmt.executeUpdate();

} catch (SQLException e) {
e.printStackTrace();
} finally {
close(pstmt);
}
return result;
}


member-mapper.xml

<entry key="insertMember">
INSERT INTO MEMBER(
USER_NO,
USER_ID,
USER_PWD,
USER_NAME,
PHONE,
EMAIL,
ADDRESS,
INTEREST
)
VALUES (
SEQ_UNO.NEXTVAL,
?,
?,
?,
?,
?,
?,
?
)
</entry>

728x90
반응형
LIST