//*/*/*/*/*/*/*/*/*/*/*/*/*/*/ // URLs for important websites //*/*/*/*/*/*/*/*/*/*/*/*/*/*/ // Original servlet: // http://novocode.de/doc/servlet-essentials/chapter2b.html // file:///C|/apache/Tomcat/webapps/examples/WEB-INF/classes/ListManagerServlet.html // Homepage of my Apache Tomcat Server (for download and documentation): // http://jakarta.apache.org/tomcat/ //*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/ // Shortcuts for compiling and testing //*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/ // Directory for compilation: // C:\apache\Tomcat\webapps\examples\WEB-INF\classes // Compile: // javac Bookmarks.java -classpath "C:\Programme\Java 2\jdk1.3\servlet.jar" // Run: // http://localhost:8080/examples/servlet/Bookmarks // http://notevom.zhwin.ch:8080/examples/servlet/Bookmarks // Homepage of my Apache Tomcat server (for testing: // http://notevom.zhwin.ch:8080/index.html //*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/ // Code for servlet "Bookmarks" //*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/ import java.util.Vector; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Bookmarks extends HttpServlet { private Vector addresses; private String filename; private String seperator = ","; public void init(ServletConfig config) throws ServletException { super.init(config); // location of file: C:\WINNT\system32 filename = "bookmarks.txt"; // comented code out and replaced with code above // filename = config.getInitParameter("Bookmarks"); if(filename == null) throw new UnavailableException(this, "The \"addressfile\" property "+ "must be set to a file name"); try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename)); addresses = (Vector)in.readObject(); in.close(); } catch(FileNotFoundException e) { addresses = new Vector(); } catch(Exception e) { throw new UnavailableException(this, "Error reading address file: "+e); } } protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); res.setHeader("pragma", "no-cache"); PrintWriter out = res.getWriter(); out.print("Bookmark Manager"); // reload to "delete" cached deleted Links, commented code out -> found better solution! // out.print(""); out.print(""); out.print("

Hyperlinks:


"); // begin of modified code out.print("Enter URL:

"); out.print("Enter Title:

"); out.print("Enter Type:

"); out.print("Enter User:


"); // end of modified code out.print(""); out.print(""); out.print("
"); out.close(); } protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String url = req.getParameter("url") + seperator + req.getParameter("title") + seperator + req.getParameter("type") + seperator + req.getParameter("user"); String msg; if(url == null) { res.sendError(res.SC_BAD_REQUEST, "No url specified."); return; } if(req.getParameter("action").equals("subscribe")) { if(subscribe(url)) msg = "Address " + url + " has been subscribed."; else { res.sendError(res.SC_BAD_REQUEST, "Address " + url + " was already subscribed."); return; } } else { if(unsubscribe(url)) msg = "Address " + url + " has been removed."; else { res.sendError(res.SC_BAD_REQUEST, "Address " + url + " was not subscribed."); return; } } res.setContentType("text/html"); res.setHeader("pragma", "no-cache"); PrintWriter out = res.getWriter(); out.print("Bookmarks Manager"); // out.print(""); out.print(""); // begin of modified code out.print("Thank you for your help!
"); out.print(msg); out.print("
Close the window.   "); out.print("Show the servlet list."); // end of modified code out.print(""); out.close(); } public String getServletInfo() { return "Bookmarks 1.0 by Martin Voegeli"; } private synchronized boolean subscribe(String url) throws IOException { if(addresses.contains(url)) return false; addresses.addElement(url); save(); return true; } private synchronized boolean unsubscribe(String url) throws IOException { if(!addresses.removeElement(url)) return false; save(); return true; } private void save() throws IOException { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename)); out.writeObject(addresses); out.close(); } // begin of added code // code source split(): // http://www.codeproject.com/useritems/string_splitter1.asp public String[] split(String str,char x){ Vector v=new Vector(); String str1=new String(); for(int i=0;i