Java Servlet/JSP Custom Error Page -
i have custom error page setup basic debugging whilst i'm programming , reason none of values try catches through. error page says: "null null null". if can i'd grateful.
servlet:
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 supplied if (request.getparameter("username") == null || request.getparameter("username") == "") { errormessage = "you must enter username!"; request.setattribute("errormessage", errormessage); request.getrequestdispatcher("/web-inf/register.jsp").forward(request, response); return; } //check user name maximum length if (request.getparameter("username").length() > 16) { errormessage = "the username entered long! 16 characters allowed."; request.setattribute("errormessage", errormessage); request.getrequestdispatcher("/web-inf/register.jsp").forward(request, response); return; } //check password supplied if (request.getparameter("password") == null || request.getparameter("password") == "") { errormessage = "you must enter password!"; request.setattribute("errormessage", errormessage); request.getrequestdispatcher("/web-inf/register.jsp").forward(request, response); return; } //check verify password supplied if (request.getparameter("vpassword") == null || request.getparameter("vpassword") == "") { errormessage = "you must enter password twice!"; request.setattribute("errormessage", errormessage); request.getrequestdispatcher("/web-inf/register.jsp").forward(request, response); return; } //check password equal verify password if (((string)request.getparameter("password")).equals((string)request.getparameter("vpassword"))) {} else { errormessage = "the passwords entered not match!"; request.setattribute("errormessage", errormessage); request.getrequestdispatcher("/web-inf/register.jsp").forward(request, response); return; } //check password complexity /*-------------------------------------------------------- (?=.*[0-9]) digit must occur @ least once (?=.*[a-z]) lower case letter must occur @ least once (?=.*[a-z]) upper case letter must occur @ least once (?=[\\s]+$) no whitespace allowed in entire string .{6,16} @ least 6 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 entered not abide strength rules!"; request.setattribute("errormessage", errormessage); request.getrequestdispatcher("/web-inf/register.jsp").forward(request, response); return; } //check email supplied if (request.getparameter("email") == null || request.getparameter("username") == "") { errormessage = "you must enter email!"; request.setattribute("errormessage", errormessage); request.getrequestdispatcher("/web-inf/register.jsp").forward(request, response); return; } //check verify email supplied if (request.getparameter("vemail") == null || request.getparameter("vemail") == "") { errormessage = "you must enter email twice!"; request.setattribute("errormessage", errormessage); request.getrequestdispatcher("/web-inf/register.jsp").forward(request, response); return; } //check email equal verify email if (((string)request.getparameter("email")).equals((string)request.getparameter("vemail"))) {} else { errormessage = "the emails 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 entered 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 entered has 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); } } }
jsp error page:
<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>exception details</title> </head> <body> <% final string errormessage = (string)request.getattribute("errormessage"); %> <% final throwable errorcause = (throwable)request.getattribute("errorcause"); %> <% final string errorlocation = (string)request.getattribute("errorlocation"); %> <h1>an error occurred...</h1> <p> <%= errormessage %><br><br> <%= errorcause %><br><br> <%= errorlocation %> </p>
make these changes catch block:
catch(throwable errormessage) { request.setattribute("errormessage", (string)errormessage.getmessage()); request.setattribute("errorcause", errormessage.getcause()); request.setattribute("errorlocation", (string)this.getservletname()); request.getrequestdispatcher("/web-inf/errordisplay.jsp").forward(request, response); }
try on jsp page:
<body> <h1>an error occurred...</h1> <p> ${requestscope.errormessage}<br><br> ${requestscope.errorcause}<br><br> ${requestscope.errorlocation} </p> </body>
Comments
Post a Comment