본문 바로가기

컴퓨터/jsp

예제

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
        <title>게시판 글쓰기</title>
    </head>
    <body>
        <h2>글쓰기</h2>
        <form action=BBSPost.jsp method=POST></form>
        이름: <input type=text NAME=NAME><BR>
        제목: <input type=text NAME=TITLE><br>
        <textarea rows=5 cols=30 NAME=CONTENT></textarea><BR>
        <input type=submit VALUE='저장'>
    </body>
</html> 

 

JSP

BBSPost

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@page import="java.io.*, java.util.Date" %>
<%
    request.setCharacterEncoding("euc-kr");
    String name = request.getParameter("NAME");
    String title = request.getParameter("TITLE");
    String content = request.getParameter("CONTENT");
    Date date = new Date();
    long time = date.getTime();
    String filename = time + ".txt";
    String result;
    PrintWriter writer = null;
    try{
        String filePath = application.getRealPath("/WEB-INF/BBS/" + filename);
        writer = new PrintWriter(filePath);
        writer.printf("제목: %s \n", title);
        writer.printf("글쓴이: %s \n", name);
        writer.println(content);
        result = "SUCCESS";
    }
    catch (IOException ioe){
        result = "FAIL";
    }
    finally{
        try{
            writer.close();
        }
        catch (Exception e){
        }
    }
    response.sendRedirect("BBSPostResult.jsp?RESULT=" + result);
%>

 

 

BBSPostResult

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
        <title>게시판 글쓰기 - 결과화면</title>
    </head>
    <body>
        <h2> 글쓰기</h2>
        <%
            String str = request.getParameter("RESULT");
        if(str.equals("SUCCESS"))
            out.println("저장되었습니다.");
        else
            out.println("파일에 데이터를 쓸 수 없습니다.");
        %>
    </body>
</html>

'컴퓨터 > jsp' 카테고리의 다른 글

예외처리  (0) 2013.07.06
버퍼  (0) 2013.07.06
Encoding  (0) 2013.07.06
개인정보 저장  (0) 2013.07.06
servlet  (0) 2013.07.06