ํฐ์คํ ๋ฆฌ ๋ทฐ
[JSP] ๋ก๊ทธ์ธ, ํ์๊ฐ์ , ์์ด๋ ์ค๋ณต๊ฒ์ฌ ๊ธฐ๋ฅ ๊ตฌํํด๋ณด๊ธฐ
doeunnkimm 2023. 4. 20. 11:30
๐ ๋ก๊ทธ์ธ, ํ์๊ฐ์ , ์์ด๋ ์ค๋ณต๊ฒ์ฌ ๊ธฐ๋ฅ ๊ตฌํํด๋ณด๊ธฐ
์ด๋ฒ ๊ธ์์๋ JSP๋ก ์ 3๊ฐ์ง ๊ธฐ๋ฅ์ ๋ํด ๊ตฌํํด ๋ณด๊ฒ ์ต๋๋ค :) ์์ ์์๋ฅผ ๋งค๊ฒจ๊ฐ๋ฉด์ ์ ๋ฆฌํด๋ณด๊ฒ ์ต๋๋ค.
์ฐ์ ๋ฏธ๋ฆฌ ํด๋ ๊ตฌ์กฐ๋ ์๋์ ๊ฐ์ด ์งํํ์ต๋๋ค.
0. MyBatis ์ค์ , Action ์ธํฐํ์ด์ค, Result ํด๋์ค ์์ฑ
๐ src/com.app.mybatis.config (package) / config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/app"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
</configuration>
๐ src/com.app.mybatis.config (package) / MyBatisConfig.java
package com.app.mybatis.config;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisConfig {
private static SqlSessionFactory sqlSessionFactory;
static {
String resource = "com/app/mybatis/config/config.xml";
try {
Reader reader = Resources.getResourceAsReader(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
}
๐ src/com.app (package) / Action.java
package com.app;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface Action {
public Result execute(HttpServletRequest req, HttpServletResponse rep) throws IOException, ServletException;
}
๐ src/com.app (package) / Result.java
package com.app;
public class Result {
private String path;
private boolean isRedirect;
public Result() {;}
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;
}
}
1. Table ๋ง๋ค๊ธฐ
create database app;
use app;
create table tbl_member(
member_id int unsigned auto_increment primary key,
member_identification varchar(500) not null unique,
member_password varchar(500) not null,
member_name varchar(500) not null,
member_birth date not null,
member_phone varchar(500) not null,
member_email varchar(500) not null unique
);
2. VO ๊ฐ์ฒด ๋ง๋ค๊ธฐ
๐ src/com.app.member.domain (package) / MemberVo.java
package com.app.member.domain;
public class MemberVO {
private Long memberId;
private String memberIdentification;
private String memberPassword;
private String memberName;
private String memberBirth;
private String memberPhone;
private String memberEmail;
public MemberVO() {;}
public Long getMemberId() {
return memberId;
}
public void setMemberId(Long memberId) {
this.memberId = memberId;
}
public String getMemberIdentification() {
return memberIdentification;
}
public void setMemberIdentification(String memberIdentification) {
this.memberIdentification = memberIdentification;
}
public String getMemberPassword() {
return memberPassword;
}
public void setMemberPassword(String memberPassword) {
this.memberPassword = memberPassword;
}
public String getMemberName() {
return memberName;
}
public void setMemberName(String memberName) {
this.memberName = memberName;
}
public String getMemberBirth() {
return memberBirth;
}
public void setMemberBirth(String memberBirth) {
this.memberBirth = memberBirth;
}
public String getMemberPhone() {
return memberPhone;
}
public void setMemberPhone(String memberPhone) {
this.memberPhone = memberPhone;
}
public String getMemberEmail() {
return memberEmail;
}
public void setMemberEmail(String memberEmail) {
this.memberEmail = memberEmail;
}
@Override
public String toString() {
return "MemberVO [memberId=" + memberId + ", memberIdentification=" + memberIdentification + ", memberPassword="
+ memberPassword + ", memberName=" + memberName + ", memberBirth=" + memberBirth + ", memberPhone="
+ memberPhone + ", memberEmail=" + memberEmail + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((memberId == null) ? 0 : memberId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MemberVO other = (MemberVO) obj;
if (memberId == null) {
if (other.memberId != null)
return false;
} else if (!memberId.equals(other.memberId))
return false;
return true;
}
}
3. Mapper.xml ๋ง๋ค๊ณ config.xml์ ๋ฑ๋กํ๊ธฐ
๐ src/com.app.mybatis.mapper (package) / memberMapper.xml
ํด๋น ํ์ผ์์๋ ๋น์ฆ๋์ค ๋ก์ง์ ๊ตฌํํ๋ฉด์ ํ์ํ DB์ ์ ๊ทผํ๋ ์ฟผ๋ฆฌ๋ฌธ์ ์ฌ๊ธฐ์ ์์ฑํ๊ณ DAO์์๋ ๋ฐ๋ก ๋ถ๋ฌ์ ์ฌ์ฉํ๊ธฐ๋ง ํ ๊ฒ์ ๋๋ค. ์ฐ์ ํ๋ง ์์ฑํด ๋ก๋๋ค.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="member">
</mapper>
์ด์ ์ฌ๊ธฐ์ ์์ฑํ ์ฟผ๋ฆฌ๋ฌธ์ MyBatis๊ฐ ์ ์ ์๋๋ก MyBatis ๊ด๋ จ ํจํค์ง์ ์๋ config.xml์ ๋ฑ๋กํด์ค๋๋ค.
๐ src/com.app.mybatis.config (package) / config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/app"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mybatis/mapper/memberMapper.xml"/>
</mappers>
</configuration>
4. Mapper์์ SQL๋ฌธ ์์ฑํ๊ธฐ (1) ํ์๊ฐ์ ์ ์ํ
๐ src/com.app.mybatis.mapper (package) / memberMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="member">
<insert id="insert">
insert into tbl_member
(member_identification, member_password, member_name, member_birth, member_phone, member_email)
values(#{memberIdentification}, #{memberPassword}, #{memberName}, #{memberBirth}, #{memberPhone}, #{memberEmail})
</insert>
</mapper>
5. DAO ๋ง๋ค์ด์ ์๋น์ค ๊ด๋ จ ๋ฉ์๋ ์ ์ธํ๊ธฐ (1) ํ์๊ฐ์ ์ ์ํ
๐ src/com.app.member.dao (package) / MemberDAO.java
package com.app.member.dao;
public class MemberDAO {
public SqlSession sqlSession;
public MemberDAO() {
sqlSession = MyBatisConfig.getSqlSessionFactory().openSession(true);
}
// ํ์๊ฐ์
public void insert(MemberVO memberVO) {
sqlSession.insert("member.insert", memberVO);
}
}
6. Controller ๋ง๋ค๊ธฐ with Action ์ธํฐํ์ด์ค (1) ํ์๊ฐ์ ์ ์ํ
Controller ์ด๋ฆ์ ๊ฒฝ์ฐ ์๋น์ค๋ช + (์ฐ์ฐ์ด ํ์ํ Controller๋ผ๋ฉด Ok) + Controller ๋ก ์ง๋ ๊ฑธ๋ก ํ์ต๋๋ค.
๐ src/com.app.member.controller (package) / JoinOkController.java
package com.app.member.controller;
public class JoinOkController implements Action {
@Override
public Result execute(HttpServletRequest req, HttpServletResponse resp) throw IOException, ServletException {
MemberDAO memberDAO = new MemberDAO();
MemberVO memberVO = new MemberVO();
Result result = new Result();
memberVO.setMemberIdentification(req.getParameter("memberIdentification"));
memberVO.setMemberPassword(req.getParameter("memberPassword"));
memberVO.setMemberName(req.getParameter("memberName"));
memberVO.setMemberBirth(req.getParameter("memberBirth"));
memberVO.setMemberPhone(req.getParameter("memberPhone"));
memberVO.setMemberEmail(req.getParameter("memberEmail"));
memberDAO.insert(memberVO);
result.setRedirect(true);
result.setPath(req.getContextPath() + "/login.member");
}
}
7. FrontController์์ ์๋ง์ Controller์ execute()๋ฅผ ์ฌ์ฉํ๋ค
๊ฐ ํด๋ผ์ด์ธํธ๋ค์ Front Controller์๊ฒ ์์ฒญ์ ๋ณด๋ด๊ณ , Front Controller๋ ๊ฐ ์์ฒญ์ ๋ง๋ ์ปจํธ๋กค๋ฌ๋ฅผ ์ฐพ์์ ํธ์ถ์ํต๋๋ค. ๊ณตํต ์ฝ๋์ ๋ํด์๋ Front Controller์์ ์ฒ๋ฆฌํ๊ณ , ์๋ก ๋ค๋ฅธ ์ฝ๋๋ค๋ง ๊ฐ Controller์์ ์ฒ๋ฆฌํ ์์๋๋ก ํฉ๋๋ค.
๐ src/com.app.member (package) / MemberFrontController.java
package com.app.member;
public class MemberFrontController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String target = req.getRequestURI().replace(req.getContextPath() + "/", "").split("\\.")[0];
Result result = null;
if(target.equals("joinOk")) {
result = new JoinOkController().excute(req, resp);
}
if(result !== null) {
if(result.isRedirect()) {
resp.sendRedirect(result.getPath());
} else {
req.getRequestDispatcher(result.getPath()).forward(req, resp);
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
doGet(req, resp);
}
}
์ฌ๊ธฐ๊น์ง ํ๋ฉด ํ์๊ฐ์ ๊ตฌํ์ ๋์ด๊ณ , ์ด์ ์์ผ๋ก ์๋น์ค ํ๋๋ฅผ ๊ตฌํํ๋ ค๊ณ ํ๋ฉด ๋ค์ 4๋ก ๋์๊ฐ๋ฉด์ ๋น์ฆ๋์ค ๋ก์ง์ ์์ฑํ๋ฉด ๋๊ฒ ์ต๋๋ค :)
4. Mapper์์ SQL๋ฌธ ์์ฑํ๊ธฐ (2) ๋ก๊ทธ์ธ์ ์ํ (3) ์์ด๋ ์ค๋ณต๊ฒ์ฌ๋ฅผ ์ํ
๐ src/com/app.mybatis.mapper (package) / memberMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="member">
<insert id="insert">
insert into tbl_member
(member_identification, member_password, member_name, member_birth, member_phone, member_email)
values(#{memberIdentification}, #{memberPassword}, #{memberName}, #{memberBirth}, #{memberPhone}, #{memberEmail})
</insert>
<select id="login" resultType="long">
select member_id from tbl_member
where member_identification = #{memberIdentification} and member_password = #{memberPassword}
</select>
<select id="selectIdentification" resultType="string">
select member_identification from tbl_member where member_identification = #{memberIdentification}
</select>
</mapper>
5. DAO ๋ง๋ค์ด์ ์๋น์ค ๊ด๋ จ ๋ฉ์๋ ์ ์ธํ๊ธฐ (2)๋ก๊ทธ์ธ์ ์ํ (3) ์์ด๋ ์ค๋ณต๊ฒ์ฌ๋ฅผ ์ํ
๐ src/com.app.member.dao (package) / MemberDAO.java
package com.app.member.dao;
import java.util.HashMap;
import org.apache.ibatis.session.SqlSession;
import com.app.member.domain.MemberVO;
import com.app.mybatis.config.MyBatisConfig;
public class MemberDAO {
public SqlSession sqlSession;
public MemberDAO() {
sqlSession = MyBatisConfig.getSqlSessionFactory().openSession(true);
}
// ํ์๊ฐ์
public void insert(MemberVO memberVO) {
sqlSession.insert("member.insert", memberVO);
}
// ์์ด๋ ์ค๋ณต๊ฒ์ฌ
public String selectIdentification(String memberIdentification) {
return sqlSession.selectOne("member.selectIdentification", memberIdentification);
}
// ๋ก๊ทธ์ธ
public Long login(String memberIdentification, String memberPassword) {
HashMap<String, String> loginMap = new HashMap<String, String>();
loginMap.put("memberIdentification", memberIdentification);
loginMap.put("memberPassword", memberPassword);
return sqlSession.selectOne("member.login", loginMap);
}
}
6. Controller ๋ง๋ค๊ธฐ (2)๋ก๊ทธ์ธ์ ์ํ (3)์์ด๋ ์ค๋ณต๊ฒ์ฌ๋ฅผ ์ํ
๐ src/ com.app.member.controller (package) / LoginOkConroller.java
package com.app.member.controller;
public class LoginOkController implements Action {
@Override
public Result execute(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
MemberDAO memberDAO = new MemberDAO();
Long memberId = memberDAO.login(req.getParameter("memberIdentification"), req.getParameter("memberPassword"));
HttpSession session = req.getSession();
Result result = new Result();
result.setRedirect(true);
if(memberId == null) {
// ๋ก๊ทธ์ธ ์คํจ
result.setPath(req.getContextPath() + "/login.member?login=false");
} else {
session.setAttribute("memberId", memberId);
result.setPath(req.getContextPath() + "/list.board");
}
return result;
}
}
๐ src/ com.app.member.controller (package) / CheckIdOkController.java
package com.app.member.controller;
public class CheckIdOkController implements Action {
@Override
public Result execute(HttpServeltRequest req, HttpServletResponse resp) throws IOException, ServletException {
MemberDAO memberDAO = new MemberDAO();
String memberIdentification = memberDAO.selectIdentification(req.getParameter("memberIdentification"));
boolean check = memberIdentification == null;
return null;
}
}
7. FrontController์์ ์๋ง์ Controller์ execute()๋ฅผ ์ฌ์ฉํ๋ค
๐ src/com.app.member (package) / MemberFrontController.java
package com.app.member;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.app.Result;
import com.app.member.controller.JoinOkController;
import com.app.member.controller.LoginOkController;
public class MemberFrontController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String target = req.getRequestURI().replace(req.getContextPath() + "/", "").split("\\.")[0];
Result result = null;
if(target.equals("join")){
result = new Result();
result.setPath("templates/member/join.jsp");
} else if(target.equals("joinOk")){
result = new JoinOkController().execute(req, resp);
} else if(target.equals("login")){
result = new Result();
result.setPath("templates/member/login.jsp");
} else if(target.equals("loginOk")) {
result = new LoginOkController().execute(req, resp);
} else if(target.equals("logout")) {
req.getSession().invalidate();
result = new Result();
result.setPath("templates/member/login.jsp");
}
if(result != null) {
if(result.isRedirect()) {
resp.sendRedirect(result.getPath());
}else {
req.getRequestDispatcher(result.getPath()).forward(req, resp);
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
์์ฒ๋ผ 0, 1, 2, 3๊น์ง๋ ๊ธฐ๋ณธ์ธํ ์ผ๋ก ํด๋๊ณ ์๋ก์ด ์๋น์ค๊ฐ ํ์ํ ๋๋ง๋ค 4, 5, 6, 7์ ๋ฐ๋ณตํ๋ฉด ๋๊ฒ ์ต๋๋ค :)
'๋ฐฑ์๋ > JSP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JSP] JSP๋ก ๊ฒ์ํ ๋ชฉ๋ก ์กฐํ ๊ธฐ๋ฅ ๊ตฌํ (0) | 2023.04.25 |
---|---|
[JSP] MVC ํจํด์ด๋? (0) | 2023.04.20 |
[JSP] ์๋ธ๋ฆฟ(Servlet)์ด๋ ? (0) | 2023.04.19 |
[JSP] MyBatis๋ ? (0) | 2023.04.19 |
[JSP] JSP๋ก ์ฌ์น์ฐ์ฐ ๊ณ์ฐ๊ธฐ ๋ง๋ค๊ธฐ (0) | 2023.04.19 |
- Total
- Today
- Yesterday
- styled-components
- ํ๋ก ํธ์๋ ๊ธฐ์ด
- rtl
- ์๋ฐ
- Python
- ์๋ฐ์คํฌ๋ฆฝํธ ๊ธฐ์ด
- ๋ฆฌ์กํธ ํ
- ๋ํ๋ง
- ํ์ ์คํฌ๋ฆฝํธ
- react
- jest
- ํ๋ก์ ํธ ํ๊ณ
- ์ธํ๋ฐ
- ์คํ์ผ ์ปดํฌ๋ํธ styled-components
- ๋จธ์ ๋ฌ๋
- testing
- ๋ฐ์ดํฐ๋ถ์
- ํ๋ก ํธ์๋
- ๋ฅ๋ฌ๋
- ์๋ฐ์คํฌ๋ฆฝํธ
- ํ์ด์ฌ
- next.js
- react-query
- ํ๋ก ํธ์๋ ๊ณต๋ถ
- CSS
- JSP
- HTML
- ๋ฆฌ์กํธ
- frontend
- TypeScript
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |