I'm using a HTML Form to send data to a servlet, the servlet then checks the data submitted is valid (against some rules) and reloads the page with new request attributes to return the results. I can almost guarantee it's not the best way of doing it but I don't think that's causing the issue. I think the issue lies in the fact the form redirects to localhost:8080/Register rather than localhost:8080/project-context/Register. Is there any way to specify the path inside the HTML? Could I do it with some JSP expressions? I'd rather not hardcode the project path as it may change in future. The HTML and relevant servlet code are below:
HTML:
<body>
<% String errorMessage = (String) request.getAttribute("ErrorMessage"); %>
<form name="Register" action="/Register" method="POST">
<%= errorMessage %><br><br>
User name: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
Verify Password: <input type="password" name="vPassword"><br>
Email: <input type="text" name="email"><br>
Verify Email: <input type="text" name="vEmail"><br><br>
<input type="submit" value="Register">
</form>
</body>
Servlet/Java:
@WebServlet("/Register")
public class UserRegistrationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public UserRegistrationServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
HttpSession session = request.getSession(false);
if (session == null) {
request.getRequestDispatcher("/");
return;
}
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
}
catch(Throwable errorMessage) {
request.setAttribute("ErrorMessage", errorMessage.getMessage());
request.setAttribute("Error Cause", errorMessage.getCause());
request.setAttribute("ErrorLocation", this.getServletName());
request.setAttribute("ErrorStackTrace", errorMessage.getStackTrace());
request.getRequestDispatcher("/WEB-INF/errorDisplay.jsp").forward(request, response);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String ErrorMessage = "";
//Check user name is supplied
if (request.getParameter("Username") == null) {
ErrorMessage = "You must enter a username!";
request.setAttribute("ErrorMessage", ErrorMessage);
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
return;
}
//Check user name for maximum length
if (request.getParameter("Username").length() > 16) {
ErrorMessage = "The username you entered was too long! Only 16 characters are allowed.";
request.setAttribute("ErrorMessage", ErrorMessage);
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
return;
}
//Check password is supplied
if (request.getParameter("Password") == null) {
ErrorMessage = "You must enter a password!";
request.setAttribute("ErrorMessage", ErrorMessage);
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
return;
}
//Check password for complexity
/*--------------------------------------------------------
(?=.*[0-9]) a digit must occur at least once
(?=.*[a-z]) a lower case letter must occur at least once
(?=.*[A-Z]) an upper case letter must occur at least once
(?=[\\S]+$) no whitespace allowed in the entire string
.{6,16} at least 6 to 16 characters
---------------------------------------------------------*/
Pattern passwordPattern = Pattern.compile("((?=.*[0-9])(?=.*[a-z]) (?=.*[A-Z])(?=[\\S]+$).{6,16})", Pattern.CASE_INSENSITIVE);
Matcher passwordMatcher = passwordPattern.matcher(request.getParameter("Password"));
if (passwordMatcher.find() == false) {
if (request.getAttribute("password") == request.getAttribute("vPassword")) {}
else {
ErrorMessage = "The passwords you entered do not match!";
request.setAttribute("ErrorMessage", ErrorMessage);
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
return;
}
ErrorMessage = "The password you entered does not abide by the strength rules!";
request.setAttribute("ErrorMessage", ErrorMessage);
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
return;
}
//Check email is supplied
if (request.getParameter("Email") == null) {
ErrorMessage = "You must enter an email!";
request.setAttribute("ErrorMessage", ErrorMessage);
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
return;
}
//Validate email - *@*
Pattern emailPattern = Pattern.compile(".+@.+\\.[a-z]+", Pattern.CASE_INSENSITIVE);
Matcher emailMatcher = emailPattern.matcher(request.getParameter("Email"));
if (emailMatcher.find() == false) {
if (request.getAttribute("email") == request.getAttribute("vEmail")) {}
else {
ErrorMessage = "The emails you entered did not match!";
request.setAttribute("ErrorMessage", ErrorMessage);
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
return;
}
ErrorMessage = "The email you entered is not valid!";
request.setAttribute("ErrorMessage", ErrorMessage);
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
return;
}
UserRegistrationBean user = new UserRegistrationBean();
user.setUsername(request.getParameter("Username"));
user.setPassword(request.getParameter("Password"));
user.setEmail(request.getParameter("Email"));
user = UserDAO.register(user);
if (user.getExists() == true) {
ErrorMessage = "The user name you entered has already been registered!";
request.setAttribute("ErrorMessage", ErrorMessage);
request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
return;
}
}
catch(Throwable errorMessage) {
request.setAttribute("ErrorMessage", errorMessage.getMessage());
request.setAttribute("Error Cause", errorMessage.getCause());
request.setAttribute("ErrorLocation", this.getServletName());
request.setAttribute("ErrorStackTrace", errorMessage.getStackTrace());
request.getRequestDispatcher("/WEB-INF/errorDisplay.jsp").forward(request, response);
}
}
}
Thanks in advance for any help you might provide!