Hi,
This is an example of a counter servlet. The counter value is stored in a
property file. This example might not compile because I have cut off parts
that are not related to the main subject. But you get the idea.
Note that the content type is application-x/www-urlformencoded and the
counter value is sent back in "url" form. (This servlet is interfaced with a
Flash client.)
Pierre-Yves
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Counter extends HttpServlet {
private Hashtable table = new Hashtable();
public void doPost( HttpServletRequest requête,
HttpServletResponse réponse)
throws IOException, ServletException {
réponse.setContentType("application-x/www-urlformencoded");
PrintWriter pw = réponse.getWriter();
String path = getServletConfig().getServletContext().getRealPath("/");
String counterName = requête.getParameter("conterName");
.
.
.
Properties properties = new Properties();
int count = 0;
synchronized(this) {
try {
FileInputStream counterFile = new FileInputStream(path
+ counterName);
properties.load(counterFile);
counterFile.close();
}
catch (IOException ioe) {
}
try {
count =
Integer.parseInt(properties.getProperty("count"));
}
catch (Exception e) {
}
if (mode.equals("rw")) {
properties.setProperty("count", "" + (++count));
try {
FileOutputStream counterFile = new
FileOutputStream(path + counter);
properties.store(counterFile, null);
counterFile.close();
}
catch (IOException ioe) {
}
}
}
pw.print("counter=" + counter);
}
}
-----Message d'origine-----
De : A mailing list for discussion about Sun Microsystem's Java Servlet
API Technology. [mailto:[EMAIL PROTECTED]]De la part de
irene_plj asiamail.com
Envoyé : mardi 27 mars 2001 16:13
À : [EMAIL PROTECTED]
Objet : Auto-Genrated Number Class
Hi all,
Can anyone tell me how to write a servlet/Java Bean Class to give an
incrementing auto-generated number whenever this Class is called each
time...
Where can i find these source codes...
Thks..
--
_______________________________________________
Get your free email from http://freemail.asiamail.com
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html