7. Advanced Servlet Techniques
The following notes look at the equivalent techniques for Servlets of some standard JSP functionality. For the most part these can be figured out by creating a JSP that does the required action then look at the Servlet code generated by the JSP Container.
Referencing the Session Object
The following code can be placed in either the doGet() or doPost()
methods to pick up the session object.
HttpSession session = request.getSession(true);
From here you can treat the session object just as you would the session
implicit object in JSP.
Referencing the Application Object
To obtain the application object you would place the following init()
method in your Servlet. Note you need to have the application object defined as a
private class variable so that other methods can then use the application object.
private ServletContext application;
public void init(ServletConfig config) throws ServletException {
application = config.getServletContext();
}
The Include Action
The <jsp:include/> action includes the output of another JSP, Servlet or
HTML page within the current page. In a Servlet you make use of the RequestDispatcher
object to perform this include. The RequestDispatcher object is obtained by calling the
getRequestDispatcher() method of the application object. See notes above
on how to instantiate the application object.
RequestDispatcher dispatcher = application.getRequestDispatcher("Page.jsp");
if (dispatcher == null) {
response.sendError(response.SC_NO_CONTENT);
}
else {
dispatcher.include(request, response);
}
The include() method of the RequestDispatcher object takes two
parameters of type HttpServletRequest and HttpServletResponse
respectively. The include() method call should be contained within a try..catch and the
method throws exceptions of type ServletException and java.io.IOException.
Consult the Servlet API for further details. The Servlet 2.3 API can be found at Sun's Java web
site: Servlet API.
The Forward Action
The equivalent to the <jsp:forward/> action is the forward()
method of the RequestDespatcher object. Below is an example of forwarding a request to
another JSP. As with the include() method above you can forward to a JSP, a Servlet or
any other process on the server that will understand an HTTP request.
RequestDispatcher dispatcher = application.getRequestDispatcher("Page.jsp");
if (dispatcher == null) {
response.sendError(response.SC_NO_CONTENT);
}
else {
dispatcher.forward(request, response);
}
JavaBean Instantiation
The last item we need to consider is how a Servlet instantiates a JavaBean. Recall that when we instantiate a JavaBean from a JSP page one of the attributes we define is the scope of the JavaBean. We must also do this when we instantiate a JavaBean from within a Servlet.
Below is an example of instantiating a JavaBean,
financialBean = (com.gulland.FinancialBean)Beans.instantiate(null, "com.gulland.FinancialBean");
application.setAttribute("FinancialBean", financialBean);
If you are using a JavaBean from within your Servlet you must reference the JavaBean pacakge:
import java.beans.*;. The instantiate() method of the Beans
class returns an object of type Object which is we require the explicit cast. The
following line in the example above is where we define the scope of the the JavaBean instance and
here it is being held at the application level. JavaBean properties are dealt with in the usual
manner of calling the appropriate getXXX() or setXXX() method of the
property. Note, HTML Form synchronisation is a JSP functionality and isn't readily achieved through
Servlets.
Quiz !
Okay, test your Servlet knowledge with the following quiz.