1 / 11

Mobile Networking with S ervlet

Mobile Networking with S ervlet. Khanh Le. Basic Architecture. User requests information from an Application (e.g. MyServlet). Web Server launches MyServlet program and sends it parameters the MIDlet requested. MIDP Device. Internet. Web Server. Web server passes output

jamal
Download Presentation

Mobile Networking with S ervlet

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mobile Networking with Servlet Khanh Le

  2. Basic Architecture User requests information from an Application (e.g. MyServlet) Web Server launches MyServlet program and sends it parameters the MIDlet requested MIDP Device Internet Web Server Web server passes output from MyServlet back to the MIDlet Web Server retrieves output from the MyServlet

  3. What is needed ? • MIDlet & MIDP Device • Servlet & Web Server • Connection between MIDP Device & Web Server Common Protocol between MIDlet and Servlet

  4. Example MIDlet import java.io.*; import javax.microedition.io.*; import javax.microedition.lcdui.*; import javax.microedition.midlet.*; public class FirstMidletServlet extends MIDlet {private Display display; String url ="http://localhost:8080/midp/test";public FirstMidletServlet() { display = Display.getDisplay(this); } public void startApp() { try { invokeServlet(url); } catch (IOException e) { System.out.println("IOException " + e); } }

  5. Example MIDlet cont. public void pauseApp() { } public void destroyApp(boolean unconditional) { } void invokeServlet(String url) throws IOException { HttpConnection c = null; InputStream is = null; StringBuffer b = new StringBuffer(); TextBox t = null; try { c = (HttpConnection)Connector.open(url); c.setRequestMethod(HttpConnection.GET); c.setRequestProperty("IF-Modified-Since", "20 Jan 2001 16:19:14 GMT"); c.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0"); c.setRequestProperty("Content-Language", "en-CA"); is = c.openDataInputStream();

  6. Example MIDlet cont int ch; while ((ch = is.read()) != -1) { b.append((char) ch); }t = new TextBox("First Servlet", b.toString(), 1024, 0); } finally { if(is!= null) { is.close(); } if(c != null) { c.close(); } } display.setCurrent(t); } }

  7. What is Servlet • A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed via a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by Web servers. For such applications, Java Servlet technology defines HTTP-specific servlet classes.

  8. Servlet Packages • The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines life-cycle methods. • When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services

  9. Example Servlet import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/plain"); PrintWriter out = response.getWriter(); out.println("Servlet invoked!"); out.println(new Date()); } }

  10. Web Server • Different options, like • Tomcat • JSDK (Java Servlet Development Kit) • Commercial Tools based on J2EE • IBM WebSphere • BEA Weblogic • Try web based tutorial: http://developers.sun.com/techtopics/mobility/midp/articles/tutorial2/

  11. Servlet in Tomcat Environment • Write Source code • Compile (javac MyServlet.java) • remember classpath variable: • set CLASSPATH=c:\Apache Tomcat 4.0\common\lib\servlet.jar • Configure Tomcat • Edit conf\server.xml • Create web.xml • Start Tomcat (\bin\startup) • Test connection (open web browser, connect http://localhost:8080) • Test Servlet (open web browser, connect http://localhost:8080/midp/test) • Test MIDlet & Servlet • Stop Tomcat (\bin\shutdown)

More Related