Implementations

Sign Up

/bbs/signUp_proc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="net.java_school.user.*" %>    
<%
request.setCharacterEncoding("UTF-8");

String email = request.getParameter("email");
String passwd = request.getParameter("passwd");
String name = request.getParameter("name");
String mobile = request.getParameter("mobile");

User user = new User(email, passwd, name, mobile);

UserService service = new UserService();
service.addUser(user);

response.sendRedirect("welcome.jsp");
%>

Visit http://localhost:8080/users/signUp.jsp, and click the submit button. If you see the welcome page, connect to SQL*PLUS and check your data in the member table. If you cannot see the welcome page, check the log messages in the WEB-INF/debug.log file. For Linux systems, you need to set permissions like chmod o+w debug.log. Tomcat also writes log messages to files in the CATALINA_HOME/logs directory. If the test succeeds, remove default text field values in SignUp.jsp.

login_proc.jsp

/users/login_proc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="net.java_school.user.User" %>
<%@ page import="net.java_school.user.UserService" %>
<%
request.setCharacterEncoding("UTF-8");

String url = request.getParameter("url");
String email = request.getParameter("email");
String passwd = request.getParameter("passwd");

UserService service = new UserService();
User user = service.login(email, passwd);

if (user == null) {
  //if login failed, redirect to login.jsp
  response.sendRedirect("login.jsp?url=" + url + "&msg=Login-Failed");
} else {
  session.setAttribute("user", user);
  if (url != null && !url.equals("")) {
    response.sendRedirect(url);
  } else {
    response.sendRedirect("../bbs/list.jsp?boardCd=smalltalk&curPage=1");
  }
}
%>

Add the following code snippet to the login page.

login.jsp
<h2>Login</h2>
<%
String msg = request.getParameter("msg");

if (msg != null) {
%>
  <p style="color: red;">Login Failed!</p>
<%
}
%>

Create a class to store static strings used in the project.

WebContants.java
package net.java_school.commons;

public class WebContants {
  //Session key
  public final static String USER_KEY = "user";
}

"user" is the key value of the User object to put in the session. Open login_proc.jsp and change "user" to WebContants.USER_KEY.

Modify Account

Add the following string to WebContants class.

public final static String NOT_LOGIN = "Not Login";
/users/editAccount_proc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="net.java_school.user.User" %>
<%@ page import="net.java_school.user.UserService" %>
<%@ page import="net.java_school.commons.WebContants" %>
<%

User user = (User) session.getAttribute(WebContants.USER_KEY);
if (user == null) {
  response.sendError(HttpServletResponse.SC_FORBIDDEN, WebContants.NOT_LOGIN);
  return;
}

request.setCharacterEncoding("UTF-8");

String name = request.getParameter("name");
String mobile = request.getParameter("mobile");
String passwd = request.getParameter("passwd");

String email = user.getEmail();

UserService service = new UserService();

user = service.login(email, passwd);

if (user == null) {
  response.sendError(HttpServletResponse.SC_FORBIDDEN, WebContants.AUTHENTICATION_FAILED);
  session.removeAttribute(WebContants.USER_KEY);
  return;
}

user = new User(email, passwd, name, mobile);

service.editAccount(user);
session.setAttribute(WebContants.USER_KEY, user);

response.sendRedirect("changePasswd.jsp");
%>

Change Password

/users/changePasswd_proc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="net.java_school.user.User" %>
<%@ page import="net.java_school.user.UserService" %>
<%@ page import="net.java_school.commons.WebContants" %>
<%

User user = (User) session.getAttribute(WebContants.USER_KEY);
if (user == null) {
  response.sendError(HttpServletResponse.SC_FORBIDDEN, WebContants.NOT_LOGIN);
  return; 
}

String currentPasswd = request.getParameter("currentPasswd");
String newPasswd = request.getParameter("newPasswd");
String email = user.getEmail();

UserService service = new UserService();
int check = service.changePasswd(currentPasswd, newPasswd, email);
if (check < 1) {
  response.sendError(HttpServletResponse.SC_FORBIDDEN, WebContants.AUTHENTICATION_FAILED);
  return; 
}

response.sendRedirect("changePasswd_confirm.jsp");
%>

Reomve Account

Add the following string to WebContants class.

public final static String AUTHENTICATION_FAILED = "Authentication Failed";
/users/bye_proc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="net.java_school.user.User" %>
<%@ page import="net.java_school.user.UserService" %>
<%@ page import="net.java_school.commons.WebContants" %>
<%

User user = (User) session.getAttribute(WebContants.USER_KEY);
if (user == null) {
  response.sendError(HttpServletResponse.SC_FORBIDDEN, WebContants.NOT_LOGIN); 
  return;
}

String email = request.getParameter("email");
String passwd = request.getParameter("passwd");

UserService service = new UserService();
User check = service.login(email, passwd);
if (check == null) {
  response.sendError(HttpServletResponse.SC_FORBIDDEN, WebContants.AUTHENTICATION_FAILED); 
  return;
}

service.bye(user.getEmail(), passwd);
session.removeAttribute(WebContants.USER_KEY);

response.sendRedirect("bye_confirm.jsp");
%>

Board List

/bbs/list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="net.java_school.board.*" %>
<%@ page import="net.java_school.commons.NumbersForPaging" %>
<%@ page import="java.util.ArrayList" %>
<%@ include file="../inc/loginCheck.jsp" %>
<%
request.setCharacterEncoding("UTF-8");

String boardCd = request.getParameter("boardCd");
int curPage = Integer.parseInt(request.getParameter("curPage"));
String searchWord = request.getParameter("searchWord");
if (searchWord == null) searchWord = "";
BoardService service = new BoardService();

int totalRecord = service.getTotalRecord(boardCd, searchWord);
int numPerPage = 10;
int pagePerBlock = 10;
int startRecord = (curPage - 1) * numPerPage;
int endRecord = curPage * numPerPage;
List<Article> articleList = service.getArticleList(boardCd, searchWord, startRecord, endRecord);
String boardName = service.getBoardName(boardCd);
NumbersForPaing numbers = service.getNumbersForPaging(totalRecord, curPage, numPerPage, pagePerBlock);
%>

Modify the part of list.jsp that shows the board name as follows.

<h1><%=boardName %></h1>

Modify the search form part as shown below.

<form action="list.jsp" method="get">
  <input type="hidden" name="curPage" value="1" />
  <input type="hidden" name="boardCd" value="<%=boardCd %>" />
  <div id="search">
    <input type="text" name="searchWord" size="15" maxlength="30" />
    <input type="submit" value="Search" />
  </div>
</form>

Modify the value of the input element attribute in the #form-group as shown below.

<div id="form-group" style="display: none">
  <form id="listForm" action="list.jsp" method="get">
    <input type="hidden" name="boardCd" value="<%=boardCd %>" />
    <input type="hidden" name="curPage" />
    <input type="hidden" name="searchWord" value="<%=searchWord %>" />
  </form>
  <form id="viewForm" action="view.jsp" method="get">
    <input type="hidden" name="articleNo" />
    <input type="hidden" name="boardCd" value="<%=boardCd %>" />
    <input type="hidden" name="curPage" value="<%=curPage %>" />
    <input type="hidden" name="searchWord" value="<%=searchWord %>" />
  </form>
  <form id="writeForm" action="write_form.jsp" method="get">
    <input type="hidden" name="boardCd" value="<%=boardCd %>" />
    <input type="hidden" name="curPage" value="<%=curPage %>" />
    <input type="hidden" name="searchWord" value="<%=searchWord %>" />
  </form>
</div>

Implement paging processing logic as follows.

<%
int listItemNo = numbers.getListItemNo();
int size = articleList.size();
for (int i = 0; i < size; i++) {
  Article article = articleList.get(i);
  int articleNo = article.getArticleNo();
  String title = article.getTitle();
  java.util.Date regdate = article.getRegdate();
  int attachFileNum = article.getAttachFileNum();
  int commentNum = article.getCommentNum();
  int hit = article.getHit();
%>           
  <tr>
    <td style="text-align: center;"><%=listItemNo %></td>
    <td>
      <a href="javascript:goView('<%=articleNo %>')"><%=title %></a>
      <% if (attachFileNum > 0) { %><img src="images/attach.png" alt="Attach File" style="vertical-align: middle;" /><% } %>
      <% if (commentNum > 0) { %><span class="bbs-strong">[<%=commentNum %>]</span><% } %>
    </td>
    <td style="text-align: center;"><%=regdate %></td>
    <td style="text-align: center;"><%=hit %></td>
  </tr>
<%
    listItemNo--;
} // for statement end
%>           
  </table>

  <div id="paging">
<%
if (numbers.getPrevPage() != 0) {
%>
    <a href="javascript:goList('<%=numbers.getPrevPage() %>')">[Prev]</a>
<%
} // if statement end

int firstPage = numbers.getFirstPage();
int lastPage = numbers.getLastPage();
for (int i = firstPage; i <= lastPage; i++) {
  if (curPage == i) {
%>
    <span class="bbs-strong"><%=i %></span>       
<%
  } else {
%>   
    <a href="javascript:goList('<%=i %>')"><%=i %></a>      
<%
  }
}// for statement end

// [Next]
if (numbers.getNextPage() != 0) {
%>
  <a href="javascript:goList('<%=numbers.getNextPage() %>')">[Next]</a>
<%
}// if statement end
%>
  </div>

New Article form page

/bbs/write_form.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" %>
<%@ page import="net.java_school.board.*" %>
<%@ page import="java.util.*" %>
<%@ include file="../inc/loginCheck.jsp" %>
<%
request.setCharacterEncoding("UTF-8");

String articleNo = request.getParameter("articleNo");
String boardCd = request.getParameter("boardCd");
String curPage = request.getParameter("curPage");
String searchWord = request.getParameter("searchWord");

BoardService service = new BoardService();
String boardName = service.getBoardName(boardCd);
%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="Keywords" content="New Article form page" />
<meta name="Description" content="New Article form page" />
<title><%=boardName %></title>
<link rel="stylesheet" href="/css/screen.css" type="text/css" />
<script type="text/javascript">
function goList() {
  var form = document.getElementById("listForm");
  form.submit();
}
function goView() {
  var form = document.getElementById("viewForm");
  form.submit();
}
</script>           
</head>
<body>

<div id="wrap">

  <div id="header">
    <%@ include file="../inc/header.jsp" %>
  </div>

  <div id="main-menu">
    <%@ include file="../inc/main-menu.jsp" %>
  </div>

  <div id="container">
    <div id="content">
        
      <div id="content-categories"><%=boardName %></div>
      <h3>New Article</h3>
      <form id="writeForm" action="write_proc.jsp" method="post" enctype="multipart/form-data">
      <input type="hidden" name="boardCd" value="<%=boardCd %>" />
      <table id="write-form" class="bbs-table">
      <tr>
        <td>Title</td>
        <td><input type="text" name="title" style="width: 90%;" /></td>
      </tr>
      <tr>
        <td colspan="2">
          <textarea name="content" rows="17" cols="50"></textarea>
        </td>
      </tr>
      <tr>
        <td>Attach File</td>
        <td><input type="file" name="attachFile" /></td>
      </tr>
      </table>
      <div style="text-align: center;padding-bottom: 15px;">
        <input type="submit" value="Submit" />
        <input type="button" value="List" onclick="goList()" />
<% 
if (articleNo != null) {
%>   
        <input type="button" value="Detailed view" onclick="goView()" />
<%
} //if statement end
%>  
      </div>
      </form>

    </div>
  </div>
    
  <div id="sidebar">
    <%@ include file="bbs-sub.jsp" %>
  </div>
    
  <div id="extra">
    <%@ include file="../inc/extra.jsp" %>
  </div>

  <div id="footer">
    <%@ include file="../inc/footer.jsp" %>
  </div>

</div>

<div id="form-group" style="display: none">
  <form id="viewForm" action="view.jsp" method="get">
    <input type="hidden" name="articleNo" value="<%=articleNo %>" />
    <input type="hidden" name="boardCd" value="<%=boardCd %>" />
    <input type="hidden" name="curPage" value="<%=curPage %>" />
    <input type="hidden" name="searchWord" value="<%=searchWord %>" />
  </form>
  <form id="listForm" action="list.jsp" method="get">
    <input type="hidden" name="boardCd" value="<%=boardCd %>" />
    <input type="hidden" name="curPage" value="<%=curPage %>" />
    <input type="hidden" name="searchWord" value="<%=searchWord %>" />
  </form>
</div>

</body>
</html>

write_proc.jsp

Download http://www.servlets.com/cos/cos-26Dec2008.zip and unzip it. Copy cos.jar to WEB-INF/lib of your web application. Create a folder named upload on the document base. If your system is Unix-like, you need to set permissions on the upload folder.

/bbs/write_proc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" %>
<%@ page import="java.io.*" %>
<%@ page import="net.java_school.commons.WebContants" %>
<%@ page import="net.java_school.user.User" %>
<%@ page import="net.java_school.user.UserService" %>
<%@ page import="net.java_school.board.*" %>
<%@ page import="com.oreilly.servlet.MultipartRequest" %>
<%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %>
<%
User user = (User) session.getAttribute(WebContants.USER_KEY);
if (user == null) {
  response.sendError(HttpServletResponse.SC_FORBIDDEN, WebContants.NOT_LOGIN);
  return;
}

String dir = application.getRealPath("/upload");

MultipartRequest multi = new MultipartRequest(
    request,
    dir,
    5*1024*1024,
    "UTF-8",
    new DefaultFileRenamePolicy());

String title = multi.getParameter("title");
String content = multi.getParameter("content");
String filename = multi.getFilesystemName("attachFile");
String filetype = multi.getContentType("attachFile");

File f = multi.getFile("attachFile");
long filesize = 0L;
AttachFile attachFile = null;

if (f != null) {
  filesize = f.length();
  attachFile = new AttachFile();
  attachFile.setFilename(filename);
  attachFile.setFiletype(filetype);
  attachFile.setFilesize(filesize);
  attachFile.setEmail(user.getEmail());
}

String boardCd = multi.getParameter("boardCd");

Article article = new Article();
article.setEmail(user.getEmail());
article.setTitle(title);
article.setContent(content);
article.setBoardCd(boardCd);

BoardService service = new BoardService();
service.addArticle(article, attachFile);

response.sendRedirect("list.jsp?boardCd=" + boardCd + "&curPage=1");
%>

view.jsp

/bbs/view.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="net.java_school.user.*" %>
<%@ page import="net.java_school.board.*" %>
<%@ page import="net.java_school.commons.*" %>
<%@ include file="../inc/loginCheck.jsp" %>
<%
request.setCharacterEncoding("UTF-8");

int articleNo = Integer.parseInt(request.getParameter("articleNo"));
String boardCd = request.getParameter("boardCd");
int curPage = Integer.parseInt(request.getParameter("curPage"));
String searchWord = request.getParameter("searchWord");
if (searchWord == null) searchWord = "";

BoardService service = new BoardService();

int totalRecord = service.getTotalRecord(boardCd, searchWord);
int numPerPage = 10;
int pagePerBlock = 10;
int startRecord = (curPage - 1) * numPerPage + 1;
int endRecord = curPage * numPerPage;

service.increaseHit(articleNo);

Article detailedArticle = service.getArticle(articleNo);
List<AttachFile> attachFileList = service.getAttachFileList(articleNo);
Article nextArticle = service.getNextArticle(articleNo, boardCd, searchWord);
Article prevArticle = service.getPrevArticle(articleNo, boardCd, searchWord);
List<Article> articleList = service.getArticleList(boardCd, searchWord, startRecord, endRecord); 
List<Comment> commentList = service.getCommentList(articleNo);

String boardName = service.getBoardName(boardCd);

String articleOwnerName = detailedArticle.getName();
%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="Keywords" content="Detailed view page" />
<meta name="Description" content="Detailed view page" />
<title><%=boardName %></title>
<link rel="stylesheet" href="/css/screen.css" type="text/css" />
<script type="text/javascript">
function modifyCommentToggle(articleNo) {
  var p_id = "comment" + articleNo;
  var form_id = "modifyCommentForm" + no;
  var p = document.getElementById(p_id);
  var form = document.getElementById(form_id);
    
  var p_display;
  var form_display;
    
  if (p.style.display) {
    p_display = '';
    form_display = 'none';
  } else {
    p_display = 'none';
    form_display = '';
  }
    
  p.style.display = p_display;
  form.style.display = form_display;
}
function goList(curPage) {
  var form = document.getElementById("listForm");
  form.curPage.value = curPage;
  form.submit();
}
function goView(articleNo) {
  var form = document.getElementById("viewForm");
  form.articleNo.value = articleNo;
  form.submit();
}
function goWrite() {
  var form = document.getElementById("writeForm");
  form.submit();
}
function goModify() {
  var form = document.getElementById("modifyForm");
  form.submit();
}
function goDelete() {
  var check = confirm("Are you sure you want to delete it?");
  if (check) {
    var form = document.getElementById("delForm");
    form.submit();
  }
}
function deleteAttachFile(attachFileNo) {
  var check = confirm("Are you sure you want to delete it?");
  if (check) {
    var form = document.getElementById("deleteAttachFileForm");
    form.attachFileNo.value = attachFileNo;
    form.submit();
  }
}
function deleteComment(commentNo) {
  var check = confirm("Are you sure you want to delete it?");
  if (check) {
    var form = document.getElementById("deleteCommentForm");
    form.commentNo.value = commentNo;
    form.submit();
  }
}
</script>
</head>
<body>

<div id="wrap">

  <div id="header">
    <%@ include file="../inc/header.jsp" %>
  </div>

  <div id="main-menu">
    <%@ include file="../inc/main-menu.jsp" %>
  </div>

  <div id="container">
    <div id="content">
        
<div id="content-categories"><%=boardName %></div>
<div class="view-menu" style="margin-top: 15px;margin-bottom: 5px;">
  <div class="fl">
<%
if (user.getEmail().equals(detailedArticle.getEmail())) {
%>      
    <input type="button" value="Modify" onclick="goModify();" />
    <input type="button" value="Del" onclick="goDelete()" />
<%
}
%>
  </div>
  <div class="fr">
<%
if (nextArticle != null) {
%>       
    <input type="button" value="Next Article" onclick="goView('<%=nextArticle.getArticleNo() %>')" />
<%
}
if (prevArticle != null) {
%>           
    <input type="button" value="Prev Article" onclick="goView('<%=prevArticle.getArticleNo() %>')" />
<%
}
%>           
    <input type="button" value="List" onclick="goList('<%=curPage %>')" />
    <input type="button" value="New" onclick="goWrite()" />
  </div>
</div>
<table class="bbs-table">
<tr>
  <th style="width: 47px;text-align: left;vertical-align: top;font-size: 1em;">TITLE</th>
  <th style="text-align: left;color: #555;font-size: 1em;"><%=detailedArticle.getTitle() %></th>
</tr> 
</table>

<div id="detail">
  <div id="date-writer-hit">edited <%=detailedArticle.getRegdate() %> by <%=articleOwnerName %> hit <%=detailedArticle.getHit() %></div>
<%
String content = detailedArticle.getContent();
content = content.replaceAll(System.getProperty("line.separator"), "<br />");
%>
  <div id="article-content"><%=content %></div>
<%       
int size = attachFileList.size();
if (size > 0) {
%>
  <div id="file-list" style="text-align: right">
<%
  String path = request.getContextPath();
  String uploadPath = path + "/upload/";
    
  for (int i = 0; i < size; i++) {
    AttachFile attachFile = attachFileList.get(i);
    String filename = attachFile.getFilename();
    int attachFileNo = attachFile.getAttachFileNo();
    String fileFullPath = uploadPath + filename;
%>
    <div id="attach-file">		
      <a href="<%=fileFullPath %>"><%=filename %></a>
<% 
    if (user.getEmail().equals(detailedArticle.getEmail())) { 
%>
      <a href="javascript:deleteAttachFile('<%=attachFileNo %>')">x</a>
<%
    }//if statement end
%>
    </div>
<%       
  }//for statement end
%>       
  </div>
<%
}//if statement end
%>      
</div>
<!-- Add comment -->
<form id="addCommentForm" action="addComment.jsp" method="post">
  <input type="hidden" name="articleNo" value="<%=articleNo %>" />
  <input type="hidden" name="boardCd" value="<%=boardCd %>" />
  <input type="hidden" name="curPage" value="<%=curPage %>" />
  <input type="hidden" name="searchWord" value="<%=searchWord %>" />
  <div id="addComment">
    <textarea name="memo" rows="7" cols="50"></textarea>
  </div>
  <div style="text-align: right;">
    <input type="submit" value="Add comment" />
  </div>
</form>
<%
size = commentList.size();
for (int i = 0; i < size; i++ ) {
  Comment comment = commentList.get(i);
  int commentNo = comment.getCommentNo();
  String email = comment.getEmail();
  String memo = comment.getMemo();
  String commentOwnerName = comment.getName();
  Date regdate = comment.getRegdate();
%>
<div class="comments">
  <span class="writer"><%=commentOwnerName %></span>
  <span class="date"><%=regdate %></span>
<%
  if (user.getEmail().equals(email)) {
%>   
  <span class="modify-del">
    <a href="javascript:modifyCommentToggle('<%=commentNo %>')">Modify</a>
    | <a href="javascript:deleteComment('<%=commentNo %>')">Del</a>
  </span>
<%
  }//if statement end
%>   
  <p id="comment<%=commentNo %>"><%=memo %></p>
  <form id="modifyCommentForm<%=commentNo %>" class="comment-form" action="updateComment_proc.jsp" method="post" style="display: none;">
    <input type="hidden" name="commentNo" value="<%=commentNo %>" />
    <input type="hidden" name="boardCd" value="<%=boardCd %>" />
    <input type="hidden" name="articleNo" value="<%=articleNo %>" />
    <input type="hidden" name="curPage" value="<%=curPage %>" />
    <input type="hidden" name="searchWord" value="<%=searchWord %>" />
    <div class="fr">
      <a href="javascript:document.forms.modifyCommentForm<%=commentNo %>.submit()">Submit</a>
      | <a href="javascript:goCommentModify('<%=commentNo %>')">Cancel</a>
    </div>
    <div>
      <textarea class="comment-textarea" name="memo" rows="7" cols="50"><%=memo %></textarea>
    </div>
  </form>
</div>
<%
}//for statement end
%>

<div id="next-prev">
<%
if (nextArticle != null) {
%>
  <p>Next Article : <a href="javascript:goView('<%=nextArticle.getArticleNo() %>')"><%=nextArticle.getTitle() %></a></p>
<%
} 
if (prevArticle != null) {
%>       
  <p>Prev Article : <a href="javascript:goView('<%=prevArticle.getArticleNo() %>')"><%=prevArticle.getTitle() %></a></p>
<%
}
%>       
</div>
<div class="view-menu" style="margin-bottom: 47px;">
  <div class="fl">
<%
if (user.getEmail().equals(detailedArticle.getEmail())) {
%>      
    <input type="button" value="Modify" onclick="goModify();" />
    <input type="button" value="Del" onclick="goDelete()" />
<%
}
%>
  </div>
        
  <div class="fr">
<%
if (nextArticle != null) {
%>       
    <input type="button" value="Next Article" onclick="goView('<%=nextArticle.getArticleNo() %>')" />
<%
}
if (prevArticle != null) {
%>           
    <input type="button" value="Prev Article" onclick="goView('<%=prevArticle.getArticleNo() %>')" />
<%
}
%>           
    <input type="button" value="List" onclick="goList('<%=curPage %>')" />
    <input type="button" value="New" onclick="goWrite()" />
  </div>
</div>
    
<!-- List -->
<table>
<tr>
  <th style="width: 60px">NO</th>
  <th>TITLE</th>
  <th style="width: 84px;">DATE</th>
  <th style="width: 60px;">HIT</th>
</tr>
<%
int listItemNo = service.getListItemNo();
size = articleList.size();
for (int i = 0; i < size; i++) {
  Article article = articleList.get(i);
  int aNo = article.getArticleNo();
  String title = article.getTitle();
  Date regdate = article.getRegdate();
  int hit = article.getHit();
  int attachFileNum = article.getAttachFileNum();
  int commentNum = article.getCommentNum();
%>  
<tr>
  <td style="text-align: center;">
<%
if (articleNo == aNo) {
%>
  <img src="../images/arrow.gif" alt="You are reading this" />
<%
} else {
%>
  <%=listItemNo %>
<%
}
%>
  </td>
  <td>
    <a href="javascript:goView('<%=aNo %>')"><%=title %></a>
<%
if (attachFileNum > 0) { 
%>
    <img src="../images/attach.png" alt="Attach File" style="vertical-align: middle;" />
<%
}
if (commentNum > 0 ) {
%>           
    <span class="bbs-strong">[<%=commentNum %>]</span>
<%
}
%>          
  </td>
  <td style="text-align: center;"><%=regdate %></td>
  <td style="text-align: center;"><%=hit %></td>
</tr>
<%
  listItemNo--;
}//for statement end
%>

</table>

<div id="paging">
<%
if (service.getPrevPage() != 0 ) {
%>
  <a href="javascript:goList('<%=service.getPrevPage() %>')">[Prev]</a>
<%
}
int firstPage = service.getFirstPage();
int lastPage = service.getLastPage();

for (int i = firstPage; i <= lastPage; i++) {
  if (curPage == i) {
%>
  <span class="bbs-strong"><%=i %></span>
<%
  } else {
%>   
    <a href="javascript:goList('<%=i %>')"><%=i %></a>        
<%
  }
}//for statement end

if (service.getNextPage() != 0) {
%>
  <a href="javascript:goList('<%=service.getNextPage() %>')">[Next]</a>
<%
}
%>
</div>

<div id="list-menu">
  <input type="button" value="New" onclick="goWrite()" />
</div>

<form action="list.jsp" method="get">
<input type="hidden" name="curPage" value="1" />
<input type="hidden" name="boardCd" value="<%=boardCd %>" />
<div id="search">
  <input type="text" name="searchWord" size="15" maxlength="30" />
  <input type="submit" value="Search" />
</div>
</form>

    </div><!-- content end -->
  </div><!--  container end -->
    
  <div id="sidebar">
    <%@ include file="bbs-sub.jsp" %>
  </div>
    
  <div id="extra">
    <%@ include file="../inc/extra.jsp" %>
  </div>

  <div id="footer">
    <%@ include file="../inc/footer.jsp" %>
  </div>

</div>

<div id="form-group" style="display: none">
  <form id="listForm" action="list.jsp" method="get">
    <input type="hidden" name="boardCd" value="<%=boardCd %>" />
    <input type="hidden" name="curPage" />
    <input type="hidden" name="searchWord" value="<%=searchWord %>" />
  </form>
  <form id="viewForm" action="view.jsp" method="get">
    <input type="hidden" name="articleNo" />
    <input type="hidden" name="boardCd" value="<%=boardCd %>" />
    <input type="hidden" name="curPage" value="<%=curPage %>" />
    <input type="hidden" name="searchWord" value="<%=searchWord %>" />
  </form>
  <form id="writeForm" action="write_form.jsp" method="get">
    <input type="hidden" name="articleNo" value="<%=articleNo %>" />
    <input type="hidden" name="boardCd" value="<%=boardCd %>" />
    <input type="hidden" name="curPage" value="<%=curPage %>" />
    <input type="hidden" name="searchWord" value="<%=searchWord %>" />
  </form>
  <form id="modifyForm" action="modify_form.jsp" method="get">
    <input type="hidden" name="articleNo" value="<%=articleNo %>" />
    <input type="hidden" name="boardCd" value="<%=boardCd %>" />
    <input type="hidden" name="curPage" value="<%=curPage %>" />
    <input type="hidden" name="searchWord" value="<%=searchWord %>" />
  </form>
  <form id="delForm" action="del_proc.jsp" method="post">
    <input type="hidden" name="articleNo" value="<%=articleNo %>" />
    <input type="hidden" name="boardCd" value="<%=boardCd %>" />
    <input type="hidden" name="curPage" value="<%=curPage %>" />
    <input type="hidden" name="searchWord" value="<%=searchWord %>" />
  </form>
  <form id="deleteCommentForm" action="deleteComment_proc.jsp" method="post">
    <input type="hidden" name="commentNo" />
    <input type="hidden" name="articleNo" value="<%=articleNo %>" />
    <input type="hidden" name="boardCd" value="<%=boardCd %>" />
    <input type="hidden" name="curPage" value="<%=curPage %>" />
    <input type="hidden" name="searchWord" value="<%=searchWord %>" />
  </form>   
  <form id="deleteAttachFileForm" action="deleteAttachFile.jsp" method="post">
    <input type="hidden" name="attachFileNo" />
    <input type="hidden" name="articleNo" value="<%=articleNo %>" />
    <input type="hidden" name="boardCd" value="<%=boardCd %>" />
    <input type="hidden" name="curPage" value="<%=curPage %>" />
    <input type="hidden" name="searchWord" value="<%=searchWord %>" />
  </form>   
</div>

</body>
</html>

addComment_proc.jsp

/bbs/addComment_proc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="net.java_school.commons.WebContants" %>
<%@ page import="net.java_school.user.User" %>
<%@ page import="net.java_school.board.*" %>
<%
User user = (User) session.getAttribute(WebContants.USER_KEY);
if (user == null) {
  response.sendError(HttpServletResponse.SC_FORBIDDEN, WebContants.NOT_LOGIN);
  return;
}
request.setCharacterEncoding("UTF-8");

String boardCd = request.getParameter("boardCd");
int articleNo = Integer.parseInt(request.getParameter("articleNo"));
int curPage = Integer.parseInt(request.getParameter("curPage"));
String searchWord = request.getParameter("searchWord");
String memo = request.getParameter("memo");

Comment comment = new Comment();
comment.setArticleNo(articleNo);
comment.setEmail(user.getEmail());
comment.setMemo(memo);

BoardService service = new BoardService();
service.addComment(comment);

searchWord = java.net.URLEncoder.encode(searchWord, "UTF-8");
response.sendRedirect("view.jsp?articleNo=" + articleNo + "&boardCd=" + boardCd + "&curPage=" + curPage + "&searchWord=" + searchWord);
%>

updateComment_proc.jsp

/bbs/updateComment_proc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="net.java_school.commons.WebContants" %>
<%@ page import="net.java_school.user.User" %>
<%@ page import="net.java_school.board.*" %>
<%
User user = (User) session.getAttribute(WebContants.USER_KEY);

request.setCharacterEncoding("UTF-8");
int commentNo = Integer.parseInt(request.getParameter("commentNo"));

BoardService service = new BoardService();
Comment comment = service.getComment(commentNo);
if (user == null || !user.getEmail().equals(comment.getEmail())) {
  response.sendError(HttpServletResponse.SC_FORBIDDEN, WebContants.AUTHENTICATION_FAILED);
  return; 
}

String boardCd = request.getParameter("boardCd");
int articleNo = Integer.parseInt(request.getParameter("articleNo"));
int curPage = Integer.parseInt(request.getParameter("curPage"));
String searchWord = request.getParameter("searchWord");

String memo = request.getParameter("memo");

comment.setCommentNo(commentNo);
comment.setArticleNo(articleNo);
comment.setEmail(user.getEmail());
comment.setMemo(memo);

service.modifyComment(comment);

searchWord = java.net.URLEncoder.encode(searchWord,"UTF-8");
response.sendRedirect("view.jsp?articleNo=" + articleNo + "&boardCd=" + boardCd + "&curPage=" + curPage + "&searchWord=" + searchWord);
%>

deleteComment_proc.jsp

/bbs/deleteComment_proc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="net.java_school.commons.WebContants" %>
<%@ page import="net.java_school.user.User" %>
<%@ page import="net.java_school.board.*" %>
<%
User user = (User) session.getAttribute(WebContants.USER_KEY);

request.setCharacterEncoding("UTF-8");
int commentNo = Integer.parseInt(request.getParameter("commentNo"));

BoardService service = new BoardService();
Comment comment = service.getComment(commentNo);
if (user == null || !user.getEmail().equals(comment.getEmail())) {
  response.sendError(HttpServletResponse.SC_FORBIDDEN, WebContants.AUTHENTICATION_FAILED);
  return; 
}

String boardCd = request.getParameter("boardCd");
int articleNo = Integer.parseInt(request.getParameter("articleNo"));
int curPage = Integer.parseInt(request.getParameter("curPage"));
String searchWord = request.getParameter("searchWord");

service.removeComment(commentNo);

searchWord = java.net.URLEncoder.encode(searchWord, "UTF-8");
response.sendRedirect("view.jsp?articleNo=" + articleNo + "&boardCd=" + boardCd + "&curPage=" + curPage + "&searchWord=" + searchWord);
%>

deleteAttachFile_proc.jsp

/bbs/deleteAttachFile_proc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="net.java_school.user.User" %>
<%@ page import="net.java_school.board.*" %>
<%@ page import="net.java_school.commons.WebContants" %>
<%
User user = (User) session.getAttribute(WebContants.USER_KEY);

request.setCharacterEncoding("UTF-8");
int attachFileNo = Integer.parseInt(request.getParameter("attachFileNo"));

BoardService service = new BoardService();
AttachFile attachFile = service.getAttachFile(attachFileNo);
if (user == null || !user.getEmail().equals(attachFile.getEmail())) {
  response.sendError(HttpServletResponse.SC_FORBIDDEN, WebContants.AUTHENTICATION_FAILED);
  return; 
}

String boardCd = request.getParameter("boardCd");
int articleNo = Integer.parseInt(request.getParameter("articleNo"));
int curPage = Integer.parseInt(request.getParameter("curPage"));
String searchWord = request.getParameter("searchWord");

service.removeAttachFile(attachFileNo);

searchWord = java.net.URLEncoder.encode(searchWord, "UTF-8");
response.sendRedirect("view.jsp?articleNo=" + articleNo + "&boardCd=" + boardCd + "&curPage=" + curPage + "&searchWord=" + searchWord);
%>

modify_form.jsp

/bbs/modify_form.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" %>
<%@ page import="net.java_school.commons.WebContants" %>
<%@ page import="net.java_school.board.*" %>
<%@ page import="java.util.*" %>
<%
request.setCharacterEncoding("UTF-8");

int articleNo = Integer.parseInt(request.getParameter("articleNo"));
String boardCd = request.getParameter("boardCd");
int curPage = Integer.parseInt(request.getParameter("curPage"));
String searchWord = request.getParameter("searchWord");

User user = (User) session.getAttribute("user");
BoardService service = new BoardService();
Article article = service.getArticle(articleNo);

if (user == null || !user.getEmail().equals(article.getEmail())) {
  response.sendError(HttpServletResponse.SC_FORBIDDEN, WebContants.AUTHENTICATION_FAILED);
  return;
}

String title = article.getTitle();
String content = article.getContent();
if (content == null) content = "";

String boardName = service.getBoardName(boardCd);
%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="Keywords" content="Board article modify form page" />
<meta name="Description" content="Board article modify form page" />
<title><%=boardName %></title>
<link rel="stylesheet" href="/css/screen.css" type="text/css"  />
<script type="text/javascript">
function goView() {
  var form = document.getElementById("viewForm");
  form.submit();
}
</script>           
</head>
<body>

<div id="wrap">

  <div id="header">
    <%@ include file="../inc/header.jsp" %>
  </div>

  <div id="main-menu">
    <%@ include file="../inc/main-menu.jsp" %>
  </div>

  <div id="container">
    <div id="content">
        
<div id="content-categories"><%=boardName %></div>
<h3>Modify</h3>
<form id="modifyForm" action="modify_proc.jsp" method="post" enctype="multipart/form-data">
<input type="hidden" name="articleNo" value="<%=articleNo %>" />
<input type="hidden" name="boardCd" value="<%=boardCd %>" />
<input type="hidden" name="curPage" value="<%=curPage %>" />
<input type="hidden" name="searchWord" value="<%=searchWord %>" />
<table id="write-form" class="bbs-table">
<tr>
  <td>Title</td>
  <td><input type="text" name="title" style="width: 90%;" value="<%=title %>" /></td>
</tr>
<tr>
  <td colspan="2">
    <textarea name="content" rows="17" cols="50"><%=content %></textarea>
  </td>
</tr>
<tr>
  <td>Attach File</td>
  <td><input type="file" name="attachFile" /></td>
</tr>
</table>
<div style="text-align: center;padding-bottom: 15px;">
  <input type="submit" value="Submit" />
  <input type="button" value="Detailed view" onclick="goView()" />
</div>
</form>

    </div><!-- content end -->
  </div><!--  container end -->
    
  <div id="sidebar">
    <%@ include file="bbs-sub.jsp" %>
  </div>
    
  <div id="extra">
    <%@ include file="../inc/extra.jsp" %>
  </div>

  <div id="footer">
    <%@ include file="../inc/footer.jsp" %>
  </div>

</div>

<div id="form-group" style="display: none;">
  <form id="viewForm" action="view.jsp" method="get">
    <input type="hidden" name="articleNo" value="<%=articleNo %>" />
    <input type="hidden" name="boardCd" value="<%=boardCd %>" />
    <input type="hidden" name="curPage" value="<%=curPage %>" />
    <input type="hidden" name="searchWord" value="<%=searchWord %>" />
  </form>
</div>

</body>
</html>

modify_proc.jsp

/bbs/modify_proc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*" %>
<%@ page import="net.java_school.commons.WebContants" %>
<%@ page import="net.java_school.user.User" %>
<%@ page import="net.java_school.user.UserService" %>
<%@ page import="net.java_school.board.*" %>
<%@ page import="com.oreilly.servlet.MultipartRequest" %>
<%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %>
<%
User user = (User) session.getAttribute(WebContants.USER_KEY);

String dir = application.getRealPath("/upload");

MultipartRequest multi = new MultipartRequest(
    request,
    dir,
    5*1024*1024,
    "UTF-8",
    new DefaultFileRenamePolicy());

BoardService service = new BoardService();
int articleNo = Integer.parseInt(multi.getParameter("articleNo"));

if (!service.getArticle(articleNo).getEmail().equals(user.getEmail())) {
  response.sendError(HttpServletResponse.SC_FORBIDDEN, WebContants.AUTHENTICATION_FAILED);
  return;
}

String boardCd = multi.getParameter("boardCd");
int curPage = Integer.parseInt(multi.getParameter("curPage"));
String searchWord = multi.getParameter("searchWord");
searchWord = java.net.URLEncoder.encode(searchWord, "UTF-8");

String title = multi.getParameter("title");
String content = multi.getParameter("content");
String filename = multi.getFilesystemName("attachFile");
String filetype = multi.getContentType("attachFile");

File f = multi.getFile("attachFile");
long filesize = 0L;
AttachFile attachFile = null;

if (f != null) {
  filesize = f.length();
  attachFile = new AttachFile();
  attachFile.setFilename(filename);
  attachFile.setFiletype(filetype);
  attachFile.setFilesize(filesize);
  attachFile.setEmail(user.getEmail());
  attachFile.setArticleNo(articleNo);
}

Article article = new Article();
article.setArticleNo(articleNo);
article.setBoardCd(boardCd);
article.setTitle(title);
article.setContent(content);
article.setEmail(user.getEmail());

service.modifyArticle(article, attachFile);

response.sendRedirect("view.jsp?articleNo=" + articleNo + "&boardCd=" + boardCd + "&curPage=" + curPage + "&searchWord=" + searchWord);
%>

del_proc.jsp

/bbs/del_proc.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="net.java_school.commons.WebContants" %>
<%@ page import="net.java_school.user.User" %>
<%@ page import="net.java_school.board.*" %>
<%
User user = (User) session.getAttribute(WebContants.USER_KEY);

request.setCharacterEncoding("UTF-8");

int articleNo = Integer.parseInt(request.getParameter("articleNo"));

BoardService service = new BoardService();
Article article = service.getArticle(articleNo);
if (user == null || !user.getEmail().equals(article.getEmail())) {
  response.sendError(HttpServletResponse.SC_FORBIDDEN, WebContants.AUTHENTICATION_FAILED);
  return;
}

service.removeArticle(articleNo);

String boardCd = request.getParameter("boardCd");
String curPage = request.getParameter("curPage");
String searchWord = request.getParameter("searchWord");

searchWord = java.net.URLEncoder.encode(searchWord, "UTF-8");
response.sendRedirect("list.jsp?boardCd=" + boardCd + "&curPage=" + curPage + "&searchWord=" + searchWord);
%>