source: test/services/jregistrate/src/register.java @ 17

Last change on this file since 17 was 17, checked in by tim, 9 years ago

.

File size: 5.7 KB
RevLine 
[17]1package jregistrate;
2
3import java.lang.System;
4import java.io.*;
5import javax.servlet.ServletException;
6import javax.servlet.http.*;
7import java.sql.*;
8import org.sqlite.JDBC;
9
10
11/* Copyright (C) 2015 Blindspot Security LLC. All rights reserved.
12 * Author: Timothy D. Morgan
13 */
14public final class register extends BaseServlet
15{
16    public void doGet(HttpServletRequest request,
17                      HttpServletResponse response)
18      throws IOException, ServletException
19    {
20        response.setContentType("text/html");
21
22        PrintWriter writer = response.getWriter();
23        writer.println("<html>");
24        writer.println("<head>");
25        writer.println("<title>Register for an Account</title>");
26        writer.println("</head>");
27        writer.println("<body bgcolor='#EEFFEE'>");
28        String error = (String)request.getAttribute("error");
29        if (error != null)
30        {
31            writer.println("<span style='color: red;'>ERROR:"+error+"</span>");
32        }
33        writer.println("<!-- Hint: sample valid: 0012-8846,9475 -->");
34       
35        String member_id = request.getParameter("member_id");
36        member_id = (member_id == null) ? "" : member_id;
37        String last_four = request.getParameter("last_four");
38        last_four = (last_four == null) ? "" : last_four;
39        //String zip_code = request.getParameter("zip_code");
40        //zip_code = (zip_code == null) ? "" : zip_code;
41        String username = request.getParameter("username");
42        username = (username == null) ? "" : username;
43        String password = request.getParameter("password");
44        password = (password == null || !password.equals(request.getParameter("conf_pwd"))) ? "" : password;
45       
46       
47        writer.println("<form action='register' method='POST'>");
48        writer.println("<table border=\"0\" cellpadding=\"10\">");
49
50        writer.println("<tr><td colspan='3'>");
51        writer.println("<h1>Boobie Veterinary Insurance Company, Inc, LLC</h1>");
52        writer.println("</td></tr>");
53       
54        writer.println("<tr><td colspan='2'><h2>Register for Your Online Account</h2></td>");
55        writer.println("<td rowspan='10'><img src='images/blue-footed-boobie.jpg' height='300' border='1' /></td>");
56        writer.println("</tr>");
57
58        writer.println("<tr><td>Membership ID<br/>(Format: ####-####):</td><td><input type='text' name='member_id' value='"+htmlEncode(member_id)+"' /></td></tr>");
59        writer.println("<tr><td>Last 4 of SSN:</td><td><input type='text' name='last_four' value='"+htmlEncode(last_four)+"' /></td></tr>");
60        //writer.println("<tr><td>Zip Code:     </td><td><input type='text' name='zip_code' value='"+htmlEncode(zip_code)+"' /></td></tr>");
61        writer.println("<tr></tr>");
62        writer.println("<tr><td>Username:</td><td><input type='text' name='username' value='"+htmlEncode(username)+"' /></td></tr>");
63        writer.println("<tr><td>Password:</td><td><input type='text' name='password' value='' /></td></tr>");
64        writer.println("<tr><td>Confirm Password:</td><td><input type='text' name='conf_pwd' value='' /></td></tr>");
65        writer.println("<tr><td><input type='submit' value='submit'></td></tr>");
66        writer.println("</table>");
67        writer.println("</form>");
68        writer.println("</body>");
69        writer.println("</html>");
70    }
71
72
73    public void doPost(HttpServletRequest request,
74                       HttpServletResponse response)
75        throws IOException, ServletException
76    {
77        request.setAttribute("error", null);
78        try
79        {
80            String member_id = request.getParameter("member_id");
81            member_id = (member_id == null) ? "" : member_id;
82            String last_four = request.getParameter("last_four");
83            last_four = (last_four == null) ? "" : last_four;
84            //String zip_code = request.getParameter("zip_code");
85            //zip_code = (zip_code == null) ? "" : zip_code;
86            String username = request.getParameter("username");
87            username = (username == null) ? "" : username;
88            String password = request.getParameter("password");
89            password = (password == null || !password.equals(request.getParameter("conf_pwd"))) ? "" : password;
90           
91           
92            Connection db = openDB();
93            PreparedStatement ps = db.prepareStatement("SELECT * FROM members WHERE member_id=?");
94            ps.setString(1, member_id);
95            ResultSet rs = ps.executeQuery();
96            long start = System.nanoTime();
97            PrintWriter writer = response.getWriter();
98            if (rs.next())
99            {
100                if (last_four.equals(decryptLastFour(rs.getString("enc_last_four"))))
101                {
102                    if (!"".equals(password))
103                    {
104                        // member_id already registered?
105                        // username already registered?
106                        response.setContentType("text/html");
107                        writer.println("<html><body>Registration Successful!</body></html>");
108                        return;
109                    }
110                    else
111                        request.setAttribute("error", "Bad password or passwords don't match");
112                }
113            }
114            response.addHeader("X-Response-Time", String.format("%fms", (System.nanoTime()-start)/1000000.0));
115            if (request.getAttribute("error") == null)
116                request.setAttribute("error", "Invalid personal information specified.  Try again.");
117        }
118        catch (Exception e)
119        {
120            request.setAttribute("error", "Unknown error occurred.  See logs.");
121            e.printStackTrace();
122        }
123       
124        doGet(request, response);
125    }
126
127}
Note: See TracBrowser for help on using the repository browser.