Web Server Programming 1. Nuts and Bolts
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
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
Contents of this course Web technology: protocols & languages Web server: request handling, output generation, session & user management Server programming: –Java servlets –JSP
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.)
About the Web Reinier Post
JAVA Support Component based Platform independent Built-in security Software libraries for every application
Web Applications Cooperating parts: –(HTML) documents –Servlets –JSP pages –Applets (client side software) Common context –Properties (appl. param., configuration data) –Resources (shared data)
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
JSP Pages Based on HTML or XML Contain processing tags Use servlet classes Converted to servlet
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)
Servlet API Interface Servlet Interface ServletConfig Interface ServletContext Interface ServletRequest Interface ServletResponse
Interface Servlet Init() Service() Destroy() getServletConfig() getServletInfo()
Class HttpServlet Inherits from GenericServlet (impl. Servlet) Additional methods: –doGet() –doPost() –doHead() –doDelete() –doOptions() –doTrace()
Requests and Responses Interface HttpServletRequest –Request as stream –HTTP headers –Parameters –Path info Interface HttpServletResponse –Output stream –HTTP headers –Cookies
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
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(" ");.. out.println(" "); } //Clean up resources public void destroy() { }
Assignment Modify TestServlet to show the current date. Study the servlet API documentation. Modify TestServlet to show initial parameters, and provide some of those.