๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Java·๏ปฟServlet·๏ปฟJSP

[IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (2) ํšŒ์› ๊ฐ€์ž…

by Leica 2020. 2. 9.
๋ฐ˜์‘ํ˜•
  • ๊ด€๋ จ ๊ธ€

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (1) ํšŒ์› ๋ชฉ๋ก ์กฐํšŒ

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (2) ํšŒ์› ๊ฐ€์ž…

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (3) ํšŒ์› ์ •๋ณด ์ˆ˜์ •

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (4) ๋ทฐ(JSP) ๋ถ„๋ฆฌํ•˜๊ธฐ


[IntelliJ] JAVA ์„œ๋ธ”๋ฆฟ + MySQL ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ - (2) ํšŒ์› ๊ฐ€์ž…

StudentListServlet.java์— ์‹ ๊ทœ ํ•™์ƒ ๋งํฌ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

out.println("<p><a href='add'>์‹ ๊ทœ ํ•™์ƒ</a></p>");

 

<a href='add'>์™€ ๊ฐ™์ด <a> ํƒœ๊ทธ์˜ ๋งํฌ URL์„ ์ƒ๋Œ€ ๊ฒฝ๋กœ๋กœ ์ง€์ •ํ–ˆ๋‹ค.

์ ˆ๋Œ€ ๊ฒฝ๋กœ๋Š” ์›น ์„œ๋ฒ„ ๋ฃจํŠธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜๊ณ  ์ƒ๋Œ€ ๊ฒฝ๋กœ๋Š” ํ˜„์žฌ ๊ฒฝ๋กœ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•œ๋‹ค.

์•„๋ž˜ ํ‘œ๋ฅผ ๋ณด๋ฉด ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šธ ๊ฒƒ์ด๋‹ค.

 

ํ‘œ - ํ˜„์žฌ URL์ด http://127.0.0.1:8080/web04/student/list ์ผ ๋•Œ ์ง€์ •๋œ URL์— ๋”ฐ๋ฅธ ์‹ค์ œ URL

์ง€์ • URL ์‹ค์ œ URL
add (์ƒ๋Œ€ ๊ฒฝ๋กœ) http://127.0.0.1:8080/web04/student/add
/add (์ ˆ๋Œ€ ๊ฒฝ๋กœ)
http://127.0.0.1:8080/add
/web04/student/add (์ ˆ๋Œ€ ๊ฒฝ๋กœ)
http://127.0.0.1:8080/web04/student/add

 

 

1. ์‹ ๊ทœ ํ•™์ƒ ๋“ฑ๋ก ํผ

์‹ ๊ทœ ํ•™์ƒ ๋“ฑ๋ก์„ ์ฒ˜๋ฆฌํ•  StudentAddServlet ํด๋ž˜์Šค๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ ๋‹ค.

 

@WebServlet("/student/add")
public class StudentAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.println("<html><head><title>์‹ ๊ทœ ํ•™์ƒ ๋“ฑ๋ก</title></head>");
        out.println("<body><h1>์‹ ๊ทœ ํ•™์ƒ ๋“ฑ๋ก</h1>");
        out.println("<form action='add' method='post'>");
        out.println("ํ•™๋ฒˆ : <input type='text' name='student_no'><br>");
        out.println("์ด๋ฆ„ : <input type='text' name='student_name'><br>");
        out.println("์„ฑ๋ณ„ : <input type='text' name='gender'><br>");
        out.println("ํ•™๊ณผ : <input type='text' name='department'><br>");
        out.println("ํ•™๋…„ : <input type='text' name='grade'><br>");
        out.println("๋‚˜์ด : <input type='text' name='age'><br>");
        out.println("์ „ํ™”๋ฒˆํ˜ธ : <input type='text' name='phone_number'><br>");
        out.println("์ฃผ์†Œ : <input type='text' name='address'><br>");
        out.println("<input type='submit' value='๋“ฑ๋ก'>");
        out.println("<input type='reset' value='์ทจ์†Œ'>");
        out.println("</form></body></html>");
    }
}

HttpServlet์„ ์ƒ์†๋ฐ›๊ณ  doGet()์„ overrideํ•˜์—ฌ ํšŒ์›๊ฐ€์ž…ํผ์„ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

 

HttpServlet์€ GenericServlet์˜ ํ•˜์œ„ ํด๋ž˜์Šค์ด๊ธฐ ๋•Œ๋ฌธ์— HttpServlet์„ ์ƒ์†๋ฐ›์€ StudentAddServlet ํด๋ž˜์Šค๋Š” javax.servlet.Servlet ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด๊ฐ€ ๋œ๋‹ค. ์„œ๋ธ”๋ฆฟ ์ปจํ…Œ์ด๋„ˆ๋Š” Servlet ๊ทœ์น™์— ์ •์˜๋œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ธ”๋ฆฟ ๊ฐ์ฒด๋Š” ๋ฐ˜๋“œ์‹œ Servlet ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

 

HttpServlet ํด๋ž˜์Šค ๊ณ„์ธต๋„

 

ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด HttpServlet์˜ service()๊ฐ€ ํ˜ธ์ถœ๋˜๊ณ  service()๋Š” ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ method์— ๋”ฐ๋ผ doGet(), doPost(), doPut() ๋“ฑ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ๋”ฐ๋ผ์„œ HttpServlet์„ ์ƒ์†๋ฐ›์„ ๋•Œ๋Š” servce()๊ฐ€ ์•„๋‹Œ doXXX() ๋ฉ”์†Œ๋“œ๋ฅผ overrideํ•œ๋‹ค.

 

์—ฌ๊ธฐ์„œ๋Š” ์šฐ์„  '์‹ ๊ทœ ํ•™์ƒ' ๋งํฌ๋ฅผ ํด๋ฆญํ–ˆ์„๋•Œ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด doGet() ๋ฉ”์†Œ๋“œ๋ฅผ override ํ•˜์˜€๋‹ค.

 

์‹ ๊ทœ ํ•™์ƒ ๋“ฑ๋ก ํŽ˜์ด์ง€

ํ•™์ƒ ๋ชฉ๋ก ํŽ˜์ด์ง€์—์„œ URL์ด 'add'๋กœ ์ง€์ •๋œ '์‹ ๊ทœ ํ•™์ƒ' ๋งํฌ๋ฅผ ํด๋ฆญํ•˜๋ฉด http://127.0.0.1:8080/web04_war_exploded/student/add๋กœ ์ด๋™ํ•˜๋ฉฐ ๋“ฑ๋ก ํผ์ด ์ถœ๋ ฅ๋œ๋‹ค.

 

<form action='add' method='post'>์˜ action์€ form์ด submit๋์„ ๋•Œ ์‹คํ–‰ํ•  ์„œ๋ธ”๋ฆฟ์˜ URL์ด๋‹ค. ์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๋ฉด http://127.0.0.1:8080/web04_war_exploded/student/add์— POST ์š”์ฒญ์ด ์ „์†ก๋œ๋‹ค.

 

๋‹ค์Œ์œผ๋กœ ์ด๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด doPost() ๋ฉ”์†Œ๋“œ๋ฅผ overrideํ•œ๋‹ค.

 

2. ์‹ ๊ทœ ํ•™์ƒ DB INSERT

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    req.setCharacterEncoding("UTF-8");
    Connection conn = null;
    PreparedStatement pstmt = null;

    Properties jdbcProperties = new Properties();
    jdbcProperties.setProperty("user", "username");
    jdbcProperties.setProperty("password", "****");
    try {
        DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3307/knou", jdbcProperties);
        pstmt = conn.prepareStatement(
                "INSERT INTO STUDENT(STUDENT_NO, DEPARTMENT, STUDENT_NAME, GRADE, GENDER, AGE, PHONE_NUMBER, ADDRESS)" +
                        " VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
        );

        pstmt.setString(1, req.getParameter("student_no"));
        pstmt.setString(2, req.getParameter("department"));
        pstmt.setString(3, req.getParameter("student_name"));
        pstmt.setInt(4, Integer.parseInt(req.getParameter("grade")));
        pstmt.setString(5, req.getParameter("gender"));
        pstmt.setInt(6, Integer.parseInt(req.getParameter("age")));
        pstmt.setString(7, req.getParameter("phone_number"));
        pstmt.setString(8, req.getParameter("address"));
        pstmt.executeUpdate();

        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.println("<html><head>");
        out.println("<title>ํ•™์ƒ ๋“ฑ๋ก</title></head>");
        out.println("<body><p> " + req.getParameter("student_name") + " - ์‹ ๊ทœ ํ•™์ƒ ๋“ฑ๋ก ์„ฑ๊ณต</p></body><html>");
    } catch (SQLException e) {
        throw new ServletException(e);
    } finally {
        try {
            if (pstmt != null) pstmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
        }
    }
}

setCharacterEncoding("UTF-8")๋กœ POST ์š”์ฒญ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ธ์ฝ”๋”ฉ์„ ์ง€์ •ํ•ด์•ผ ํ•œ๋‹ค.

๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด getParameter() ๋ฉ”์†Œ๋“œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ISO-8859-1(ISO-Latin-1)๋กœ ์ธ์ฝ”๋”ฉ๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๊ธ€์ด ๊นจ์ง„๋‹ค.

 

ํ•œ๊ธ€์ด ๊นจ์ง„ ์˜ˆ
DB์—๋„ ๊นจ์ ธ์„œ Insert ๋œ๋‹ค.

 

SQL๋ฌธ์—์„œ '?'๋Š” ์ž…๋ ฅ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ executeUpdate()๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์ „์— setXXX() ๋ฉ”์†Œ๋“œ๋กœ ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค.

setXXX() ๋ฉ”์†Œ๋“œ์˜ ์ฒซ๋ฒˆ์งธ ์ธ์ž๊ฐ’์€ ์ธ๋ฑ์Šค, ๋‘๋ฒˆ์งธ ์ธ์ž๊ฐ’์€ ์‹ค์ œ ์ž…๋ ฅ๊ฐ’์ด๋‹ค.

์ธ๋ฑ์Šค๋Š” 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.

 

๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” SELECT ๋ฌธ์„ ์‹คํ–‰ํ•  ๋•Œ๋Š” executeQuery()๋ฅผ, ๊ฒฐ๊ณผ ๋ ˆ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๋Š” DDL/DML SQL๋ฌธ์„ ์‹คํ–‰ํ•  ๋•Œ๋Š” executeUpdate()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” ์‹ ๊ทœ ํ•™์ƒ์„ DB์— Insert ํ•  ๊ฒƒ์ด๋ฏ€๋กœ executeUpdate()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

์—ฌ๊ธฐ๊นŒ์ง€ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

 

3. Refresh / Redirect

Refresh์™€ Redirect๋ฅผ ์ด์šฉํ•ด์„œ ํšŒ์› ๊ฐ€์ž…(์‹ ๊ทœ ํ•™์ƒ ๋“ฑ๋ก) ํ›„ ํ•™์ƒ ๋ชฉ๋ก ๋ฆฌ์ŠคํŠธ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๋„๋ก ํ•œ๋‹ค. Refresh๋Š” ์ผ์ • ์‹œ๊ฐ„์ด ์ง€๋‚œ ํ›„, Redirect๋Š” ์ฆ‰์‹œ ํŽ˜์ด์ง€๊ฐ€ ์ด๋™๋œ๋‹ค.

 

Refresh

Refresh๋Š” ์ž‘์—… ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์ผ์ • ์‹œ๊ฐ„์ด ์ง€๋‚œ ํ›„ ํŽ˜์ด์ง€๋ฅผ ์ด๋™ํ•œ๋‹ค.

 

Refresh ์‹œ๋‚˜๋ฆฌ์˜ค

โ‘  ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ '์‹ ๊ทœ ํ•™์ƒ' ๋งํฌ๋ฅผ ํด๋ฆญํ•˜์—ฌ 'ํ•™์ƒ ๋“ฑ๋กํผ'์„ ์š”์ฒญํ•œ๋‹ค.

โ‘ก StudentAddServlet์€ ํ•™์ƒ ๋“ฑ๋กํผ์„ ํด๋ผ์ด์–ธํŠธ๋กœ ๋ณด๋‚ธ๋‹ค.

โ‘ข ์›น ๋ธŒ๋ผ์šฐ์ €๋Š” ํ•™์ƒ ๋“ฑ๋กํผ์„ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๊ณ  ์‚ฌ์šฉ์ž๊ฐ€ ํ•™์ƒ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  submitํ•˜๋ฉด ์„œ๋ฒ„์— ๋“ฑ๋ก์„ ์š”์ฒญํ•œ๋‹ค.

โ‘ฃ StudentAddServlet์€ ๋“ฑ๋ก ๊ฒฐ๊ณผ๋ฅผ ํด๋ผ์ด์–ธํŠธ๋กœ ๋ณด๋‚ด๋ฉด์„œ Refresh ์ •๋ณด๋ฅผ ํ•จ๊ป˜ ๋ณด๋‚ธ๋‹ค.

โ‘ค ์›น ๋ธŒ๋ผ์šฐ์ €๋Š” ๋“ฑ๋ก ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ๋ฐ›์€ Refresh ์ •๋ณด์— ๋”ฐ๋ผ n์ดˆ ํ›„ ์„œ๋ฒ„์— 'ํ•™์ƒ ๋ชฉ๋ก ํŽ˜์ด์ง€'๋ฅผ ์š”์ฒญํ•œ๋‹ค. (Refresh)

โ‘ฅ StudentListServlet์€ ํ•™์ƒ ๋ชฉ๋ก์„ ํด๋ผ์ด์–ธํŠธ๋กœ ๋ณด๋‚ธ๋‹ค.

 

Refresh๋ฅผ ๊ตฌํ˜„ํ•˜๋ ค๋ฉด โ‘ฃ StudentAddServlet์ด ๋“ฑ๋ก ๊ฒฐ๊ณผ๋ฅผ ํด๋ผ์ด์–ธํŠธ๋กœ ๋ณด๋‚ผ๋•Œ Refresh ์ •๋ณด๋ฅผ ํ•จ๊ป˜ ๋ณด๋‚ด๋ฉด ๋œ๋‹ค. Refresh ์ •๋ณด๋ฅผ ๋ณด๋‚ด๋Š” ๋ฐฉ๋ฒ•์—๋Š” ์‘๋‹ต ํ—ค๋”๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ HTML meta ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

 

1) ์‘๋‹ต ํ—ค๋”์— ๋ฆฌํ”„๋ ˆ์‹œ ์ •๋ณด ๋ณด๋‚ด๊ธฐ

1
2
3
4
5
6
7
8
    out.println("<body><p> " + req.getParameter("student_name"+ " - ์‹ ๊ทœ ํ•™์ƒ ๋“ฑ๋ก ์„ฑ๊ณต</p></body><html>");
 
    // ๋ฆฌํ”„๋ ˆ์‹œ ์ •๋ณด๋ฅผ ์‘๋‹ต ํ—ค๋”์— ์ถ”๊ฐ€
    // Refresh : ํ•„๋“œ๋ช…
    // 2;url=list : 2์ดˆ ํ›„ 'list'๋กœ ์„œ๋น„์Šค ์žฌ์š”์ฒญ
    resp.addHeader("Refresh""2;url=list");
catch (SQLException e) {
    throw new ServletException(e);
cs

HttpServletResponse์˜ addHeader()๋Š” HTTP ์‘๋‹ต ์ •๋ณด์— ํ—ค๋”๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค.

 

์‘๋‹ต ํ—ค๋”๋กœ ๋ณด๋‚ธ ๊ฒฝ์šฐ ์‘๋‹ต ๋ฉ”์‹œ์ง€

 

2) meta ํƒœ๊ทธ์— ๋ฆฌํ”„๋ ˆ์‹œ ์ •๋ณด ๋ณด๋‚ด๊ธฐ

1
2
3
4
5
6
7
    out.println("<html><head>");
    // ๋ฆฌํ”„๋ ˆ์‹œ ์ •๋ณด๋ฅผ ๋ฉ”ํƒ€ ํƒœ๊ทธ์— ์ถ”๊ฐ€
    out.println("<meta http-equiv='Refresh' content='2; url=list'>");
    out.println("<title>ํ•™์ƒ ๋“ฑ๋ก</title></head>");
    out.println("<body><p> " + req.getParameter("student_name"+ " - ์‹ ๊ทœ ํ•™์ƒ ๋“ฑ๋ก ์„ฑ๊ณต</p></body><html>");
catch (SQLException e) {
    throw new ServletException(e);
cs

<meta http-equiv='Refresh' content='2; url=list'>์™€ ๊ฐ™์ด HTML ๋ณธ๋ฌธ์— ํฌํ•จ์‹œ์ผœ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

 

meta ํƒœ๊ทธ๋กœ ๋ณด๋‚ธ ๊ฒฝ์šฐ ์‘๋‹ต ๋ฉ”์‹œ์ง€

 

๋‘˜ ์ค‘ ํ•˜๋‚˜์˜ ๋ฐฉ๋ฒ•์„ ํƒํ•˜์—ฌ ์ ์šฉํ•˜๊ณ  ์‹คํ–‰ํ•ด๋ณด๋ฉด ์‹ ๊ทœ ํ•™์ƒ ๋“ฑ๋ก ํ›„ 2์ดˆ ๋’ค ๋‹ค์‹œ ํ•™์ƒ ๋ชฉ๋ก ํŽ˜์ด์ง€๋กœ ์ด๋™๋œ๋‹ค.

 

Redirect

Redirect๋Š” ์ž‘์—… ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜์ง€ ์•Š๊ณ  ์ฆ‰์‹œ ํŽ˜์ด์ง€๋ฅผ ์ด๋™ํ•œ๋‹ค.

Redirect ์‹œ๋‚˜๋ฆฌ์˜ค

โ‘  ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ '์‹ ๊ทœ ํ•™์ƒ' ๋งํฌ๋ฅผ ํด๋ฆญํ•˜์—ฌ 'ํ•™์ƒ ๋“ฑ๋กํผ'์„ ์š”์ฒญํ•œ๋‹ค.

โ‘ก StudentAddServlet์€ ํ•™์ƒ ๋“ฑ๋กํผ์„ ํด๋ผ์ด์–ธํŠธ๋กœ ๋ณด๋‚ธ๋‹ค.

โ‘ข ์›น ๋ธŒ๋ผ์šฐ์ €๋Š” ํ•™์ƒ ๋“ฑ๋กํผ์„ ํ™”๋ฉด์— ์ถœ๋ ฅํ•˜๊ณ  ์‚ฌ์šฉ์ž๊ฐ€ ํ•™์ƒ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  submitํ•˜๋ฉด ์„œ๋ฒ„์— ๋“ฑ๋ก์„ ์š”์ฒญํ•œ๋‹ค.

โ‘ฃ StudentAddServlet์€ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธํ•  ํ•™์ƒ ๋ชฉ๋ก ํŽ˜์ด์ง€์˜ URL ์ •๋ณด๋ฅผ ๋ณด๋‚ธ๋‹ค.

โ‘ค ์›น ๋ธŒ๋ผ์šฐ์ €๋Š” ์ฆ‰์‹œ ๋ฐ›์€ URL ์ •๋ณด์— ๋”ฐ๋ผ ์„œ๋ฒ„์— 'ํ•™์ƒ ๋ชฉ๋ก ํŽ˜์ด์ง€'๋ฅผ ์žฌ์š”์ฒญํ•œ๋‹ค. (Redirect)

โ‘ฅ StudentListServlet์€ ํ•™์ƒ ๋ชฉ๋ก์„ ํด๋ผ์ด์–ธํŠธ๋กœ ๋ณด๋‚ธ๋‹ค.

 

์ด๋™ํ•  ํŽ˜์ด์ง€์˜ URL ์ •๋ณด์ธ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ์ •๋ณด๋Š” ์‘๋‹ต ํ—ค๋”์— ์„ค์ •ํ•œ๋‹ค.

 

1
2
3
4
5
6
7
pstmt.setInt(6, Integer.parseInt(req.getParameter("age")));
pstmt.setString(7, req.getParameter("phone_number"));
pstmt.setString(8, req.getParameter("address"));
pstmt.executeUpdate();
 
// ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ
resp.sendRedirect("list");
cs

ํ˜„์žฌ ์„œ๋น„์Šค์˜ ์ž‘์—…์ด ์™„๋ฃŒ๋œ ํ›„ HttpServletResponse์˜ sendRedirect()๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ์ •๋ณด๋ฅผ ๋ณด๋‚ธ๋‹ค. ํ•ด๋‹น URL๋กœ ์ฆ‰์‹œ ์ด๋™๋˜๋ฏ€๋กœ ์ž‘์—… ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์‘๋‹ต ๋ณธ๋ฌธ์ด ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค.

 

Redirect ์‘๋‹ต ๋ฉ”์‹œ์ง€

HTTP ์‘๋‹ต ์ฝ”๋“œ 302๋Š” ์š”์ฒญํ•œ ์ž์›์€ Location ํ—ค๋”์— ์žˆ๋Š” ์ฃผ์†Œ์— ์žˆ์œผ๋‹ˆ ๋‹ค์‹œ ์š”์ฒญํ•˜๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.

์›น ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ด ์‘๋‹ต์„ ๋ฐ›์œผ๋ฉด ์ฆ‰์‹œ Location ํ—ค๋”์˜ ์ฃผ์†Œ๋กœ ์žฌ์š”์ฒญํ•œ๋‹ค.


  • ๊ด€๋ จ ๊ธ€

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (1) ํšŒ์› ๋ชฉ๋ก ์กฐํšŒ

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (2) ํšŒ์› ๊ฐ€์ž…

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(JAVA + MySQL) - (3) ํšŒ์› ์ •๋ณด ์ˆ˜์ •

- [IntelliJ] ํšŒ์›๊ฐ€์ž… ์˜ˆ์ œ(MVC) - (4) ๋ทฐ(JSP) ๋ถ„๋ฆฌํ•˜๊ธฐ

 

#JAVA MySQL INSERT

#Refresh์™€ Redirect

 

References

์—ดํ˜ˆ๊ฐ•์˜ ์ž๋ฐ” ์›น ๊ฐœ๋ฐœ ์›Œํฌ๋ถ(ํ”„๋ฆฌ๋ ‰, 2016, ์—„์ง„์˜)

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€