title: Java Servlet/JSP Custom Error Page
body: <p>I have a custom error page setup for basic debugging whilst I'm programming and for some reason none of the values from the try catches get through. The error page just says: "Null null null". If anyone can help with this I'd be very grateful.</p>
  
  <p>Servlet:</p>
  
  <pre><code>package com.atrium.userServlets;
  
  import java.io.IOException;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  
  import javax.servlet.ServletException;
  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 com.atrium.DAOs.UserDAO;
  import com.atrium.userBeans.UserRegistrationBean;
  
  @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) {
  
                  response.sendRedirect(this.getServletContext() + "/Authenticate");
                  return;
  
              }
  
              request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
  
          }
  
          catch(Throwable exception) {
  
              String errorMessage = exception.getMessage();
              Throwable errorCause = exception.getCause();
              String errorLocation = this.getServletName();
  
              request.setAttribute("ErrorMessage", errorMessage);
              request.setAttribute("ErrorCause", errorCause);
              request.setAttribute("ErrorLocation", errorLocation);
  
              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 || request.getParameter("Username") == "") {
  
                  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() &gt; 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 || request.getParameter("Password") == "") {
  
                  ErrorMessage = "You must enter a password!";
  
                  request.setAttribute("ErrorMessage", ErrorMessage);
  
                  request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
  
                  return;
  
              }
  
              //Check verify password is supplied
  
              if (request.getParameter("vPassword") == null || request.getParameter("vPassword") == "") {
  
                  ErrorMessage = "You must enter your password twice!";
  
                  request.setAttribute("ErrorMessage", ErrorMessage);
  
                  request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
  
                  return;
  
              }
  
              //Check password is equal to verify password
  
              if (((String)request.getParameter("Password")).equals((String)request.getParameter("vPassword"))) {}
              else {
  
                  ErrorMessage = "The passwords you entered do not match!";
  
                  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})");
  
              Matcher passwordMatcher = passwordPattern.matcher(request.getParameter("Password"));
  
              if (passwordMatcher.find() == false) {
  
                  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 || request.getParameter("Username") == "") {
  
                  ErrorMessage = "You must enter an email!";
  
                  request.setAttribute("ErrorMessage", ErrorMessage);
  
                  request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
  
                  return;
  
              }
  
              //Check verify email is supplied
  
              if (request.getParameter("vEmail") == null || request.getParameter("vEmail") == "") {
  
                  ErrorMessage = "You must enter your email twice!";
  
                  request.setAttribute("ErrorMessage", ErrorMessage);
  
                  request.getRequestDispatcher("/WEB-INF/register.jsp").forward(request, response);
  
                  return;
  
              }
  
              //Check email is equal to verify email
  
              if (((String)request.getParameter("Email")).equals((String)request.getParameter("vEmail"))) {}
              else {
  
                  ErrorMessage = "The emails you entered did not match!";
  
                  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) {
  
                  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 exception) {
  
              String errorMessage = exception.getMessage();
              Throwable errorCause = exception.getCause();
              String errorLocation = this.getServletName();
  
              request.setAttribute("ErrorMessage", errorMessage);
              request.setAttribute("ErrorCause", errorCause);
              request.setAttribute("ErrorLocation", errorLocation);
  
              request.getRequestDispatcher("/WEB-INF/errorDisplay.jsp").forward(request, response);
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>JSP Error Page:</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  
  &lt;html&gt;
  
  &lt;head&gt;
  
      &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
      &lt;title&gt;Exception Details&lt;/title&gt;
  
  &lt;/head&gt;
  
  &lt;body&gt;
  
      &lt;% final String errorMessage = (String)request.getAttribute("ErrorMessage"); %&gt;
      &lt;% final Throwable errorCause = (Throwable)request.getAttribute("ErrorCause"); %&gt;
      &lt;% final String errorLocation = (String)request.getAttribute("ErrorLocation"); %&gt;
  
      &lt;h1&gt;An Error Occurred...&lt;/h1&gt;
  
      &lt;p&gt;
  
  
  
          &lt;%= errorMessage %&gt;&lt;br&gt;&lt;br&gt;
  
          &lt;%= errorCause %&gt;&lt;br&gt;&lt;br&gt;
  
  
  
      &lt;%= errorLocation %&gt;
  
  &lt;/p&gt;
  </code></pre>
  
  <p></p>
  
  <p></p>