Presentation - University of Reading
Download
Report
Transcript Presentation - University of Reading
Electronic Commerce
Java (2)
John Wordsworth
Department of Computer Science
The University of Reading
[email protected]
Room 129, Ext 6544
April 2002
3CSG1
1
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
April 2002
3CSG1
2
Applets
An applet is a Java progam loaded by a browser from a Web
site.
<p>Here comes my applet:</p>
<applet code="MyApplet.class" width="200"
height="200">
</applet>
<p>This text comes after the applet.</p>
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.
April 2002
3CSG1
3
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.
April 2002
3CSG1
4
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: …
April 2002
3CSG1
5
HTTP requests and responses
Request
method
URI
body
HTTP version
http://www.solent.ac.uk/courseinfo.asp?courseid=256&
coursetype=Undergraduate&parttime=yes&yearofentry=2
Response
HTTP version
April 2002
status code
body
3CSG1
reason phrase
6
Servlets
browser
server
80
application server
T
C
P
servlet
I
P
April 2002
3CSG1
7
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 ();}
April 2002
3CSG1
8
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("<HEAD><TITLE>");
out.print("A server-side strategy");
out.print("</TITLE></HEAD><BODY>");
out.print("<H1>Servlets rule! " + i++);
out.print("</H1></BODY>");
out.close (); }
April 2002
3CSG1
9
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
April 2002
3CSG1
10
JavaServer Pages
A mixture of static HTML and Java-like code.
Compiled to produce a servlet that creates the HTML for
a response.
<p>For the latest information
<%= new java.util.Date () %>
see <a href="latest.html">Recent news</a></p>
April 2002
3CSG1
11
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.
April 2002
3CSG1
12