๐™ฟ๐š›๐š˜๐š“๐šŽ๐šŒ๐š

[Java] ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ

ํ•ด๋ฒ„๋‹ˆ 2023. 11. 14. 16:44
๋ฐ˜์‘ํ˜•

์ž๋ฐ” ๊ต์œก์„ ๋ฐ›์œผ๋ฉฐ ํ˜ผ์ž ๋งค์šฐ ์•„์ฃผ ๋ฒ ๋ฆฌ ๊ฐ„๋‹จํ•œ ๊ฒŒ์‹œํŒ์„ ๋งŒ๋“ค์–ด ๋ดค๋‹ค. 

 

 

DataBase : mariaDB

Back-End : Java, JSTL

Front-End : Jsp

Server : Apach Tomcat 9 

 

 

 

๊ตฌํ˜„

indexํŽ˜์ด์ง€

 

 

 

 

 

๋กœ๊ทธ์ธ ํŽ˜์ด์ง€

 

 

 

 

 

 

 

๋กœ๊ทธ์ธ ํ›„ ์ด๋™๋œ ๊ธ€ ์ „์ฒด๋ณด๊ธฐ ํŽ˜์ด์ง€

 

 

 

 

 

 

 

 

๊ธ€ ์ƒ์„ธ๋ณด๊ธฐ

 

 

 

 

 

 

 

๊ฒ€์ƒ‰ํ•˜๊ธฐ : "์ œ"๋กœ ๊ฒ€์ƒ‰

ํฌํ•จ๋œ ๊ฒƒ๋„ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค. 

 

 

 

 

 

 

 

 

๊ฒ€์ƒ‰ํ•˜๊ธฐ : "์ œ๋ชฉ4"๋กœ ๊ฒ€์ƒ‰

 

 

 

 

๊ฒŒ์‹œ๋ฌผ ์ž‘์„ฑํ•˜๊ธฐ

 

 

 

 

 

 

์ž…๋ ฅํ•˜๊ณ  ๋“ฑ๋ก ๋ˆ„๋ฅด๊ธฐ

 

 

 

 

 

 

๋“ฑ๋ก๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ „์ฒด ๋ชฉ๋ก ํŽ˜์ด์ง€๋กœ ๋„˜์–ด๊ฐ„๋‹ค.

๊ฒŒ์‹œ๊ธ€์ด ์ž˜ ์ž‘์„ฑ๋œ ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

๊ตฌ์กฐ

๋‚˜์ค‘์—๋Š” beans๋Š” beans์—๋‹ค๊ฐ€ ์œ„์น˜์‹œ์ผฐ๊ณ ,

๋‹ค๋ฅธ ์ปจํŠธ๋กค๋Ÿฌ, ์„œ๋น„์Šค, ์„œ๋ธ”๋ ›์€ ๊ทธ ์ƒ์œ„ ํด๋”์—๋‹ค ์œ„์น˜์‹œ์ผฐ๋‹ค. 

 

 

 

 

 

 

 

 

์ „์ฒด ์†Œ์Šค

BoardController.java

package board.beans;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * ๊ฒŒ์‹œํŒ ์ปจํŠธ๋กค๋Ÿฌ
 */
public class BoardController {

    /** ๊ฒŒ์‹œํŒ ์„œ๋น„์Šค */
    private MariaBoardService boardService = new MariaBoardService();
    /** ํšŒ์› ์„œ๋น„์Šค */
    private MariaUserService userService = new MariaUserService();

    /**
     * ๊ธ€ ์ƒ์„ธ ๋ณด๊ธฐ
     * 
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String viewBoardDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        long id = Long.valueOf(request.getParameter("id"));
        BoardPost resultboardpost = boardService.getBoardPost(id);
        request.setAttribute("boardpost", resultboardpost);

        return "/WEB-INF/jsp/board/viewBoardDetail.jsp";
    }

    /**
     * ๊ธ€ ์ „์ฒด ๋ชฉ๋ก
     * 
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String viewBoardPosts(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String subject = request.getParameter("subject");
        List<BoardPost> boardPostList = boardService.getBoardPostAll(subject);
        request.setAttribute("boardPosts", boardPostList);

        return "/WEB-INF/jsp/board/viewBoardPosts.jsp";
    }

    /**
     * ์ƒˆ ๊ฒŒ์‹œ๋ฌผ ์ž‘์„ฑ
     * 
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String viewCreateBoardPost(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {
        return "/WEB-INF/jsp/board/viewCreateBoardPost.jsp";
    }

    /**
     * ๋กœ๊ทธ์ธ ํ™”๋ฉด
     * 
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String viewLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return "/WEB-INF/jsp/board/viewLogin.jsp";
    }

    /**
     * ๊ธ€ ์ƒ์„ฑ ์ฒ˜๋ฆฌ
     * 
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String createBoardPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String id = request.getParameter("creatorId");

        System.out.println(id);
        BoardPost boardPost = boardService.newBoardPost(title, content, id);
        BoardPost postResult = boardService.createBoardPost(boardPost); // db insert

        return null;
    }

    /**
     * 
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = request.getParameter("userName");
        String userPassword = request.getParameter("userPassword");
        User loginUser = userService.getUser(userName, userPassword);
        if (loginUser != null) {
            HttpSession session = request.getSession();
            session = request.getSession();
            session.setAttribute("user", loginUser);
            System.out.println("login User :" + loginUser);
        } else {
            System.out.println("๋กœ๊ทธ์ธ ์•ˆ ๋จ");
        }
        return null;
    }

}

 

 

 

 

 

 

 

 

 

 

 

BoardPost.java

package board.beans;

import java.sql.Timestamp;

/**
 * ๊ฒŒ์‹œํŒ
 */
public class BoardPost {
    /** ๊ธ€๋ฒˆํ˜ธ */
    private long id;
    /** ์ œ๋ชฉ */
    private String subject;
    /** ๋‚ด์šฉ */
    private String content;
    /** ์‚ฌ์šฉ์ž */
    private User creator;
    /** ์ž‘์„ฑ ๋‚ ์งœ */
    private Timestamp createDate;

    /**
     * ์ƒ์„ฑ์ž
     */
    public BoardPost() {
        // Do nothing
    }

    /**
     * ์ƒ์„ฑ์ž
     * 
     * @param id
     * @param subject
     * @param content
     * @param creator
     * @param createDate
     */
    public BoardPost(long id, String subject, String content, User creator, Timestamp createDate) {
        super();
        this.id = id;
        this.subject = subject;
        this.content = content;
        this.creator = creator;
        this.createDate = createDate;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public User getCreator() {
        return creator;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    public Timestamp getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Timestamp createDate) {
        this.createDate = createDate;
    }

    @Override
    public String toString() {
        return "BoardPost [id=" + id + ", subject=" + subject + ", content=" + content + ", creator=" + creator + ", createDate="
                        + createDate + "]";
    }

}

 

 

 

 

 

 

 

 

 

 

 

BoardServlet.java

package board.beans;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * ๊ฒŒ์‹œํŒ ์„œ๋ธ”๋ฆฟ
 */
@WebServlet("/board/BoardServlet/*")
public class BoardServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /** ๊ฒŒ์‹œํŒ ์ปจํŠธ๋กค๋Ÿฌ */
    private BoardController boardController = new BoardController();


    /**
     * @see HttpServlet#HttpServlet()
     */
    public BoardServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


    /**
     * ์„œ๋น„์Šค
     * 
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stu
        // ํ•œ๊ธ€ ์„ค์ •
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        response.setCharacterEncoding("utf-8");

        String pathInfo = request.getPathInfo();
        String methodName = pathInfo.substring(1);

        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");
        System.out.println("48 user:" + user);

        try {
            Method method = boardController.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            String result = (String) method.invoke(boardController, request, response);
            System.out.println("result :" + result);
            if (result != null && !result.equals("")) {

                if ((!result.equals("/login") || !result.equals("/WEB-INF/jsp/board/viewLogin.jsp")) && user == null) {
                    request.getRequestDispatcher("/WEB-INF/jsp/board/viewLogin.jsp").forward(request, response);
                } else {
                    request.getRequestDispatcher(result).forward(request, response);
                }
            } else {
                response.sendRedirect("viewBoardPosts");
            }
        } catch (NoSuchMethodException | SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 

 

 

 

 

 

 

 

 

 

MariaBoardService.java

package board.beans;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class MariaBoardService {
    /**
     * Maria DB ์—ฐ๊ฒฐ
     * 
     * @return
     */
    private Connection getConnection() {
        Connection connection = null;
        String jdbcUrl = "jdbc:mariadb://127.0.0.1:3306/mysql"; // ๋งˆ๋ฆฌ์•„DB ์—ฐ๊ฒฐ URL
        String username = "root";
        String password = "1234";

        try {
            Class.forName("org.mariadb.jdbc.Driver"); // ๋งˆ๋ฆฌ์•„DB JDBC ๋“œ๋ผ์ด๋ฒ„ ํด๋ž˜์Šค
            connection = DriverManager.getConnection(jdbcUrl, username, password);
            if (connection != null) {
                System.out.println("์—ฐ๊ฒฐ ์„ฑ๊ณต");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }



    /**
     * ๊ฒŒ์‹œ๊ธ€ ์ƒ์„ธ ๋ณด๊ธฐ
     * 
     * @param id
     * @return
     */
    public BoardPost getBoardPost(long id) {
        Connection connection = getConnection();
        if (connection == null) {
            return null;
        }
        BoardPost boardpost = null;
        String sql = "SELECT a.id, a.subject, a.content, a.createDate, b.id AS creatorId, b.name AS creatorName, b.fullName AS creatorFullName "
                        + " FROM boardpost a, user2 b " + "WHERE a.id LIKE " + id + " AND a.creatorId=b.id; ";
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            pstmt = connection.prepareStatement(sql);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                User creator = new User();
                long id2 = rs.getLong(1);
                String postSubject = rs.getString(2);
                String content = rs.getString(3);
                Timestamp dateString = rs.getTimestamp(4);
                // creator.setId(resultSet.getString("creatorId"));

                creator.setId(rs.getLong(5));
                creator.setName(rs.getString(6));
                creator.setFullName(rs.getString(7));

                boardpost = new BoardPost(id2, postSubject, content, creator, dateString);
            } else {
                System.out.println("๊ฒ€์ƒ‰ํ•ญ๋ชฉ ์—†์Œ");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null)
                    connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (pstmt != null)
                    pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (rs != null)
                    rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return boardpost;
    }

    /**
     * ๊ฒŒ์‹œ๊ธ€ ์ „์ฒด๋ณด๊ธฐ
     * 
     * @param subject
     * @return
     */
    public List<BoardPost> getBoardPostAll(String subject) {
        List<BoardPost> boardPosts = new ArrayList<BoardPost>();
        String query = "";

        // subject์— ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ๊ฒฐ๊ณผ๊ฐ’ ๋ณด์—ฌ์ฃผ๊ธฐ / ์•„๋‹ˆ๋ผ๋ฉด ์ „์ฒด ๋ชฉ๋ก ๋ณด์—ฌ์ฃผ๊ธฐ
        if (subject == null) {
            query = "SELECT a.id, a.subject, a.content, a.createDate, b.id AS creatorId, b.name AS creatorName, b.fullName AS creatorFullName "
                            + " FROM boardpost a, user2 b " + " WHERE a.creatorId = b.id ";
        } else {
            query = "SELECT a.id, a.subject, a.content, a.createDate, b.id AS creatorId, b.name AS creatorName, b.fullName AS creatorFullName "
                            + " FROM boardpost a, user2 b " + " WHERE a.subject like '%" + subject + "%' and a.creatorId = b.id ";
        }

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            connection = getConnection();
            statement = connection.prepareStatement(query);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                User creator = new User();
                long id = resultSet.getLong(1);
                String postSubject = resultSet.getString(2);
                String content = resultSet.getString(3);
                Timestamp dateString = resultSet.getTimestamp(4);

                creator.setId(resultSet.getLong(5));
                creator.setName(resultSet.getString(6));
                creator.setFullName(resultSet.getString(7));


                boardPosts.add(new BoardPost(id, postSubject, content, creator, dateString));

            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null)
                    connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (statement != null)
                    statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (resultSet != null)
                    resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return boardPosts;
    }


    /**
     * boardpost ๊ฐ์ฒด ์ƒ์„ฑ
     * 
     * @param request
     * @param user
     * @return
     */
    public BoardPost newBoardPost(String title, String content, String creatorId) {
        BoardPost boardPost = new BoardPost();
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());

        boardPost.setSubject(title);
        boardPost.setContent(content);
        boardPost.setCreateDate(timestamp);
        boardPost.setCreator(new User(Long.valueOf(creatorId), null, null, null, null, 0, null));
        System.out.println(boardPost);
        return boardPost;
    }

    /**
     * ๊ฒŒ์‹œ๊ธ€ ์ž‘์„ฑํ•˜๊ธฐ
     * 
     * @param boardPost
     * @return
     */
    public BoardPost createBoardPost(BoardPost boardPost) {
        Connection connection = getConnection();

        if (connection == null) {
            return null;
        }

        String insertQuery = "INSERT INTO boardpost (subject, content, creatorId, createDate) VALUES(?, ?, ?, ?)";
        String selectQuery = "select * from boardpost where id = LAST_INSERT_ID()";

        PreparedStatement pstmt = null;
        PreparedStatement selectedStatement = null;
        ResultSet rs = null;

        try {
            pstmt = connection.prepareStatement(insertQuery);
            pstmt.setString(1, boardPost.getSubject());
            pstmt.setString(2, boardPost.getContent());
            pstmt.setLong(3, boardPost.getCreator().getId());
            pstmt.setTimestamp(4, boardPost.getCreateDate());

            int num = pstmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null)
                    connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (pstmt != null)
                    pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (selectedStatement != null)
                    selectedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (rs != null)
                    rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

        return boardPost;
    }

}

 

 

 

 

 

 

 

 

 

 

 

MariaUserService.java

package board.beans;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MariaUserService {
    /**
     * Maria DB ์—ฐ๊ฒฐ
     * 
     * @return
     */
    private Connection getConnection() {
        Connection connection = null;
        String jdbcUrl = "jdbc:mariadb://127.0.0.1:3306/mysql"; // ๋งˆ๋ฆฌ์•„DB ์—ฐ๊ฒฐ URL
        String username = "root";
        String password = "1234";

        try {
            Class.forName("org.mariadb.jdbc.Driver"); // ๋งˆ๋ฆฌ์•„DB JDBC ๋“œ๋ผ์ด๋ฒ„ ํด๋ž˜์Šค
            connection = DriverManager.getConnection(jdbcUrl, username, password);
            if (connection != null) {
                System.out.println("์—ฐ๊ฒฐ ์„ฑ๊ณต");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * ๋กœ๊ทธ์ธ ํ•˜๊ธฐ
     * 
     * @param request
     * @return
     */
    public User getUser(String userName, String userPassword) {

        Connection connection = getConnection();
        String query = "select * from user2 where name like '" + userName + "'";

        User user = new User();
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            pstmt = connection.prepareStatement(query);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                String password = rs.getString(4);

                if (userPassword.equals(password)) {
                    System.out.println("๋น„๋ฐ€๋ฒˆํ˜ธ ์ผ์น˜ " + password);
                    user.setId(rs.getInt(1));
                    user.setName(rs.getString(2));
                    user.setFullName(rs.getString(3));
                    user.setPassword(rs.getString(4));
                    user.setEmail(rs.getString(5));
                    user.setAge(rs.getInt(6));
                    user.setCreateDate(rs.getTimestamp(7));
                }
            } else {
                user = null;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return user;
    }
}

 

 

 

 

 

 

 

 

 

 

 

User.java

package board.beans;

import java.sql.Timestamp;

/**
 * ์‚ฌ์šฉ์ž
 */
public class User {
    /** ์‚ฌ์šฉ์ž ๊ณ ์œ  ID */
    private long id;
    /** ์˜์–ด ์ด๋ฆ„ */
    private String name;
    /** ํ•œ๊ตญ์–ด ์ด๋ฆ„ */
    private String fullName;
    /** ๋น„๋ฐ€๋ฒˆํ˜ธ */
    private String password;
    /** ์ด๋ฉ”์ผ */
    private String email;
    /** ๋‚˜์ด */
    private int age;
    /** ๊ฐ€์ž… ๋‚ ์งœ */
    private Timestamp createDate;

    /**
     * ์ƒ์„ฑ์ž
     */
    public User() {
        // Do nothing
    }

    /**
     * ์ƒ์„ฑ์ž
     * 
     * @param name
     * @param age
     */
    public User(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    /**
     * ์ƒ์„ฑ์ž
     * 
     * @param id
     * @param name
     * @param fullName
     * @param password
     * @param email
     * @param age
     * @param createDate
     */
    public User(long id, String name, String fullName, String password, String email, int age, Timestamp createDate) {
        super();
        this.id = id;
        this.name = name;
        this.fullName = fullName;
        this.password = password;
        this.email = email;
        this.age = age;
        this.createDate = createDate;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Timestamp getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Timestamp createDate) {
        this.createDate = createDate;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", fullName=" + fullName + ", password=" + password + ", email=" + email
                        + ", age=" + age + ", createDate=" + createDate + "]";
    }

}

 

 

 

 

 

 

 

 

 

 

 

 

viewBoardDetail.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>



<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<c:if test="${boardpost ne null }">
	<h3>${boardpost.id }์ƒ์„ธ๋ณด๊ธฐ</h3>
	<table>
		<tr>
			<td>ID</td>
			<td>${boardpost.id }</td>
		</tr>
		<tr>
			<td>SUBJECT</td>
			<td>${boardpost.subject }</td>
		</tr>
		<tr>
			<td>CONTENT</td>
			<td>${boardpost.content }</td>
		</tr>
		<tr>
			<td>NAME</td>
			<td>${boardpost.creator.fullName }</td>
		</tr>
		<tr>
			<td>DATE</td>
			<td>${boardpost.createDate }</td>
		</tr>
	</table>
</c:if>
</head>
<body>

</body>
</html>

 

 

 

 

 

 

 

 

 

 

viewBoardPosts.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
table, th, tr, td {
	border: 1px solid black;
}

table {
	width: 80%;
	border-collapse: collapse;
}
</style>
</head>
<body>
	${user }
	<form name="searchForm" action="viewBoardPosts">
		<input type="text" name="subject" /> <input type="button" value="๊ฒ€์ƒ‰"
			onclick="searchBoardPosts()" />
	</form>
	<br />

	<table>
		<tr>
			<th>ID</th>
			<th>์ œ๋ชฉ</th>
			<th>๋‚ด์šฉ</th>
			<th>์ž‘์„ฑ์ž</th>
			<th>์ž‘์„ฑ์ผ</th>
		</tr>
		<c:forEach var="item" items="${boardPosts }">
			<tr>
				<td>${item.id }</td>
				<td><a href="viewBoardDetail?id=${item.id }">${item.subject }</a></td>
				<td>${item.content }</td>
				<td>${item.creator.fullName }</td>
				<td>${fn:substring(item.createDate, 0, 10)}</td>
			</tr>
		</c:forEach>

	</table>
	<br />

	<a href="viewCreateBoardPost">๊ฒŒ์‹œ๋ฌผ ์ž‘์„ฑ</a>

	<script>
		function searchBoardPosts() {
			document.searchForm.submit();
		}
	</script>
</body>
</html>

 

 

 

 

 

 

 

 

 

 

viewCreateBoardPost.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
.f {
	margin-top: 10px;
	margin-bottom: 10px;
}

input {
	width: 200px;
}

textarea {
	width: 200px;
}
</style>
</head>
${user }
<form name="mainForm" method="post" action="createBoardPost">
	<div class="f">
		์ œ๋ชฉ <input type="text" name="title" placeholder="์ œ๋ชฉ ์ž…๋ ฅ" />
	</div>
	<div class="f">
		๋‚ด์šฉ
		<textarea name="content"></textarea>
	</div>
	<c:if test="${user ne null}">
		<input type="hidden" name="creatorId" value="${user.id }">
	</c:if>
	<div class="f">
		์ œ๋ชฉ <input type="button" value="๋“ฑ๋ก" onclick="createBoardPost()">
	</div>
</form>

<script>
	function createBoardPost() {
		let title = document.mainForm.title.value;
		let content = document.mainForm.content.value;

		if (title == null || title == "") {
			alert("์ œ๋ชฉ์„ ์ž…๋ ฅํ•˜์„ธ์š”");
		} else if (content == null || content == "") {
			alert("๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜์„ธ์š”");
		} else {
			document.mainForm.submit();
		}
	}
</script>
</body>
</html>

 

 

 

 

 

 

 

 

 

 

viewLogin.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form name="mainForm" method="post" action="login">
		<div>
			userName <input type="text" name="userName" />
		</div>
		<div>
			userPassword <input type="text" name="userPassword" />
		</div>
		<div>
			<input type="button" onclick="login()" value="๋กœ๊ทธ์ธ">
		</div>
	</form>

	<script>
		function login() {
			let id = document.mainForm.userName.value;
			let password = document.mainForm.userPassword.value;

			if (id == null || id == "") {
				alert("userName์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”");
			} else if (password == null || password == "") {
				alert("userPassword๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”");
			} else {
				document.mainForm.submit();
			}
		}
	</script>
</body>
</html>

 

 

 

 

 

 

 

 

 

 

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="board/BoardServlet/viewLogin">๋กœ๊ทธ์ธ</a>
</body>
</html>

 

 

 

 

 

 

 

 

 

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1">
	<display-name>123007</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>

	<jsp-config>
		<taglib>
			<taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
			<taglib-location>/WEB-INF/lib/c.tld</taglib-location>
		</taglib>
	</jsp-config>

	<servlet>
		<servlet-name>BoardServlet1</servlet-name>
		<servlet-class>board.beans.BoardServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>BoardServlet1</servlet-name>
		<url-pattern>/servlet/BoardServlet/*</url-pattern>
	</servlet-mapping>

	<servlet>
		<servlet-name>MultiplicationServlet</servlet-name>
		<servlet-class>multiplication.MultiplicationServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>MultiplicationServlet</servlet-name>
		<url-pattern>/servlet/MultiplicationServlet</url-pattern>
	</servlet-mapping>
</web-app>
๋ฐ˜์‘ํ˜•