/* $Id: CreateAccountServlet.java,v 0.0 2001/01/04 20:21:33 roma Exp $ * */ import java.io.*; import java.text.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import com.stalker.CGPro.*; //make sure you have CGProCLI.jar file /** * Example servlet creating a CGPro account * * @author Roman Prokhorov */ public class CreateAccountServlet extends HttpServlet { ResourceBundle rb = ResourceBundle.getBundle("LocalStrings"); public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String host,adminPassword; out.println(""); out.println(""); String title = "CommuniGate Pro CreateAccount servlet"; out.println("" + title + ""); out.println(""); out.println(""); try { host = rb.getString("cgpro.host"); adminPassword = rb.getString("cgpro.password"); //if you can recompile this, it will be more convinient for you to //hard-code the host and password instead of reading them from a file } catch(MissingResourceException x) { errormsg(out,"Error in configuration:
"+ "The CGPro host address and postmaster password must be included into LocalStrings.properties file.
"); return; } out.println("

" + "User Creation Sample Servlet for \""+host + "\"

"); if(!request.getParameterNames().hasMoreElements()) { out.println("

"); out.print("

"); out.println("

Account name:

"); out.println("

Real name:

"); out.println("

Password: "); out.println("

Password (again):

"); out.println("

Mailbox type:

"); out.println("

E-mail address to retrieve forgotten password:

"); out.println("

"); out.println("

"); out.println("
"); } else { String account = request.getParameter("account"); String realName = request.getParameter("realname"); String password1 = request.getParameter("password1"); String password2 = request.getParameter("password2"); String boxtype = request.getParameter("boxtype"); String recover = request.getParameter("recover"); if(account==null || account.length()==0) { errormsg(out,"No account name specified"); return; } if(password1==null || password1.length()==0) { errormsg(out,"No password specified"); return; } if(!password1.equals(password2)) { errormsg(out,"Passwords do not match!"); return; } try { CGProCLI cli=new CGProCLI(host,106,"postmaster",adminPassword); Hashtable settings=new Hashtable(); settings.put("RealName",realName); settings.put("Password",password1); settings.put("RecoverPassword",recover); try { cli.createAccount(account,settings,boxtype,false); out.println("

Account created.

"); out.println("Create one more User
"); out.println("Open the created Account"); } catch (CGProException x) { out.println("Can't create account:"+x.getMessage()); out.println("Create a User
"); } cli.logout(); } catch (CGProException x) { out.println("Error:"+x.getMessage()); } } out.println(""); out.println(""); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doGet(request, response); } private void errormsg(PrintWriter out,String msg) { out.println(""); out.println(msg); out.println("Create a User
"); out.println(""); out.println(""); } }