meljun cortes jedi course notes mobile application devt-lesson07-j2 me and enterprise computing

Download MELJUN CORTES Jedi course notes mobile application devt-lesson07-j2 me and enterprise computing

If you can't read please download the document

Upload: meljun-cortes

Post on 26-Jun-2015

524 views

Category:

Documents


1 download

DESCRIPTION

MELJUN CORTES Jedi course notes mobile application devt-lesson07-j2 me and enterprise computing

TRANSCRIPT

  • 1. J.E.D.I. J2ME and Enterprise Computing1 ObjectivesIn this section, we will be learning how to write Servlets, use JSP and JSTL, access adatabase using JDBC and create and parse XML documents.After finishing this lesson, the student should be able to: write simple Servlets write simple JavaServer Pages (JSP) using JSTL write JSTL code using Expression Language (EL) access a database using JDBC generate XML parse the XML on the Mobile client2 ServletsA servlet is a server-side Java class for implementing a request-response typetransaction. With servlets, Java provides a better and portable alternative to CGI(Common Gateway Interface) scripts. Compared to CGI scripts (written in Perl, sheelscripts, or other scripting languages), Java servlets provide a scalable, platformdependent technology that deliver dynamic content.A servlet has two methods for processing a request, the doGet() and doPost() methods.These methods are called when the web client (browser) issues a "GET" or "POST"command. These methods have identical parameters. These parameters are normallyhanded off to a common method so that both GET and POST request are handled thesame way. protected void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {// process request ... }Mobile Application Development 1

2. J.E.D.I. protected void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {// process request ... }import java.io.*;import java.net.*;import javax.servlet.*;import javax.servlet.http.*;public class SampleServlet extends HttpServlet {protected void processRequest( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println(""); out.println(""); out.println("Servlet SampleServlet"); out.println(""); out.println(""); out.println("Servlet SampleServlet at " + request.getContextPath () + ""); out.println(""); out.println(""); out.close();}Mobile Application Development 2 3. J.E.D.I.protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {processRequest(request, response);}protected void doPost( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {processRequest(request, response);}/** Returns a short description of the servlet. */public String getServletInfo() {return "Short description";}}Mobile Application Development 3 4. J.E.D.I.To create a new Web Project, click File -> New Project... ->Web: Web Application. Selectand input a name for your project and click finish.Mobile Application Development 4 5. J.E.D.I.To create a new Servlet, click on File -> New File.. -> Web: ServletMobile Application Development 5 6. J.E.D.I.3 JSP/JSTLThe primary goal of the JavaServer Pages Standard Tag Library (JSTL) is to help authorssimplify scripting on JSP pages. JSTL briges the gap between programmers and (non-programmer) page authors by providing a simple expression language for JSP pages.Aside from the expression language support actions and crontrol flow actions, JSTL alsoprovides functionality for accessing URL-based resources, internationalization andnumber and date formatting, database access and XML processing.JTSL is composed of several tag libraries, grouped by functional area. Area PrefixURI Example code snippetcore c http://java.sun.com/jstl/core I18N formattingfmt http://java.sun.com/jstl/fmt