-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fully working and Error Handling for Registration page
- Loading branch information
1 parent
aefb315
commit 96dec4a
Showing
8 changed files
with
181 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package controller; | ||
|
||
import model.entity.PersonalInfo; | ||
import model.entity.User; | ||
import model.service.registration.Registration; | ||
import model.service.user.UserData; | ||
import model.service.user.UserRegistry; | ||
|
||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import javax.servlet.http.HttpSession; | ||
import java.io.IOException; | ||
|
||
@WebServlet("/register") | ||
|
||
public class RegistrationServlet extends HttpServlet { | ||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { | ||
|
||
String licenseCode = request.getParameter("licenseCode"); | ||
String email = request.getParameter("email"); | ||
String password = request.getParameter("password"); | ||
String name = request.getParameter("name"); | ||
String surname = request.getParameter("surname"); | ||
|
||
Registration registration = new Registration(); | ||
int result = registration.registerNewUser(licenseCode, email, password, name, surname); | ||
response.setContentType("text/html"); | ||
response.setCharacterEncoding("UTF-8"); | ||
response.getWriter().write(String.valueOf(result)); | ||
if(result == 0) { | ||
setSessionAttributes(email, request); | ||
response.sendRedirect("JSP/legal.jsp"); | ||
} | ||
} | ||
|
||
private void setSessionAttributes(String email, HttpServletRequest request){ | ||
HttpSession session = request.getSession(); | ||
|
||
UserData userData = new UserData(); | ||
UserRegistry userReg = new UserRegistry(); | ||
|
||
User user = userData.getUserByIdOrEmail(email); | ||
PersonalInfo personalInfo = userReg.getPersonalInfo(user.getId()); | ||
|
||
session.setAttribute("id", user.getId()); | ||
session.setAttribute("name", personalInfo.getFirstname()); | ||
|
||
if(!userData.isTherapist(user)) { | ||
session.setAttribute("type", "patient"); | ||
session.setAttribute("therapist", user.getIdTherapist()); | ||
} | ||
else { | ||
session.setAttribute("type", "therapist"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
$(document).ready(function() { | ||
$('#signUpButton').click(function(e) { | ||
//e.preventDefault(); | ||
let pin = $('#pin').val(); | ||
let cognome = $('#cognome').val(); | ||
let nome = $('#nome').val(); | ||
let email = $('#email').val(); | ||
let password = $('#password').val(); | ||
|
||
let registrationData = { | ||
licenseCode: pin, | ||
surname: cognome, | ||
name: nome, | ||
email: email, | ||
password: password | ||
}; | ||
|
||
$.ajax({ | ||
type: 'POST', | ||
url: '../register', | ||
data: registrationData, // data is passed as form data | ||
success: function(response) { | ||
var resultCode = parseInt(response); | ||
var message; | ||
switch (resultCode) { | ||
case 0: | ||
break; | ||
case 1: | ||
message = "Licenza non valida."; | ||
break; | ||
case 2: | ||
message = "Email già in uso"; | ||
break; | ||
case 3: | ||
message = "Impossibile generare l'utente"; | ||
break; | ||
case 4: | ||
message = "Impossibile generare anagrafica"; | ||
break; | ||
default: | ||
message = "Errore sconosciuto"; | ||
} | ||
if(resultCode !== 0) { | ||
alert(message); | ||
} | ||
console.log('User registered error code: ', response); | ||
}, | ||
error: function(error) { | ||
console.log('Error while registering user: ', error); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters