⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 guestbookapp.java

📁 java 写的一个新闻发布系统
💻 JAVA
字号:
package components.guestbook;import java.io.*;						// IOExceptionimport java.util.*;                     // Hashtableimport javax.servlet.*;                 // ServletConfigimport javax.servlet.http.*;            // HttpServletRequest, HttpServletResponseimport org.jahia.services.applications.*;    // JahiaApplicationimport org.jahia.params.*;          // ParamBeanimport org.jahia.utils.*;import org.jahia.exceptions.*;public class GuestBookApp extends HttpServlet {    private String version = "1.5";    private static final String ENTRIES_PATH    = File.separator + "guestbook" + File.separator + "data" + File.separator;    private static final String ENTRIES_FILE    = "entries.txt";    private static final String TEMPLATE_PATH   = File.separator + "guestbook" + File.separator + "templates" + File.separator;    private static final String TEMPLATE_FILE   = "std.html";    /***        * doGet        * EV    10.12.2000        *        */    public void doGet( HttpServletRequest request, HttpServletResponse response ) throws IOException    {        PrintWriter out = response.getWriter();        //System.out.println( "GuestBook App ver" + version + " responding to " + request.getRemoteAddr() + " GET METHOD ");        ParamBean jParams = (ParamBean) request.getAttribute( "org.jahia.params" );        if (jParams == null)        return;        checkEntriesFile( jParams );        if (request.getParameter("gb_flush") != null) {            //System.out.println("Guestbook. Flushing entries...");            flushEntries( request, jParams );        }        out.println(drawForm( request, response, jParams ) );    } // end doGet    /***        * doPost        * EV    10.12.2000        *        */    public void doPost( HttpServletRequest request, HttpServletResponse response ) throws IOException    {        //System.out.println( "GuestBook App ver" + version + " responding to " + request.getRemoteAddr() + " POST METHOD ");        BufferedReader requestReader = null;        try {            requestReader = request.getReader();        } catch (IOException ioe) {        //System.out.println("Guestbook : IOException while trying to recuperate input stream");        } catch (IllegalStateException ise) {            //System.out.println("Guestbook : IllegalStateException means InputStream has already been called on the request body");        }            if (requestReader == null) {            //System.out.println("Guestbook : ERROR RETRIEVING INPUT STREAM !");        } else {            try {                requestReader.reset();        } catch (IOException ioe) {            //System.out.println("Guestbook : IOException while resetting input stream");        }        try {                //System.out.println("Guestbook : REQUEST BODY START ----- ");            int nc = requestReader.read();            while (nc != -1) {                    //System.out.print((char)nc);                nc = requestReader.read();            }                //System.out.println("Guestbook : REQUEST BODY END ----- ");        } catch (IOException ioe) {            //System.out.println("Guestbook : IOException while reading body of request");        }        }        ParamBean jParams = (ParamBean) request.getAttribute( "org.jahia.params" );        if (jParams == null)        return;        saveEntry( request, jParams );        doGet( request, response );    } // end doPost    /***        * flushEntries        * EV    11.12.2000        *        */    public void flushEntries( HttpServletRequest request, ParamBean jParams )    {        if (request.getParameter("gb_flush").equals("true")) {            //System.out.println( "Flushing !!!! \n" ) ;            String fileName = jParams.settings().getComponentsDiskPath() + ENTRIES_PATH + ENTRIES_FILE;            try {                FileUtils.getInstance().writeFile( fileName, " " );            } catch (Exception e) {                //System.out.println( "GuestBook App > Error in trying to flush data file : " + e );            }        }    } // end flushEntries    /***        * checkEntriesFile        * EV    11.12.2000        *        */    public void checkEntriesFile( ParamBean jParams )    {        String filePath = jParams.settings().getComponentsDiskPath() + ENTRIES_PATH;        String fileName = jParams.settings().getComponentsDiskPath() + ENTRIES_PATH + ENTRIES_FILE;        File entriesPath = new File( filePath );        if (!entriesPath.exists()) {            entriesPath.mkdirs();        }        File entriesFile = new File( fileName );        if (!entriesFile.exists()) {            try {                FileUtils.getInstance().writeFile( fileName, " " );            } catch (Exception e) {                //System.out.println( "GuestBook App > Error in trying to create data file : " + e );            }        }    } // end    /***        * saveEntry        * EV    10.12.2000        *        */    public void saveEntry( HttpServletRequest request, ParamBean jParams )    {        String contents = "";        String filePath = jParams.settings().getComponentsDiskPath() + ENTRIES_PATH + ENTRIES_FILE;        try {            contents = FileUtils.getInstance().readFile( filePath );        } catch (Exception e) {            //System.out.println( "GuestBook App > Error occured : " + e );        }        String comment = request.getParameter( "gb_comment" );        String author  = request.getParameter( "gb_author" );        String line = "<table border=\"0\" width=\"100%\">\n";        line += "<tr><td width=\"100%\"><font face=\"arial\" size=\"2\" color=\"white\">\n";        line += comment + "\n";        line += "</font></td></tr>\n";        line += "<tr><td align=\"right\">\n";        line += "<font face=\"arial\" size=\"1\" color=\"white\"><i>" + author + "</i></font>\n";        line += "</td></tr>\n";        line += "<tr><td><hr noshade></td></tr></table>\n";        contents += line;        try {            FileUtils.getInstance().writeFile( filePath, contents );        } catch (Exception e) {            //System.out.println( "GuestBook App > Error occured : " + e );        }    } // end saveEntry    /***        * loadEntries        * EV    10.12.2000        *        */    public String loadEntries( HttpServletRequest request, ParamBean jParams )    {        String contents = "";        String filePath = jParams.settings().getComponentsDiskPath() + ENTRIES_PATH + ENTRIES_FILE;        try {            contents = FileUtils.getInstance().readFile( filePath );        } catch (Exception e) {            //System.out.println( "GuestBook App > Error occured : " + e );            contents = "- Error in reading data file -";        }        return contents;    } // end loadEntries    /***        * drawForm        * EV    10.12.2000        *        */    public String drawForm( HttpServletRequest request,                    HttpServletResponse response,                    ParamBean jParams )    {            String html = "";            String entries = loadEntries( request, jParams );            String commentField = "<textarea name=\"gb_comment\" rows=\"3\" cols=\"25\" wrap=\"soft\"></textarea>\n";            String authorField = "<input type=\"text\" name=\"gb_author\" size=\"20\" maxlength=\"200\">\n";            String submitField = "<input type=\"submit\" value=\"Add Entry\">\n";            String filePath = jParams.settings().getComponentsDiskPath() + TEMPLATE_PATH + TEMPLATE_FILE;            String actionURL;            if (request.getContextPath().equals("/")) {                actionURL = request.getServletPath();            } else {            actionURL = request.getContextPath() + request.getServletPath();            }            String flushField = "";            if (jParams.getOperationMode() == jParams.EDIT) {                int matrix = (int)(Math.random() * 10000);                flushField += "<input type=\"button\" name=\"flush\" value=\"Flush Entries\" ";                flushField += "onClick=\"location.href='" + response.encodeURL(actionURL + "?gb_flush=true&matrix=" + matrix ) + "'\">\n";            }            Hashtable tokens = new Hashtable();            tokens.put( "entries", entries );            tokens.put( "commentField", commentField );            tokens.put( "authorField", authorField );            tokens.put( "submitField", submitField );            tokens.put( "flushField", flushField );            html = "<form method=\"POST\" action=\"" + response.encodeURL(actionURL) + "\">\n";            html += composeSource(  filePath, tokens );            html += "</form>\n";            return html;    } // end drawForm    /***        * composeSource        * EV    10.12.2000        *        */    public String composeSource( String filePath, Hashtable tokens )    {        String contents = "";        try {            contents = FileUtils.getInstance().readFile( filePath );        } catch (Exception e) {            //System.out.println( "GuestBookApp > Error occured : " + e );            contents = "- Error in reading template file - ";            return contents;        }        int pos = 0;        String theKey = "";        Enumeration keys = tokens.keys();        while (keys.hasMoreElements()) {            theKey = (String) keys.nextElement();            pos = contents.indexOf( "<!--" + theKey + "-->" );            if (pos > -1) {                contents = contents.substring( 0, pos )                            + tokens.get(theKey)                            + contents.substring( pos, contents.length() );            }        }        return contents;    } // end composeSource} // end GuestBookApp

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -