Presentation is loading. Please wait.

Presentation is loading. Please wait.

April 20023CSG11 Electronic Commerce Java (2) John Wordsworth Department of Computer Science The University of Reading Room 129,

Similar presentations


Presentation on theme: "April 20023CSG11 Electronic Commerce Java (2) John Wordsworth Department of Computer Science The University of Reading Room 129,"— Presentation transcript:

1 April 20023CSG11 Electronic Commerce Java (2) John Wordsworth Department of Computer Science The University of Reading J.B.Wordsworth@rdg.ac.uk Room 129, Ext 6544

2 April 20023CSG12 Lecture objectives Describe the use of applets for client-side programming Describe the use of servlets for server-side programming Describe the use of JavaServer Pages for creating HTML Describe the use of cookies for storing persistent information about a user

3 April 20023CSG13 Applets An applet is a Java progam loaded by a browser from a Web site. Here comes my applet: This text comes after the applet. The browser opens a window of the specified size for the applet to use. Nothing to install, but slow to load. Cannot access local disk.

4 April 20023CSG14 A sample applet import java.awt.*; import java.applet.*; public class MyApplet extends Applet { public void paint (Graphics g) { g.setColor (Color.pink); g.fillRect (15, 15, 170, 170); g.setColor (Color.black); g.drawString ("MY APPLET", 50, 45);} } Test with appletviewer, or with a Web browser.

5 April 20023CSG15 Applet methods init: when the applet is started by the browser start: whenever the applet's display area comes into view in the window stop: whenever the applet's display area moves out of view destroy: when the browser moves elsewhere paint: to put something in the graphics window Others: …

6 April 20023CSG16 HTTP requests and responses methodURIHTTP version body HTTP versionstatus codereason phrase body Request Response http://www.solent.ac.uk/courseinfo.asp?courseid=256& coursetype=Undergraduate&parttime=yes&yearofentry=2

7 April 20023CSG17 Servlets browser server TCPIPTCPIP 80 application server servlet

8 April 20023CSG18 The servlet interface public interface Servlet { public void init (ServletConfig config) throws ServletException; public ServletConfig getServletConfig (); public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException; public String getServletInfo (); public void destroy ();}

9 April 20023CSG19 A sample HTTP servlet import javax.servlet.*; import javax.servlet.http.*; import javax.io.*; public class ServletsRule extends HttpServlet { public void service (HttpServletRequest req, HttpServletResponse res) throws IOException { int i = 0; res.setContentType ("text/html"); PrintWriter out = res.getWriter (); out.print(" "); out.print("A server-side strategy"); out.print(" "); out.print(" Servlets rule! " + i++); out.print(" "); out.close (); }

10 April 20023CSG110 Cookies A cookie is a text file created by the server and saved on the client by the browser When the browser moves to the associated URL, the cookie is sent in the HTTP response. Cookie choc = new Cookie ("mycookie" "value"); res.addCookie (choc); Cookie [] cookies = req.getCookies

11 April 20023CSG111 JavaServer Pages A mixture of static HTML and Java-like code. Compiled to produce a servlet that creates the HTML for a response. For the latest information see Recent news

12 April 20023CSG112 Key points Java applets allow client-side programming. Java servlets allow server-side programming. Servlets are started by an HTTP request, to which they have access, and can construct an HTTP response. Servlets can acess and construct cookies. JavaServer Pages are used to create HTML pages for the response.


Download ppt "April 20023CSG11 Electronic Commerce Java (2) John Wordsworth Department of Computer Science The University of Reading Room 129,"

Similar presentations


Ads by Google