[404]不能从doGet()转到doPost()(在board_write)

时间:2017-07-18 01:44:12

标签: java jsp servlets

enter image description here

我想连接'boardWrite.jsp' 单击该彩色(写)按钮时。 (使用命令) 其他功能表现不错,但这只是出错了。

错误信息是404这样的

enter image description here

当我尝试调试时,我发现它无法从下面的BoardServlet.java从doGet()转到doPost()

这是我的代码......

由于我是新手,我的问题看起来很荒谬。 但这对我来说很重要..那就是......我希望我能在这里得到帮助。 我正在努力......

 ***1. boardlist.jsp***

    `<body>
      <div id="wrap" align="center">
        <h1>게시글 리스트</h1>
        <table class="list">
          <tr>
            <td colspan=5 style="border:white; text-align:right">
              <a href = "BoardServlet?command=board_write_form">게시글등록</a>
            </td>
          </tr>
          <tr>
            <th>번호</th>
            <th>제목</th>
            <th>작성자</th>
            <th>작성일</th>
            <th>조회</th>

          </tr>


            <c:forEach var="board" items="${boardList}"> 
           <tr class = "record">
             <td>${board.num}</td>
             <td><a href="BoardServlet?command=board_view&num=${board.num}">${board.title}</a></td>
             <td>${board.name}</td>
             <td><fmt:formatDate value="${board.writedate}" /></td>
             <td>${board.readcount}</td>
           </tr> 


            </c:forEach>

        </table>


      </div>`

    2. BoardServlet.java 
package com.board.controller;

import java.io.IOException;
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 com.board.controller.action.Action;

/**
 * Servlet implementation class BoardServlet
 */
@WebServlet("/BoardServlet")
public class BoardServlet extends HttpServlet {     //컨트롤러 클래스
    private static final long serialVersionUID = 1L;

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

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String command = request.getParameter("command");
        System.out.println("BoardServlet에서 요청을 받음을 확인 :"+command);
        ActionFactory af = ActionFactory.getInstance();
        Action action = af.getAction(command);
        if(action!=null){
            action.execute(request, response);

        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("UTF-8");
        doGet(request, response);
    }

}


    ***2.ActionFactory.java***

    `package com.board.controller;

    import com.board.controller.action.Action;
    import com.board.controller.action.BoardListAction;
    import com.board.controller.action.BoardViewAction;
    import com.board.controller.action.BoardWriteAction;
    import com.board.controller.action.BoardWriteFormAction;

    public class ActionFactory {
        private static ActionFactory instance = new ActionFactory();

        private ActionFactory(){
            super();
        }

        public static ActionFactory getInstance(){
            return instance; 
        }

        // 요청된 명령어별로 분기 처리하는 메서드 

        public Action getAction(String command){
            Action action = null;
            System.out.println("ActionFactory:"+command);

            if(command.equals("board_list")){
                action = new BoardListAction();  // 객체를 생성했다는 말은 => 해당 객체의 클래스를 찾아간다는 의미로 받아들여도 된다.
            }else if(command.equals("board_view")){
                action = new BoardViewAction();
            }else if(command.equals("board_write_form")){
                action = new BoardWriteFormAction();
            }else if(command.equals("board_write")){
                action = new BoardWriteAction();
            }
            return action;
        }

    }`

    3. BoardDAO.java

       ' public class BoardDAO {

        private BoardDAO(){}

        private static BoardDAO instance = new BoardDAO();

        public static BoardDAO getInstance(){
            return instance;
        }

        public List<BoardVO> selectAllBoard(){
            String sql = "select * from board order by num desc";
            List<BoardVO> list = new ArrayList<BoardVO>();
            Connection conn = null;
            Statement stmt = null; 
            ResultSet rs = null; 

            try{
                conn=DBManager.getConnection();
                stmt = conn.createStatement();
                rs = stmt.executeQuery(sql);

                while(rs.next()){
                    BoardVO bVo = new BoardVO();
                    bVo.setNum(rs.getInt("num"));
                    bVo.setTitle(rs.getString("title"));
                    bVo.setName(rs.getString("name"));
                    bVo.setPass(rs.getString("pass"));
                    bVo.setContent(rs.getString("content"));
                    bVo.setReadcount(rs.getInt("readcount"));
                    bVo.setWritedate(rs.getTimestamp("writedate"));
                    list.add(bVo);
                }

            }catch(SQLException e){
                e.printStackTrace();
            }finally{
                DBManager.close(rs, stmt, conn);
            }

            return list;
        } 

        // 글 내용의 readcount 값 증가 

        public void updateReadCount(String num){
            String sql = "update board set readcount=readcount+1 where num=?";
            Connection conn = null;
            PreparedStatement pstmt = null; 

            try{
                conn = DBManager.getConnection();
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, num);
                pstmt.executeUpdate();


            }catch(SQLException e){
                e.printStackTrace();
            }finally{
                DBManager.close(pstmt, conn);
            }
        }

        // 게시판 글 상세 내용 보기 

        public BoardVO selectBoardByNum(String num){
            String sql = "select * from board where num=?";
            BoardVO bVo = null;
            Connection conn = null;
            PreparedStatement pstmt = null;
            ResultSet rs = null;

            try{ 
                 conn = DBManager.getConnection();
                 pstmt = conn.prepareStatement(sql);
                 pstmt.setString(1, num);
                 rs = pstmt.executeQuery();

                 if(rs.next()){
                     bVo = new BoardVO();

                     bVo.setNum(rs.getInt("num"));
                     bVo.setName(rs.getString("name"));
                     bVo.setTitle(rs.getString("title"));
                     bVo.setPass(rs.getString("pass"));
                     bVo.setEmail(rs.getString("email"));
                     bVo.setTitle(rs.getString("title"));
                     bVo.setContent(rs.getString("content")); 
                     bVo.setWritedate(rs.getTimestamp("writedate"));
                     bVo.setReadcount(rs.getInt("readCount"));
                 }

            }catch(Exception e){
                e.printStackTrace();
            }finally{
                DBManager.close(rs, pstmt, conn);
            }
            return bVo;
        }

        //게시글 등록 
        public void insertBoard(BoardVO bVo){
            String sql = "insert into board("
                    + "num, name, email, pass, title, content )"
                    + "values(board_seq.nextVal, ?, ?, ?, ?, ?)";
            Connection conn = null;
            PreparedStatement pstmt = null;

            try{
                conn = DBManager.getConnection();
                pstmt = conn.prepareStatement(sql);

                pstmt.setString(1, bVo.getName());
                pstmt.setString(2, bVo.getEmail());
                pstmt.setString(3, bVo.getPass());
                pstmt.setString(4, bVo.getTitle());
                pstmt.setString(5, bVo.getContent());
                pstmt.executeUpdate();

            }catch(SQLException ex){
                ex.printStackTrace();
            }finally{
                DBManager.close(pstmt, conn);
            }
        }


    }'

    ***4. BoardWriteFormAction.java***

    public class BoardWriteFormAction implements Action {

        @Override
        public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub

            String url = "\board\boardWrite.jsp";
            RequestDispatcher dispatcher = request.getRequestDispatcher(url);
            dispatcher.forward(request, response);
        }

    }

1 个答案:

答案 0 :(得分:-1)

由于您收到404错误,我怀疑您的servlet未正确映射到您的web.xml中。检查一下。如果您不知道这意味着什么,那么Google将如何在Tomcat上为servlet或JSP应用程序设置web.xml。简而言之,这就是将请求URL映射到servlet类的原因。如果你还没有这样做,那么服务器就不知道&#34; BoardServlet&#34;是

另外,您没有说,但是您的servlet打印输出到Catalina控制台的doGet方法中是否通过System.out.printl发送了日志语句?如果是,那么您就知道正在调用您的servlet。如果没有,那么我再次建议您确保您的servlet在web.xml中正确映射。

最后,您应该使用java.util.logging(或其他日志记录解决方案)来记录服务器上的代码。虽然System.out.printl可以在Tomcat中运行并写入控制台,但当您的服务器不是您的开发机器时,它将无法工作。