JSP Basics  |  Mobile Infrastructure  |  AWT  |  Developing with Eclipse

 

Java Server Pages (JSP)

As programming model for browser applications, the SAP Mobile Infrastructure supports JSP, based on the Tomcat servlet engine.

JSP and Servlets

The Mobile Infrastructure provides the class com.sap.ip.me.api.runtime.jsp.AbstractMEHttpServlet which extends the class javax.servlet.http.HttpServlet. For a servlet you usually have to implement the doPost() and doGet() methods, getting the event that has been raised and calling the JSP. The AbstractMEHttpServlet class provides the method doHandleEvent(String eventName, HttpServletRequest request, HttpServletResponse response) that implements the doGet() and doPost() methods. The doHandleEvent() method gets the raised event from the Http request and stores it in the parameter String eventName and calls a JSP after method doHandleEvent() is finished. The name of the JSP to be called is supplied by the return statement of the doHandleEvent() method. The JSP is called with the forward method or with the sendRedirect method in case the request dispatcher is null.


Applications that use several servlets must observe the following rules:


Forms and Events

For user interfaces that require user input you have to put the input objects (for example, input field, check boxes and so on) in a HTML Form to have access to the input objects in the servlet. The Form requires a submit button.


Example for a submit button with the label Send :

<input type="submit" value="Send" name="_event_mySubmitEvent">

When the user clicks on the Send button, the AbstractMEHttpServlet class gets the event from the request by scanning the Form for a parameter that begins with _event_. This prefix identifies the submit button.

Do not place more than one submit button in a Form which name attribute starts with _event_. The AbstractMEHttpServlet will return the first object it finds in the Form, which name starts with _event_ . This may not be the object that sent the event. To avoid that problem, put every submit button in its own Form.

 

JSP and Encoding

The JSP examples in the MDK are servlet - JSP examples. The servlet contains the "business logic" and the JSP represents the user interface. Whenever you pass a text string from the servlet to the JSP (for example, using a bean) that should be displayed on the web browser, the JSP sends this string to the web browser, without any transformation. The web browser itself interprets all received data as HTML. HTML uses "tags" (for example <b> to switch to bold characters) for text formatting. If your application sends a string that contains terms that could be interpreted, completely or partially, as HTML tag, the display on the web browser is incorrect.

To overcome this problem, HTML has "encoded" characters. For the W3C specification, see W3C HTML Document Representation for details. The AbstractMEHttpServlet class provides the method encodeForHtml() for encoding string according to the W3C standard.

Following table shows some characters and the equivalent HTML encoding.

Character

HTML encoded
> &gt;
< &lt;
€ (Euro currency symbol) &euro;


The persistence and generic sync examples of the MDK contain an encoding method. See Persistence Example 1, for more details.

JSP Programming Examples in the MDK

The JSP in the Example 2 of the "Getting Started" section demonstrates the usage of a gridlayout for better positioning of the graphical user interface elements and places an input field and a button in the grid layout

Example 2: welcome.jsp

The tableView.jsp of Example 2 demonstrates a layout to represent data in a tabular form. It uses variables supplied by a bean to keep the rows and columns of the table flexible.

Example 2: tableView.jsp

The menu.jsp in the persistence example uses a table structure similar to tableView.jsp but has additional checkboxes in the first row.

Persistence example: menu.jsp

The menu.jsp in the persistence query/sort example uses a table structure similar to tableView.jsp but has additional checkboxes in the first row. The servlet sets up a URL for two column headers, so that the events can be captured when the user clicks on one of these column headers..

Persistence query/sort example: menu.jsp
Persistence query/sort example implementation of the servlet: PersistenceExample.java

Information about JSP

You find JSP syntax basics in document JSP Basics. To learn about JSP you can use one of the following links:

Provided by

Link
Sun Microsystems Inc. http://java.sun.com/products/jsp
JSP Source http://www.jspin.com/
JSP Insider http://www.jspinsider.com/index.jsp
Johns Hopkins University http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
Apache Jakarta Project http://jakarta.apache.org/tomcat/