1. ๊ฒ์ํ์ ๋ค์ด๊ฐ ์ ๋ณด๋ฅผ ๋ด์ DTO ํฌ๋์ค ์์ฑ
public class BoardDTO {
private long num;
private String subject;
private String name;
private String content;
private String pwd;
private String ipAddr;
private String reg_date;
private int hintCount;
}
DTO ํด๋์ค๋ฅผ ์์ฑํ๊ณ ํด๋น ํด๋์ค์ getter()/setter()์ ์์ฑํ๋ค.
DTO ํด๋์ค์ ๋ค์ด๊ฐ๋ ๋ฐ์ดํฐ๋ก๋ ํ ์ด๋ธ์ ์ฐธ๊ณ ํ๋ฉด ๋๋๋ฐ,
์ค๋ผํด์์ ์ฒ๋ฆฌํ ์ ์๋ ๊ฒ์ ์ ๊ฐ์ ธ์ฌ ์๋ ์๊ณ ์ฐ์ฐ์ ๋ฐ๋ผ ์ถ๊ฐ๋๋ ๊ฒ๋ ์๋ค.
2. HttpServlet์ ์์ ๋ฐ์ ํด๋์ค๋ฅผ ์์ฑํ๋ค.
public class BoardServlet extends HttpServlet {
}
3. doGet/ doPost ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ํ๋ค.
@WebServlet("/๊ฒฝ๋ก/*")
public class ์๋ธ๋ฆฟํด๋์ค๋ช
extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
execute(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
execute(req, resp);
}
protected void forward(HttpServletRequest req, HttpServletResponse resp, String path)
throws ServletException, IOException {
// ์๋ธ๋ฆฟ์์ ํฌ์๋ฉ(path๋ก ํฌ์๋ฉ). // ํฌ์๋ฉ์์์ /๋ cp ๊น์ง๋ฅผ ์๋ฏธํ๋ค.
RequestDispatcher rd=req.getRequestDispatcher(path);
rd.forward(req, resp);
}
protected void execute(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
// cp๋ถํฐ ๋๊น์ง ์ฃผ์
String uri = req.getRequestURI();
if(uri.indexOf("์ฃผ์1.do") != -1) {
๋ฉ์๋1(req. resp);
} else if(uri.indexOf("์ฃผ์2.do") != -1) {
๋ฉ์๋2(req. resp);
}
}
protected void ๋ฉ์๋1(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// ์ฃผ์1 ์ฒ๋ฆฌ ๋ด์ฉ
}
protected void ๋ฉ์๋2(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// ์ฃผ์2 ์ฒ๋ฆฌ ๋ด์ฉ
}
}
์ฃผ์๋ฅผ ์ค์ ํ๋ ๋ฐฉ๋ฒ
annotation ์ด์ฉ
= > @WebServlet("/bbs/*")
web.xml์์ ์๋ธ๋ฆฟ ํ๊ทธ๋ฅผ ์ด์ฉํ๋ ๋ฐฉ๋ฒ
4.
์ฃผ์๊ฐ bbs๋ก ์์ํ๋ฉด ๋ญ๊ฐ ์๋ ์๊ด์ด ์๋ค.
// jsp์ ์๋ธ๋ฆฟ์ ์ฃผ์๊ฐ ๋์ผํ๋ฉด ์๋ธ๋ฆฟ์ด ์คํ๋๋ค. ์๋ธ๋ฆฟ์ด ๋ ์ฐ์ ์์๊ฐ ๋๋ค. ใ
'๐ปProgramming > JSP' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JSP] JSTL (0) | 2022.10.05 |
---|---|
[JSP] ํํ์ธ์ด EL - EL ์ฐ์ฐ์/EL ๋ด์ฅ๊ฐ์ฒด (1) | 2022.10.04 |
[JSP] ํ๋ผ๋ฏธํฐ๋ฅผ ๋๊ฒจ ๋ฐ๋ ๋ฐฉ๋ฒ - getParameter()/getParameterValues()/getParameterMap (0) | 2022.09.28 |
GET๊ณผ POST ์ฐจ์ด์ (0) | 2022.09.28 |
[JSP] 04. Request Parameter - GET/POST (0) | 2022.09.27 |