코딩하렴

JSP&Servlet MVC2패턴으로 회원가입 만들기 1

by 으렴

이전글에서 마지막 서블릿을만들고 그 곳에 메시지를 만들고 끝이났다.

hello.java를 지우자

그리고 Member패키지를 만들어 준다.

위와같은 것들을 만들어 줄 예정이다.

board는 나중에 만들 것이니 신경 ㄴㄴ

public interface Action {
public ActionForward execute(HttpServletRequest request,HttpServletResponse response)
throws Exception;
}

Action 인터페이스와

public class ActionForward {
private boolean isRedirect = false;
private String path = null;
public boolean isRedirect() {
return isRedirect;
}
public void setRedirect(boolean Redirect) {
this.isRedirect = Redirect;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}

Action Forward를 만들어 줌.

우리는 이것으로 경로를 컨트롤러에 전달할 예정.

<h1>login/main</h1>
<a href="../signUp.m">join</a>

view/index.jsp에다가 위의 경로를 추가한다.

join을 클릭하면 회원가입 페이지가 나오도록 만들것이다.

다음으로 Member 패키지에다가 MemberController.java를 만들어준다.

어노테이션을 보면 @webServlet("*.m");이라고 해줬는데

.m이 맨 뒤에 붙은 모든 경로는 이곳으로 들어온다는 뜻이 된다.

@WebServlet("*.m")
public class MemberController extends HttpServlet implements javax.servlet.Servlet {
private static final long serialVersionUID = 1L;
protected void doProcess(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String RequestURI = request.getRequestURI(); //
String contextPath = request.getContextPath(); //
String command = RequestURI.substring(contextPath.length()); // 부르는 경로
ActionForward forward = null;
Action action = null;
/* 여기서 경로가 나뉘어 집니다. */
if (command.equals("/signUp.m")) { // sign up page send redirect
System.out.println("signUp 들어갔습니다.");
forward = new ActionForward();
forward.setRedirect(false);
forward.setPath("/view/member/signUp.jsp");
}
/* Redirect OR dispatcher */
if (forward.isRedirect()) { //forward.isRedirect() == false
response.sendRedirect(forward.getPath());
} else { //forward.isRedirect() == true
RequestDispatcher dispatcher = request.getRequestDispatcher(forward.getPath());
dispatcher.forward(request, response);
}
}
/* get으로 가든 post로 가든 doProcess로 간다요. */
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doProcess(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doProcess(request, response);
}

command를 보면 호출한 경로가 담아진다.

command.equals("/signUp.m")에서 호출한 경로가 /signUp.m과 같게된다면

signUp.jsp경로를 Path에 넣으면 requestDispatcher가 그 경로로 보내준다.

여기서 호출한 경로가 forward인지 send redirect인지 결정된다.

 

이렇게 하고 view/member/signUp.jsp를 만들어준다.

저 signUp.jsp에다가 다음의 코드를 추가한다.

<form method="post" action="./signUp_submit.m">
<h1>sign up</h1>
<p>id</p>
<input type="text" name="user_id"> &nbsp;
<p>password</p>
<input type="text" name="user_pw"> &nbsp;
<p>name</p>
<input type="text" name="user_name"> &nbsp;
<p>job</p>
<input type="text" name="user_job"> &nbsp;
<input type="submit" value="join in">
</form>

이제 index.jsp에서 서버를 구동한다.

index.jsp
/signUp.m 호출

컨트롤러를 통해 signU.jsp를 불러냈다.

다음은 회원가입을 할거

사이트의 프로필 사진

사이트의 정보

코딩하렴

으렴

활동하기