Home | | Sitemap ||Page number :6

3 Servlet samples

3.1 HelloWorld sample

The following class presents a basic sample of a WAP service implemented as a servlet. As the sample illustrates, writing servlets producing WML content is very much like writing servlets for generating HTML content. Thus, all the knowledge regarding Java Servlets available in various sources can easily be applied to the development of servlets for the Nokia WAP Server.

import java.io.*; import javax.servlet.*; import javax.servlet.http.*;

/**

* HelloWorldServlet -a very simple servlet */

public class HelloWorld extends HttpServlet { String m_text;

// the initialization parameter is read during // the initialization of the servlet

public void init(ServletConfig config) throws ServletException

{super.init(config); m_text = config.getInitParameter("text"); if (m_text == null) {

m_text = "This is a simple test servlet."; } }

public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException

{PrintWriter out = response.getWriter(); out.println("<?xml version=\"1.0\"?>"); out.println("<!DOCTYPE wml PUBLIC \"-//WAPFORUM//DTD WML

1.1//EN\"

\"http://www.wapforum.org/DTD/wml_1.1.xml\">"); out.println("<wml>"); out.println("<card id=\"card1\" title=\"Hello

World\">"); out.println("<p>");

out.println(m_text); out.println("</p>"); out.println("</card>"); out.println("</wml>");

// Remember to close the out object out.close(); }

public String getServletInfo() { return "The simple HelloWorld servlet."; } }

Note: The above sample is the same one that is pre-installed in the standard Nokia WAP Server package.

3.2 Connector sample

The following servlet illustrates an imaginary application that initialises an application server connection during servlet.init() and uses the application server during the request processing. The Nokia WAP Server has been configured so that it requires authentication before the request reaches the servlets.

The sample service is a connector type of servlet that connects to an existing system that controls doors and locks in an office building. The door locking system provides a monthly changing PIN-code to prevent unauthorised entry to building outside office hours. The WAP service provides the authorised users the currently correct PIN-code for the selected door. The user navigates to a WML page that either contains a list of possible doors or that asks the user to supply the door ID with a WML input form. The servlet then responds with the correct PIN.

import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import com.xcomp.DoorService.*; import com.nokia.wap.server.*; import samples.WMLTool;

public class WapService extends HttpServlet

{

// Initialization of a connection to a DoorService system

DoorService doors;

public void init(ServletConfig config) throws ServletException { doors = new DoorService (config.getInitParameter("ServerAddress")); doors.openConnection(); }

public void doGet(HttpServletRequest req,

Servlet samples

HttpServletResponse res) throws ServletException, IOException

{String user = req.getRemoteUser(); // Authenticated user Hashtable ht =

HttpUtils.parseQueryString(req.getQueryString()); String door = (String)ht.get("doorId"); int pin = doors.FetchCurrentPIN(user, door); StringBuffer response = new StringBuffer();

if (pin > 0) { response.append("Welcome, the current PIN to this door is:");

response.append(pin); } else {

response.append("Sorry, correct PIN can not be provided now."); }

// Create the response by using the WMLTool sample

WMLTool.Deck deck = new WMLTool.Deck( new WMLTool.Card("card1", "Door Service"));

deck.getCard(0).add(new WMLTool.Paragraph(response));

res.setContentType(WMLTool.WML_MIME_TYPE); res.getWriter().print(deck); res.getWriter().close();

}}

The following chapter describes in more detail the interfaces used in the sample.