-
Notifications
You must be signed in to change notification settings - Fork 0
/
signup.jsp
executable file
·88 lines (72 loc) · 3.32 KB
/
signup.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<html>
<head>
<title>Sign Up Page</title>
</head>
<body>
<%@ page import="java.sql.*"%>
<%-- -------- Open Connection Code -------- --%>
<%
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
boolean emptyString = false;
boolean spaces = false;
try {
// Registering Postgresql JDBC driver with the DriverManager
Class.forName("org.postgresql.Driver");
// Open a connection to the database using DriverManager
conn = DriverManager.getConnection(
"jdbc:postgresql://localhost/Project1DB?" +
"user=postgres&password=postgres");
%>
<%-- -------- INSERT Code -------- --%>
<%
String action = request.getParameter("action");
emptyString = request.getParameter("username").equals("");
spaces = request.getParameter("username").contains(" ");
// Check if an insertion is requested
if (action != null && action.equals("insert") && emptyString == false && spaces == false) {
// Begin transaction
conn.setAutoCommit(false);
// Create the prepared statement and use it to
// INSERT student values INTO the students table.
pstmt = conn
.prepareStatement("INSERT INTO users (username, age, state, role) VALUES (?, ?, ?, ?)");
pstmt.setString(1, request.getParameter("username"));
pstmt.setInt(2, Integer.parseInt(request.getParameter("age")));
pstmt.setString(3, request.getParameter("state"));
pstmt.setString(4, request.getParameter("role"));
int rowCount = pstmt.executeUpdate();
// Commit transaction
conn.commit();
conn.setAutoCommit(true);
out.println("You have successfully signed up.");
}
else if(emptyString == true){
throw new RuntimeException("EMPTY STRING");
}
else if(spaces == true){
throw new RuntimeException("SPACES EXIST");
}
%>
<%} catch(Exception e){
//throw new Exception(e);
//out.println(e);
if (e.getMessage().contains("For input string:")) {
out.println("The age field must be an integer. Please try signing up again.");
} else if (e.getMessage().contains("duplicate key value violates unique constraint")) {
out.println("The username \"" + request.getParameter("username") + "\" has already " +"been taken. Please sign up again with a different username.");
} else if(e.getMessage().contains("EMPTY STRING")) {
out.println("You must enter something in the username field. Please try again.");
} else if(e.getMessage().contains("SPACES EXIST")){
out.println("You may not have spaces in your username. Please try again.");
}
else {
out.println("Your signup failed.");
}
}
%>
<p /><a href="signup.html">Return to signup page</a>
<p /><a href="login.html">Login</a>
</body>
</html>