⑤ 게시판에 글 작성 하기
<button><a href = "${pageContext.request.contextPath }/board/insert">글작성</a></button>
▶ servlet (/board/insert) 작성
package board.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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;
import board.Boardservice;
import board.Boardvo;
/**
* Servlet implementation class insert
*/
@WebServlet("/board/insert")
public class insert extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public insert() {
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
request.setCharacterEncoding("euc-kr");
response.setCharacterEncoding("euc-kr");
response.setContentType("text/html; charset = EUC-KR");
RequestDispatcher dis = request.getRequestDispatcher("/board/add.jsp");
dis.forward(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("euc-kr");
response.setCharacterEncoding("euc-kr");
response.setContentType("text/html; charset = EUC-KR");
HttpSession session = request.getSession();
String writer = (String) session.getAttribute("loginId");
String title = request.getParameter("title");
String content = request.getParameter("content");
Boardservice service = new Boardservice();
service.addboard(new Boardvo (0,null,writer,title,content));
response.sendRedirect(request.getContextPath() + "/board/boardmain");
}
}
글 작성 버튼을 클릭하면 하기 이미지와 같은 곳으로 이동한다. 주소창에서 볼 수 있는 것 처럼 해당페이지는 board/insert servlet doGet 을 통해 작성 폼 형식을 전달 받았다.
▶ jsp 작성
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action = "${pageContext.request.contextPath }/board/insert" method = "post" >
<table border = "1">
<tr><th>작성자</th><td><input type = "text" value = "${sessionScope.loginId }" readonly></td></tr>
<tr><th>제목</th><td><input type = "text" name = "title"></td></tr>
<tr><th>내용</th><td><input type = "text" name = "content"></td></tr>
<tr><th>등록</th><td><input type = "submit" name = "등록"></td></tr>
</table>
</form>
</body>
</html>
● <input type = "text" value = "${sessionScope.loginId }" readonly>
작성자의 value 를 "${sessionScope.loginId }" 로 주어 자동으로 로그인 한 사람의 아이디 정보가 붙게 만들었다.
'MVC > MVC 모델 실습' 카테고리의 다른 글
[MVC 실습] 로그인하고 게시판 운영하기_6 (0) | 2023.04.03 |
---|---|
[MVC 실습] 로그인하고 게시판 운영하기_4 (0) | 2023.04.03 |
[MVC 실습] 로그인하고 게시판 운영하기_3 (0) | 2023.04.03 |
[MVC 실습] 로그인하고 게시판 운영하기_2 (0) | 2023.04.03 |
[MVC 실습] 로그인하고 게시판 운영하기_1 (0) | 2023.04.03 |