coderzz027
coderzz027

Reputation: 349

Exceptions in jsp

My question is regarding exceptions. Is it really bad if the program throws an exception even if i handle it?? I am working on a jsp project, where i have set the username of a user as a session variable, which i access on every page, like this session.getAttribute("username") to convert it into a string i use .toString() function to convert it into string which i then use in my code for further processing.

So, if the user hasn't authenticated himself at the login page and tries to access some page such as view-forums.jsp (one of the page names in my project) directly from the url then thesession.getAttribute("username").toString() throws a null pointer exception, for which I have an error page, which looks like this

<%@page  isErrorPage="true"%>
<%@include file="classes/template.jsp"%>
<%
    session.invalidate();
    Template objTemplate=new Template();
    String Data="<div id=text>ERROR!!!!!!!!!! Page Not Found<br>Please Sign in Again</div>";
    out.println(objTemplate.genPage(Data,false));
%>`

So, whenever there is any exception my webapp gets redirected to this error page. So, in a way I am using exceptions to make secure login to my website. Is this is a bad way to do so?? If yes then can someone please explain? Thanks for your any help in advance.. :)

Upvotes: 1

Views: 68

Answers (1)

Jeanne Boyarsky
Jeanne Boyarsky

Reputation: 12266

Yes, this is bad. Scriptlets were acceptable technology over a decade ago. Their time has come and gone.

New code should be using model view controller (MVC). The idea is to have your logic in a servlet (or a class called from a servlet) and have it forward to a JSP for display purposes only.

Having any scriptlets in a JSP isn't good. Having scriptlets with logic in a JSP is even worse.

Why not put all that code in a servlet?

Upvotes: 1

Related Questions