web server programming 1. nuts and bolts. premises of course provides general introduction, no...

18
Web Server Programming 1. Nuts and Bolts

Upload: arleen-wilson

Post on 30-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some

Web Server Programming

1. Nuts and Bolts

Page 2: Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some

Premises of Course

• Provides general introduction, no in-depth training• Assumes some HTML knowledge• Assumes some programming experience• Assumes some knowledge of object orientation• Only treats JAVA applications. Alternatives:

– Perl, (PHP)

– XML

Page 3: Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some

Web Services

• Document level– XML + Style sheets– Client-side programming (javascript, applets)– Server-side programming:

• CGI

• JAVA servlets

• HTML++ / XML++ (SSI: php, asp, jsp)

• RPCs

Page 4: Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some

Contents of this course

• Web technology: protocols & languages

• Web server: request handling, output generation, session & user management

• Server programming:– Java servlets– JSP

Page 5: Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some

What This Course Is NOT About

• RPCs

• Use of XML for (enterprise) web services

• Layout & content management

• Use of external components (databases, etc.)

• Client based software (applets, etc.)

Page 6: Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some

About the Web

• Reinier Post

Page 7: Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some

JAVA Support

• Component based

• Platform independent

• Built-in security

• Software libraries for every application

Page 8: Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some

Web Applications

• Cooperating parts:– (HTML) documents– Servlets– JSP pages– Applets (client side software)

• Common context– Properties (appl. param., configuration data)– Resources (shared data)

Page 9: Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some

JAVA servlets

Alternative for CGI scripts:

• JAVA technology

• Servlet support integrated in server– JAVA threads: no independent process– Sharing of resources

• Protocol independent

• Security issues: JAVA sandbox

Page 10: Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some

JSP Pages

• Based on HTML or XML

• Contain processing tags

• Use servlet classes

• Converted to servlet

Page 11: Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some

What About Servlets?

• Standard for interfacing web client with web server.

• Set of JAVA packages (javax.servlet, javax.servlet.http)

• Way of interfacing clients with back-end services (JDBC servers, etc.)

• Way to implement proxies (e.g. for applets)

Page 12: Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some

Servlet API

• Interface Servlet

• Interface ServletConfig

• Interface ServletContext

• Interface ServletRequest

• Interface ServletResponse

Page 13: Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some

Interface Servlet

• Init()

• Service()

• Destroy()

• getServletConfig()

• getServletInfo()

Page 14: Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some

Class HttpServlet

• Inherits from GenericServlet (impl. Servlet)• Additional methods:

– doGet()– doPost()– doHead()– doDelete()– doOptions()– doTrace()

Page 15: Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some

Requests and Responses

• Interface HttpServletRequest– Request as stream

– HTTP headers

– Parameters

– Path info

• Interface HttpServletResponse– Output stream

– HTTP headers

– Cookies

Page 16: Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some

Example: TestServlet.java

• Implements doGet() and doPost() using common (private) function doIt()

• Analyses client request and shows:– Servlet requested– Path info– Get and post parameters

Page 17: Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some

public class TestServlet extends HttpServlet { private static final String CONTENT_TYPE = "text/html"; private PrintWriter out; //Initialize global variables public void init() throws ServletException { } //Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException { doIt(request, response); } //Process the HTTP Post request public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException { doIt(request, response); } private void doIt(HttpServletRequest req, HttpServletResponse res) throws

ServletException, IOException { res.setContentType(CONTENT_TYPE); out = res.getWriter(); out.println("<html>"); .. out.println("</html>"); } //Clean up resources public void destroy() { }}

Page 18: Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some

Assignment

• Modify TestServlet to show the current date.

• Study the servlet API documentation.

• Modify TestServlet to show initial parameters, and provide some of those.