SSC - Communication and Networking SSC - Web applications and development Introduction to Java Servlet (I) Shan He School for Computational Science University of Birmingham Module 06-19321: SSC SSC - Communication and Networking Outline Outline of Topics What will we learn Web development Java servlet Java servlet: our first example SSC - Communication and Networking What will we learn What is web applications and development? I Java Servlet: basic concepts, configure, install and use servlet based web applications, basic implementation. I Session management and Servlet with JDBC I Model View Controller (MVC) for Java Servlet and Java Serve Pages I Advanced topics: Multithreaded Servlet SSC - Communication and Networking Web development What is web applications and development? I Web-based application: an application that uses a web browser as a client, e.g., google calendar or GMail. I Web development: work involved in developing a web site for the Internet or intranet, which include: I web design I web content development I client-side/server-side coding I Web development coding (platforms or languages): I Client side: HTML5, JavaScript, Ajax (Asynchronous JavaScript), Flash, JavaFX, etc. I Server side: PHP, Python, Node.js, Java servlet I Client-side/Server-side: Google Web Toolkit, Opa I Full stack web frameworks { built on the development platforms wtih a higher level set of functionality: Meteor, Yahoo! Mojito, MEAN SSC - Communication and Networking Java servlet What is Java servlet? I Java servlet: a Java platform technology \for extending the functionality of a Web server and for accessing existing business systems", part of the Java Enterprise Edition (Java EE) I Component-based, server/platform-independent method for building Web-based applications I Can use entire family of Java APIs, also can use existing POJOs (Plain Old Java Objects) I Using Java servlet, you can generate dynamic web pages based on: I data submitted by the user, e.g., results pages from search engines and Google calendar I the data changes frequent, e.g., news headlines page or weather report I information from corporate databases or other such sources, e.g., online store such as Amazon SSC - Communication and Networking Java servlet Why use Java servlet? I Efficient: the dynamic web pages are easier to write and faster to run since they are run in compiled Java code. I Convenient: you can use your familiar language Java I Powerful: Java servlets provide all the powerfull features of Java, such as multi-threads, exception handling and garbage collection. You can communicate with different Java servlets and servers. I Portable: Servlets are written in Java and can run across Web Servers without changes on Apache or Microsoft IIS. I Inexpensive: Running servlets only requires Java, Apache Tomcat, which are all free. SSC - Communication and Networking Java servlet Java servlet vs other Web development script I Java vs PHP vs Perl vs Python I Note: Java is statically typed language: types are associated with a variable declaration I Note: While PHP/Perl/Python are dynamically typed language, e.g., it does not associate values strictly with a specific type I Advantages: I Statically typed language ensures earlier detection of errors during compiling I Many third party libraries for Java Web development I Modern Java Web Development frameworks such as Java Enterprise Edition and Spring are robust and convenient I Java provide power multi-threaded features I Java is a compiled programming language which is faster than PHP and other interpreted languages SSC - Communication and Networking Java servlet Java servlet life cycle I Java Servlets are run inside a Servlet compatible "Servlet Container", e.g., Apache Tomcat, JBoss, Jetty, etc. I A Servlets's life cycle consists of the following steps: I Step 1: Load Servlet Class. I Step 2: Create Instance of Servlet. I Step 3: Call the servlets init() method. I Step 4: Call the servlets service() method. I Step 5: Call the servlets destroy() method. I Note 1: By default, a servlet is not loaded until the first request is received for it. I Note 2: Multiple requests to the same servlet are executed simultaneously in multiple threads I Note 3: When the servlet container unloads the servlet, destroy() method is called and the container finalises the servlet and collect garbage. SSC - Communication and Networking Java servlet Java servlet life cycle Servlet Container Load class Instantiation Instantiation Instantiation init() Response service() Request destroy() Finalisation and garbage collection SSC - Communication and Networking Java servlet: our first example How to set up your programming IDE I What are required? I Java EE I A Servlet Container, e.g., Apache Tomcat I You need to read how to run tomcat from school's support web I I am using Eclipse to show you how to build your first servlet but you can also use NetBean. See a tutorial here and our School's own tutorial SSC - Communication and Networking Java servlet: our first example Creating your first servlet I Create a new \Dynamic Web project": File menu −! New −! Dynamic Web Project I Enter the project name I Choose \Target Runtime", e.g., Tomcat X.x I Choose \Dynamic web module version" depends on your Servlet API version (Servlet 3.0 for Java EE6+ and Tomcat 7) I Tick \Generate web.xml deployment descriptor" if you are NOT using Servlet 3.0 (I will explain this tomorrow) I Create a new \Sevlet" which will extend HttpServlet class I Write methods for each HTTP method (GET, POST etc.) SSC - Communication and Networking Java servlet: our first example Java servlet explained: HTTP methods I Recalled what we learned: HTTP (Hypertext Transfer Protocol) protocol has the following methods: GET(), POST(), HEAD(), PUT(), DELETE() , etc. I Two methods for a request-response between a client and server: I GET() : Requests data from a specified resource (server) I POST() : Submits data to be processed to a specified resource (server) I Note: In a GET request, query strings (name/value pairs) is sent in the URL, which can be cached, bookmarked, remained in the browser history, have length restrictions I GET requests should be used only to retrieve non-sensitive data I Comparison between GET and POST SSC - Communication and Networking Java servlet: our first example Java servlet explained: HttpServlet class I In Java, your servlet classes extend the HttpServlet abstract class I Two most important methods: I doGet() : to respond HTTP GET requests only I doPost() : to respond HTTP POST requests only I From one method you can call the other methods to handle both GET and POST requests I Both methods required two parameters: doGet(HttpServletRequest request, HttpServletResponse response) I Two parameters are two classes with a lot of methods to process HTTP requests and responses SSC - Communication and Networking Java servlet: our first example Java servlet explained: HttpRequest class I Provides request information for HTTP servlets. I Request Parameters: sent as part of the URL (in the "query string"), or as part of the body of an HTTP request. http://georgewbush.com/mytopsecrets.html? Action=showme&Document=NuclearLaunchCodes I A query string of two parameters with parameter values: I Action=showme I Document=NuclearLaunchCodes I getParameterNames : Returns the value of a request parameter as a String , or null if the parameter does not exist. I getParameter : Returns an Enumeration of String objects containing the names of the parameters in the "query string". SSC - Communication and Networking Java servlet: our first example Java servlet explained: HttpResponse class I Provides HTTP-specific functionality in sending a response I setHeader : Set HTTP Header, e.g., response.setHeader("Header-Name", "Header Value"); I getWriter() : Returns a PrintWriter from the HttpResponse object, so the server can send character text to the client. I Use PrintWriter to send plain text or HTML: PrintWriter writer = response.getWriter(); writer.write("<html><body>HTML reponse</body></html>"); I getOutputStream : Returns a ServletOutputStream for writing binary data in the response. I sendRedirect : Sends a temporary redirect response to the client using the specified redirect location URL.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages15 Page
-
File Size-