/ <form action="/user/UserJoin.us" method="post">의 .us
@WebServlet("*.us")
public class UserFrontController extends HttpServlet {
/*
* doget, doposet, doProcess
* uri 파싱, if문으로 조건
*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doProcess(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doProcess(req, resp);
}
protected void doProcess(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String requestURI = req.getRequestURI();
ActionForward forward = null;
if(requestURI.equals("/user/UserJoin.us")) {
System.out.println("컨트롤러 도착");
// UserJoinAction 호출
// forward 객체로 UserJoinAction의 리턴값을 받음
forward = new UserJoinAction().execute(req,resp);
}
// 페이지 이동 일괄처리
// forward 값에 null은 존재x
if(forward != null) {
if(forward.isRedirect()) {
// redirect
// 응답에 sendRedirect메서드를 통해서 요청을 보내준다.
// URI는 forward객체의 Path가 가지고있다.
resp.sendRedirect(forward.getPath());
}else {
// forward
RequestDispatcher disp = req.getRequestDispatcher(forward.getPath());
disp.forward(req, resp);
}
}
}
}
// ActionForward 역할 -> 어떤 페이지 방식으로 이동 시킬건지,
// 어떤 위치에 페이지 이동을 시킬건지에 대한 경로를 가진 class
public class ActionForward {
// isRedirect ->Redirect 방식으로 간다는 정보를 담는 boolean변수 -> 페이지 이동 true
// DB에서 select해온 내용이 있어서 forward 방식으로한다. -> false
private boolean isRedirect;
private String path; // 경로
public ActionForward() {}
public ActionForward(boolean isRedirect, String path) {
super();
this.isRedirect = isRedirect;
this.path = path;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public boolean isRedirect() {
return isRedirect;
}
public void setRedirect(boolean isRedirect) {
this.isRedirect = isRedirect;
};
}
public interface Action {
public ActionForward execute(HttpServletRequest req, HttpServletResponse rsp);
}
public class UserJoinAction implements Action{
@Override
public ActionForward execute(HttpServletRequest req, HttpServletResponse rsp) {
System.out.println("excute도착");
UserDAO udao = new UserDAO();
UserDTO udto = new UserDTO();
// 기본생성자 객체 사용
ActionForward forward = new ActionForward();
// userid 넘어온 값을 Userid에 세팅
udto.setUserid(req.getParameter("userid"));
udto.setUserpw(req.getParameter("userpw"));
udto.setUsername(req.getParameter("username"));
udto.setUserphone(req.getParameter("userphone"));
udto.setUseraddr(req.getParameter("useraddr"));
if(udao.join(udto)) {
// DAO완성 후 다시 시작
// 회원가입 성공, app/user/loginview.jsp
// redirect
forward.setRedirect(true); // Redirect여부
forward.setPath("/app/user/loginview.jsp");
}else {
// 회원가입 실패, index.jsp
// redirect
forward.setRedirect(true);
forward.setPath("/index.jsp");
}
return forward; // UserFrontController 로 이동
}
}
public class UserDTO {
private String userid;
private String userpw;
private String username;
private String userphone;
private String useraddr;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUserpw() {
return userpw;
}
public void setUserpw(String userpw) {
this.userpw = userpw;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserphone() {
return userphone;
}
public void setUserphone(String userphone) {
this.userphone = userphone;
}
public String getUseraddr() {
return useraddr;
}
public void setUseraddr(String useraddr) {
this.useraddr = useraddr;
}
}
public class UserDAO {
SqlSessionFactory factory = SqlMapConfig.getFactory();
SqlSession sqlsession;
public UserDAO() {
sqlsession = factory.openSession(true);
}
public boolean join(UserDTO udto) {
boolean result = false;
if( sqlsession.insert("User.join",udto) != 0) {
result = true;
}
return result;
}
}
<mapper namespace="User">
<insert id="join" parameterType="userdto">
insert into "USER" values(#{userid}, #{userpw}, #{username}, #{userphone},#{useraddr})
</insert>
</mapper>
<typeAliases>
<typeAlias type="com.koreait.app.user.dao.UserDTO" alias="userdto"/>
</typeAliases>
DBConnection'''
user -> 예약어라 ""를 사용하면 생성된다..
CREATE TABLE "USER"(
userid varchar2(300) PRIMARY KEY,
userpw varchar2(300),
username varchar2(300),
userphone varchar2(300),
useraddr varchar2(1000)
);
SELECT * FROM "USER";
<body>
<form action="/user/UserJoin.us" method="post">
<p>
아이디 <input type="text" name="userid">
</p>
<p>
비밀번호 <input type="password" name="userpw">
</p>
<p>
이름 <input type="text" name="username">
</p>
<p>
전화번호 <input type="text" name="userphone">
</p>
<p>
주소 <input type="text" name="useraddr">
</p>
<p>
<input type="submit" name="회원가입">
</p>
</form>
</body>