File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Please try to execute this??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Please try to execute this???" Watch "Please try to execute this???" New topic
Author

Please try to execute this???

S Varana
Greenhorn

Joined: Feb 04, 2004
Posts: 18
import java.util.Vector;
2: import java.io.*;
3: import javax.servlet.*;
4: import javax.servlet.http.*;
5:
6: public class ListManagerServlet extends HttpServlet
7: {
8: private Vector addresses;
9: private String filename;
10:
11: public void init(ServletConfig config) throws ServletException
12: {
13: super.init(config);
14: filename = config.getInitParameter("addressfile");
15: if(filename == null)
16: throw new UnavailableException(this,
17: "The \"addressfile\" property "+
18: "must be set to a file name");
19: try
20: {
21: ObjectInputStream in =
22: new ObjectInputStream(new FileInputStream(filename));
23: addresses = (Vector)in.readObject();
24: in.close();
25: }
26: catch(FileNotFoundException e) { addresses = new Vector(); }
27: catch(Exception e)
28: {
29: throw new UnavailableException(this,
30: "Error reading address file: "+e);
31: }
32: }
33:
34: protected void doGet(HttpServletRequest req,
35: HttpServletResponse res)
36: throws ServletException, IOException
37: {
38: res.setContentType("text/html");
39: res.setHeader("pragma", "no-cache");
40: PrintWriter out = res.getWriter();
41: out.print("<HTML><HEAD><TITLE>List Manager</TITLE></HEAD>");
42: out.print("<BODY><H3>Members:</H3><UL>");
43: for(int i=0; i<addresses.size(); i++)
44: out.print("<LI>" + addresses.elementAt(i));
45: out.print("</UL><HR><FORM METHOD=POST>");
46: out.print("Enter your email address: <INPUT TYPE=TEXT NAME=email><BR>");
47: out.print("<INPUT TYPE=SUBMIT NAME=action VALUE=subscribe>");
48: out.print("<INPUT TYPE=SUBMIT NAME=action VALUE=unsubscribe>");
49: out.print("</FORM></BODY></HTML>");
50: out.close();
51: }
52:
53: protected void doPost(HttpServletRequest req,
54: HttpServletResponse res)
55: throws ServletException, IOException
56: {
57: String email = req.getParameter("email");
58: String msg;
59: if(email == null)
60: {
61: res.sendError(res.SC_BAD_REQUEST,
62: "No email address specified.");
63: return;
64: }
65: if(req.getParameter("action").equals("subscribe"))
66: {
67: if(subscribe(email))
68: msg = "Address " + email + " has been subscribed.";
69: else
70: {
71: res.sendError(res.SC_BAD_REQUEST,
72: "Address " + email + " was already subscribed.");
73: return;
74: }
75: }
76: else
77: {
78: if(unsubscribe(email))
79: msg = "Address " + email + " has been removed.";
80: else
81: {
82: res.sendError(res.SC_BAD_REQUEST,
83: "Address " + email + " was not subscribed.");
84: return;
85: }
86: }
87:
88: res.setContentType("text/html");
89: res.setHeader("pragma", "no-cache");
90: PrintWriter out = res.getWriter();
91: out.print("<HTML><HEAD><TITLE>List Manager</TITLE></HEAD><BODY>");
92: out.print(msg);
93: out.print("<HR><A HREF=\"");
94: out.print(req.getRequestURI());
95: out.print("\">Show the list</A></BODY></HTML>");
96: out.close();
97: }
98:
99: public String getServletInfo()
100: {
101: return "ListManagerServlet 1.0 by Stefan Zeiger";
102: }
103:
104: private synchronized boolean subscribe(String email) throws IOException
105: {
106: if(addresses.contains(email)) return false;
107: addresses.addElement(email);
108: save();
109: return true;
110: }
111:
112: private synchronized boolean unsubscribe(String email) throws IOException
113: {
114: if(!addresses.removeElement(email)) return false;
115: save();
116: return true;
117: }
118:
119: private void save() throws IOException
120: {
121: ObjectOutputStream out =
122: new ObjectOutputStream(new FileOutputStream(filename));
123: out.writeObject(addresses);
124: out.close();
125: }
126: }

I have taken this code from a website http://novocode.de/doc/servlet-essentials/chapter2b.html
but i get a problem when
I run this code at "addressfile".
I created a file named addressfile in my local disk and specified with the path , But it still throws unavaiable exeption
What should i do there? What exactly is this addressfile?
Help me and give ur suggestion
Praveen Raja
Greenhorn

Joined: May 01, 2000
Posts: 13
some one correct me if im wrong.
The addressfile is an initialisation parameter that must be specified in a servlet properties file
hanumanth reddy
Ranch Hand

Joined: Jun 12, 2000
Posts: 118
ur right praveen the initial paramete for the servlet must be specified in the properties file

<a href="http://www.jobklub.com" target="_blank" rel="nofollow">http://www.jobklub.com</a><br /> 'Add Job To Life'
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please try to execute this???