Host Programming Table of Content: 1 Forming an HTTP request 2 Object-Oriented Host Language Sample Courseware Host Programming Language Interactions with a database would require some form of interface with users. Normally, the interface is built by means of so-called Host Programming Language (such as C, Java, COBOL, Pascal, etc.) through the database schema and using the DML. Host language is a tool of the technical experts and skilled programmers who use it to develop specialised software or database applications. Very often, the user interface is built using Internet technology. In this case we speak about Internet-based information systems. Generally, functionality of an Internet-based information system can be described as follows: 1. Client generates an HTTP request, and sends it to an HTTP server. 2. Server-side application processes the request, and generates HTTP response. 3. Client gets the response, and visualizes it. Thus, in order to build an Internet-based Information system, we need to implement the following tasks: generating an HTTP request on the client side. processing request and generating response on the server side. Forming an HTTP request Generally, a server-side application in invoked by an HTTP request looking as follows: http://[Uniform Resource Locator of the application] ? [parameters] Values are assigned to environment variables by the server before the application begins execution and, thus, are available to it when it begins. For example: http://coronet.iicm.edu/servlets/getMail ? Name=Nick&City=Graz Parameters="Name=Nick&City=Graz" Parameters are typically sent as a result of processing a so-called HTML FORM. It often represent a query string, such as a query to a database, depending on the function of the FORM. You can, of course, manually enter parameters directly in the URL. for example: <A HREF="http:coronet.iicm.edu/servlets/sentMail?Name=Nick&Topic=Important">Click here to run it</A> A form is introduced by the tag <FORM> and terminated by the inverse tag </FORM>. The attributes of the <FORM> tag includes METHOD and ACTION. For example: <FORM METHOD=GET ACTION="http://host/servlets/application_name"> </FORM> METHOD specifies which technical protocol the web server will use to pass the form data to the program that processes it, and ACTION tells the server exactly which program that is. A form field to request the user to enter text that is to be sent to the application is introduced by the following tag: <INPUT TYPE="text" NAME= "name of parameter" SIZE="width of the input area"> Note that the input data is sent to the CGI script in the form "Name of the parameter" = "Entered Value" The server-side application processes the entered data and responds with a new HTML document If a particular form contains multiple elements, the following tag is used to pass the submission of the input data to the CGI script: <INPUT TYPE= "submit" NAME="parameter" VALUE="Value if pressed"> The button when pressed will send, in addition to any information entered in the form, the message "parameter"= "Value if pressed". Note that there may be several of these input tags within a form. The VALUE attribute identifies which button, i.e. <INPUT> has been selected. When the user clicks the "submit" button, the browser collects the values of each of the input fields and sends them to the web server identified in the ACTION keyword of the FORM open tag. The web server then passes that data to the program identified in the ACTION, using the METHOD specified. Consider the following HTML form: <form action = "action1" method = "POST"> Name: <input type = "text" name = "name" size = "20"> <BR> I prefer: <select name = "preference"> <option value = Movies>Movies <option value = Music>Music <option value = Theater>Theater </select> <BR> <input type = "submit" value = "Send it!" > </form > After entering the requested info and pressing "Send it!" button: The client will send the following HTTP request to the server: http//[host]/[path]/action1?name=[value]&preference=[value] For example: Would produce: http//[host]/[path]/action1?name=Nick&preference=Theater Object-Oriented Host Language In object-oriented programs we represent real-world objects by means of software objects also known as abstract objects or simply objects. These objects are modeled after real-world objects in that they too have state and behavior: Software object maintains its state in one or more variables. A variable is an item of data named by an identifier. Software object implements its behavior with methods. A method is a function (subroutine) associated with an object. Speaking more formally, an object-oriented program consists of a number of objects, which communicate with each other by sending so-called messages. Thus, if one object wants another object to do something then it sends a message to the second object. Practically, sending message is equal to calling an instance method of another object. Sometimes, the object, which receives a message, needs to have more information in order to properly execute its method. For instance, if the car object has to change its gear it has to know which is the desired gear. Such additional information is attached to the message and called message parameters Message parameters are also objects ! In the real world there are objects that are of the same kind. Some of these objects have things in common with other objects. For instance, all cars have some state (gear, speed, etc.) and behavior (change gear, accelerate, etc.) in common. However, the state of each car is independent and can be different from other cars. In object-oriented programming we try to catch things that objects have in common by defining classes of objects. A class is a prototype that defines variables and methods that are common to all objects of the same kind. The process of receiving HTTP request and generating HTTP response is modelled as follows: When a Sevlet Engine receives an HTTP reguest: 1. Engine creates a new instance (object) of class HttpServletRequest. The object supports an interface to read incoming HTTP headers (e.g. cookies) and parameters (e.g. data the user entered and submitted) 2. Engine also creates a new instance (object) of class HttpServletResponce. The object supports an interface to specify the HTTP response line and headers. 3. Engine creates a new instance (object) of a specified sub-class of abstract class HttpServlet. The object supports a number of special methods (e.q. "doGet"). 4. Engine sends "doGet" message to the servlet object with "HttpServletRequest" and "HttpServletResponse" objects as parameters. 5. The servlet object runs the "doGet" method which normally accesses "HttpServletRequest" and "HttpServletResponse" objects. Developing a Servlet may be seen as the following sequence of steps. When a Sevlet Engine receives an HTTP reguest: 1. Programmer defines a new sub-class of the abstract data class "HttpServlet". 2. Programmer implements the methods "doGet", "doPost", "doDelete", etc . 3. Programmer re-use public interface of classes "HttpServletRequest" and "HttpServletResponse" to get HTTP parameters and to form an HTTP response. Typically a servlet implementation looks as follows: import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.io.PrintWriter; public class TemplateServlet extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ // Use "request" to read incoming parameters, e.g. request.getParameter("query"); // Use "response" to write HTTP headers PrintWriter writer = response.getWriter(); // Use "writer" to send response to the client } } A simplest "Hello World" servlet might look as follows: import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.io.PrintWriter; public class TemplateServlet extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ String hello = "Hello World"; response.setContentType("text/html"); PrintWriter writer = response.getWriter(); writer.println("<html>"); writer.println("\t<head>"); writer.println("\t<title>" + hello + "</title>"); writer.println("\t</head>"); writer.println("\t<body>"); writer.println(hello); writer.println("\t</body></html>"); } }