Sun Java Solaris Communities My SDN Account Join SDN
 
Java Developer Connection (JDC):Behind the Scenes

RegisterServlet

 
 
  1. /**
  2. * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  3. *
  4. * Permission to use, copy, modify, and distribute this software
  5. * and its documentation for NON-COMMERCIAL purposes and without
  6. * fee is hereby granted provided that this copyright notice
  7. * appears in all copies. Please refer to the file "copyright.html"
  8. * for further important copyright and licensing information.
  9. *
  10. * The Java source code is the confidential and proprietary information
  11. * of Sun Microsystems, Inc. ("Confidential Information"). You shall
  12. * not disclose such Confidential Information and shall use it only in
  13. * accordance with the terms of the license agreement you entered into
  14. * with Sun.
  15. *
  16. * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  17. * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  18. * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  19. * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  20. * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  21. * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  22. */
  23. import java.io.*;
  24. import javax.servlet.*;
  25. import javax.servlet.http.*;
  26. import java.util.*;
  27. import java.io.*;
  28. /**
  29. * @author Tony Squier
  30. * @version 1.8 97/02/10 13:29:25
  31. */
  32. public class RegisterServlet extends HttpServlet
  33. {
  34. /**
  35. * The UserRegistry implementation that provides access to a storage of users.
  36. */
  37. private UserRegistry _registry;
  38. /**
  39. * @param stub Arguments passed by the Servlet loader
  40. * @return void
  41. */
  42. public void init (ServletConfig config) throws ServletException
  43. {
  44. super.init(config);
  45. _registry = new UserRegistryJdbcImpl();
  46. }
  47. /**
  48. * Main service routine - do a register.
  49. * @param request The HttpServletRequest.
  50. * @param response The HttpServletResponse.
  51. * @throws ServletException
  52. * @throws IOException
  53. */
  54. public void service (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  55. {
  56. // Check for valid fields
  57. String userId = request.getParameter ("UserId");
  58. String password = request.getParameter ("Password");
  59. String email = request.getParameter ("Email");
  60. if ( userId != null && password != null )
  61. {
  62. // Check for a duplicate
  63. try
  64. {
  65. JDCMember members[] = _registry.getMember (userId);
  66. if ( members.length > 0 )
  67. {
  68. // Duplicate
  69. response.sendError (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "The UserId you've chosen already exist, please go back and pick another one.");
  70. }
  71. else
  72. {
  73. JDCMember member = new JDCMember ();
  74. member.setUserId (userId);
  75. member.setPassword (password);
  76. member.setEmail (email);
  77. _registry.setMember (member);
  78. response.sendRedirect ("/servlet/SessionServlet?action=showLogin");
  79. }
  80. }
  81. catch (JDCAdminException e)
  82. {
  83. e.printStackTrace();
  84. response.sendError (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "A problem occured with our database, please try again later.");
  85. }
  86. }
  87. else
  88. {
  89. // Show the register page again.
  90. response.sendError (HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Please go back and provide both a User Id and Password");
  91. }
  92. }
  93. }