简体   繁体   中英

Display servlet errors in JSP using JSTL or EL

I want to display error messages eg SQLException errors in the jsp page that the servlet has redirected to. I am new in java and JSP but I have managed to get basic things like saving data using a servlet to a database working. Below is my code for catching errors encountered

 catch(SQLException e){
         out.println("SQL Error encountered "+e.getMessage()); 

How can I catch this error into a jsp page that has been redirected to by the servlet using jstl or Expression Language? Eg if an admin submits a duplicate email address from a page called createuser.jsp, the error should be displayed in creatuser.jsp

You should never post exception related material to your front end since this can provide information on your internal structure and underlying architecture which is never a good idea since it could lead to possible attacks.

What you could do would be to do some sort of form validation, as shown in here and, as in your case, instead of showing exception messages, you simply make a check for the name and if it throws and exception, just display a message on the screen something like so: User Already Exists! . Error messages such as these are usually also less confusing for non technical people.

You can add the message as an attribute of your request:

Servlet:

catch(SQLException e) {
    request.setAttribute("exception", "SQL error encountered");
}

Jsp:

${exception}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM