WK 11 Ses 19-20 - Handling Error and Security
WK 11 Ses 19-20 - Handling Error and Security
SPECIFIC OBJECTIVES:
At the end of the topic session, the students are expected to:
Cognitive :
Affective:
1. Participate in group activities.
2. Be attentive to the teacher’s lectures and demonstrations.
3. Participate in class and group discussions.
Psychomotor:
1. Develop a Web application that demonstrates error handling and
security.
MATERIALS/EQUIPMENTS:
o OHP
o Topic slides
TOPIC PREPARATION:
TOPIC PRESENTATION:
The topic will revolve around handling errors and security. Discussions
will include HTTP error codes, servlet exception, programmatic exception
handling, Web security, authentication and authorization.
This will be the suggested flow of discussion for Handling Errors and
Security.
The table above shows the status codes in the 400-500 range. These
Page 2 of 23 are used to indicate some error.
Java Enterprise Edition Programming
By default, the Web browser displays some message to the user. Often
Example Error Page this message is composed of HTML that is generated in the Web
browser. This means that the Web server did not send any HTML
message in the HTTP response. An example generic HTTP error page is
shown in the figure below.
Servlet Exceptions
Servlet Exceptions }
Page 3 of 23 catch (ArithmeticException ae)
{ throw new ServletException(ae);}
Java Enterprise Edition Programming
}
Servlet Exceptions
A servlet can throw a ServletException to indicate to the Web
In addition to HTTP errors, a Java
technology Web application can generate container that an exception has occurred. An example of a servlet that
exceptions to indicate a problem with
processing the HTTP request. throws an exception is shown in the codes above. The
ArithmeticException is a “non-checked” exception and need not be
public void doGet(HttpServletRequest
request, HttpServletResponse
response)throws ServletException
caught in a try-catch block. All non-check exceptions thrown by the
{
int x = 0, y = 0; service method are caught by the Web container, which issues a servlet
try
{
int z = x / y;
exception on behalf of the srvlet itself. The Web container will catch
}
catch (ArithmeticException ae)
these exceptions and send an HTTP response with a 500 status code
}
{ throw new ServletException(ae);}
and an HTML response with stack trace exception.
Declarative
Programmatic
The declarative way makes use of the deployment descriptor to declare
error pages for specific situations such as HTTP errors or Java
technology exceptions, and let the Web container handle the forwarding
to these pages.
Handling Error and Security * Property of STI
Page 4 of 23
The programmatic way handles the Java technology exceptions direct in
the servlet code and forwards the HTTP request to the error page of your
choice.
error page.
You can specify any number of error-page elements but only one for a
specific Java exception class. This is similar to custom error page. Also,
you can use a superclass like java.lang.Exception to capture a
range of exceptions.
The code above tells the Web container to catch the exception and
forward the HTTP request to the /error/bad_number custom error page.
The /error/bad_number is mapped to a servlet class (the error page
servlet). Before the error page servlet is activated, the Web container
adds two request attributes:
1. javax.servlet.error.exception
This attribute holds the actual exception object thrown by the original
2. javax.servlet.error.request_uri
This attribute holds a String of the request URL of the servlet in which
the error occurred which is the page or resource that the user had
originally requested.
These two request attributes are used to dynamically generate the HTML
response for the error page. Access to these attributes is handled by the
getAttribute() method.
To handle exceptions programmatically, Declarative exception handling is powerful and easy to use but it might
all error-prone business logic is wrapped
in a try-catch block. not be applicable for some situations. Programmatic exception handling
is another technique. It only applies to Java technology exceptions
thrown by servlets. It is not appropriate for handling HTTP errors.
try {
// Attempt to access the first character // of a
null String object string.charAt(0);
ServletContext context =
Example (cont..) getServletContext();
}
request.getRequestURI());
errorPage.forward(request, response); request.setAttribute("javax.servlet.error.request_u
}
ri",
//ExceptionDisplay class
Example (cont..) // Servlet imports
import javax.servlet.http.HttpServlet.*;
//ExceptionDisplay class
// Servlet imports
import javax.servlet.http.*;
import javax.servlet.http.HttpServlet.*;
import javax.servlet.http.*;
import javax.servlet.*;
import javax.servlet.*;
// Support classes
import java.io.*; // Support classes
public final class ExceptionDisplay extends
HttpServlet {
import java.io.*;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException {
response.setContentType("text/html");
public final class ExceptionDisplay extends
PrintWriter out = response.getWriter();
HttpServlet {
response.setContentType("text/html");
Page 12 of 23
PrintWriter out = response.getWriter();
Java Enterprise Edition Programming
Example (cont..)
Throwable exception
= (Throwable)
request.getAttribute("javax.servlet.error.exception
Throwable exception
= (Throwable)
request.getAttribute("javax.servlet.error.except
");
ion");
String expTypeFullName
= exception.getClass().getName();
String expTypeName
String expTypeFullName
=
expTypeFullName.substring(expTypeFullName.lastIn
dexOf(".")+1);
= exception.getClass().getName();
String request_uri
= (String)
request.getAttribute("javax.servlet.error.reques
t_uri");
String expTypeName
out.println("<HTML>");
out.println("<HEAD>");
=
out.println("<TITLE>Servlet
Exception</TITLE>");
out.println("</HEAD>");
expTypeFullName.substring(expTypeFullName.lastIndex
out.println("<BODY BGCOLOR='white'>");
out.println("<TABLE BORDER='0' CELLSPACING='0'
CELLPADDING='0' WIDTH='600'>");
out.println("<TR>");
Of(".")+1);
out.println("<TD
ALIGN='center‘VALIGN='center'>");
out.println(" </TD>");
String request_uri
= (String)
request.getAttribute("javax.servlet.error.request_u
Handling Error and Security * Property of STI
Page 12 of 23
ri");
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Servlet
Exception</TITLE>");
out.println("</HEAD>");
out.println("<BODY BGCOLOR='white'>");
out.println("<TABLE BORDER='0' CELLSPACING='0'
In the catch clause, you can forward the request to the exception
handling error page using a RequestDispatcher object. The request
dispatcher object is retrieved from the servlet context using the
getNamedDispatcher() method. The string passed to the
getNamedDispatcher() method must be the name of the exception
handling servlet defined in the deployment descriptor. You can also pass
request attributes to the exception handling servlet.
Note to the instructor: Code, compile and deploy the codes above and
show the result to your students.
Authentication Authentication
Page 15 of 23
Authentication techniques:
BASIC
BASIC
DIGEST
FORM
DIGEST
CLIENT-CERT
FORM
CLIENT-CERT
In the BASIC authentication, the Web browser solicits the username and
password and sends data to the Web server.
Handling Error and Security * Property of STI
Page 15 of 23
The Web container verifies the data against the vendor-specific security
realm. If the password is verified and the user is a member of the
required security role, then the Web container activates the original HTTP
request which could be a static HTML page or the activation of a servlet.
When the user fills in this form and selects the Submit button, the Web
container intercepts the j_security_check action and handles the
authentication. There is nothing that you need to code to make the
j_security_check action work. The Web container implements the servlet
Authorization Authorization
Page 19 of 23
3. Map the Web resource collection to The details for each will be discussed in the succeeding slides.
the roles
The http-method element specifies the HTTP method for the requests
that must be authorized by the Web container for the given Web resource
collection. You can specify any number of http-method elements in a
Web resource collection configuration. It is a good programming practice
to include both POST and GET HTTP methods.
EVALUATION:
o Ask the students to perform the laboratory exercise for this topic.
REFERENCES: