Servlet, web application AdvJ Session1,2 –Servlet, Web application 41 Session 1, Introduction to Basic Java Web Application AdvJ Session1,2 –Servlet, Web application AdvJ Session1,2 –Servlet, Web application 41 Objectives Introduction to java web application + The core and basic of Java web server technologies + Web design vs server technologies Setup Environment JDK 1 7 or higher Servlet container Tomcat 7, Glassfish 4 1, etc Intergrate Netbeans 8 with the web container CreatingBuilding th.
Session 1, Introduction to Basic Java Web Application AdvJ Session1,2 –Servlet, Web application 1/41 Objectives Introduction to java web application + The core and basic of Java web server technologies + Web design vs server technologies Setup Environment : JDK: 1.7 or higher Servlet container: Tomcat 7, Glassfish 4.1, etc Intergrate Netbeans with the web container Creating/Building the first application: Learn to create Servlet Learn to create JSP Deploy the web application AdvJ Session1,2 –Servlet, Web application 2/41 What is HTML? HTML is a language for describing web pages • HTML stands for Hyper Text Markup Language • HTML is not a programming language, it is a markup language • A markup language is a set of markup tags • HTML uses markup tags to describe web pages AdvJ Session1,2 –Servlet, Web application 3/41 HTML Tags HTML markup tags are usually called HTML tags • HTML tags are keywords surrounded by angle brackets like • HTML tags normally come in pairs like and • The first tag in a pair is the start tag, the second tag is the end tag • Start and end tags are also called opening tags and closing tags AdvJ Session1,2 –Servlet, Web application 4/41 HTML Documents = Web Pages • HTML documents describe web pages • HTML documents contain HTML tags and plain text • HTML documents are also called web pages AdvJ Session1,2 –Servlet, Web application 5/41 Web browser • The purpose of a web browser (like Internet Explorer or Firefox) is to read HTML documents and display them as web pages • The browser does not display the HTML tags, but uses the tags to interpret the content of the page AdvJ Session1,2 –Servlet, Web application 6/41 Web page example My First HeadingMy first paragraph
place Studying AdvJ Session1,2 –Servlet, Web application 7/41 What a Servlet is • Servlets are small Java programs that run on a Web server and help to build dynamic Web pages • Servlets receive and respond to requests from Web clients, usually across HTTP, the HyperText Transfer Protocol • Java Servlet technology was created as a portable way to provide dynamic, user-oriented content AdvJ Session1,2 –Servlet, Web application 8/41 What a Servlet is Browser Browser Client Client Request Internet HTTP Protocol Response Web Web Server Server ServletContainer Servlets Database Database AdvJ Session1,2 –Servlet, Web application 9/41 Architecture of the Servlet Package • The javax.servlet package provides interfaces and classes for writing servlets • When a servlet accepts a call from a client, it receives two objects: – ServletRequest, which encapsulates the communication from the client to the server – ServletResponse, which encapsulates the communication from the servlet to the client AdvJ Session1,2 –Servlet, Web application 10/41 The Form's Action Attribute and the Submit Button… User name: Password: AdvJ Session1,2 –Servlet, Web application 29/41 The Form's Action Attribute and the Submit Button… AdvJ Session1,2 –Servlet, Web application 30/41 Form Data • Call getParameter method of the HttpServletRequest, supplying the parameter name as an argument public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //get form data String u =request.getParameter("user"); String p=request.getParameter("pass"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(""); out.println("You sent me:"); out.println(u+""+p); out.println(""); } AdvJ Session1,2 –Servlet, Web application 31/41 Web application • A web application or webapp is an application that is accessed via web browser over a network such as the Internet or an intranet It is also a computer software application that is coded in a browser-supported language (such as HTML, JavaScript, Java, etc.) and reliant on a common web browser to render the application executable • Web applications are popular due to the ubiquity of web browsers, and the convenience of using a web browser as a client, sometimes called a thin client AdvJ Session1,2 –Servlet, Web application 32/41 File and Directory Structure • A Place for Everything and Everything in Its Place • Construct the file and directory structure of a Web Application that may contain: – – – – – – – static content, JSP pages, servlet classes, the deployment descriptor, tag libraries, JAR files and Java class files; and describe how to protect resource fi les from HTTP access AdvJ Session1,2 –Servlet, Web application 33/41 Special Directories Beneath the Context Root • /WEB-INF/classes—for classes that exist as separate Java classes (not packaged within JAR files) These might be servlets or other support classes • /WEB-INF/ lib—for JAR fi les These can contain anything at all—the main servlets for your application, supporting classes that connect to databases—whatever • / WEB-INF itself is the home for an absolutely crucial file called web.xml, the deployment descriptor file AdvJ Session1,2 –Servlet, Web application 34/41 Deployment Descriptor Elements • The first thing to note about the deployment descriptor file is that it’s an XML file Given that the name is web.xml AdvJ Session1,2 –Servlet, Web application 35/41 Overall Structure of the Deployment Descriptor AdvJ Session1,2 –Servlet, Web application 36/41 and Its Important Subelements 06/20/22 AdvJ Session1,2 –Servlet, Web application 37/41 Deployment Descriptor simple web.xml Servlet 2.4 Examples Servlet 2.4 Examples FirstServlet FirstServlet FirstServlet /FirstServlet AdvJ Session1,2 –Servlet, Web application 38/41 Welcome Files index.html index.jsp mainlibrary/catalog.jsp AdvJ Session1,2 –Servlet, Web application 39/41 Packaging Your Web Application • A WAR Is Not a JAR – Although a WAR fi le can be produced in the same way as a JAR fi le, and has the same underlying fi le format, it is different The most obvious difference is the file extension naming convention: jar for Java ARchive, and war for Web (Application) ARchive – WARs are packaged for a different purpose: to make it as easy as possible for a web container to deploy an application AdvJ Session1,2 –Servlet, Web application 40/41 WAR file • Several web containers have automatic deployment mechanisms • The server recommended for this course—Tomcat 7.x or Glassfish 4.x—has a “webapps” directory • Place a WAR file in this directory, and Tomcat (by default) will un-jar the contents into the file system under the webapps directory • a context root directory is the same name as the WAR file (but without the war extension) — then makes the application available for use AdvJ Session1,2 –Servlet, Web application 41/41 War file demo Demo\WarFile\FirstServlet.war AdvJ Session1,2 –Servlet, Web application 42/41 Summary HTML Introduction • What is HTML? • HTML Tags • HTML Documents = Web Pages • Web browser • Example Servlets • What a Servlet is and how you can use one • How to define and write servlets • Basic Servlet Structure • Request / Response Headers • Handling Form Data • Java Servlet Specification • Jakarta-tomcat-7.x or Glassfish 4.x Web application • File and Directory Structure • Deployment Descriptor Elements • WAR Files AdvJ Session1,2 –Servlet, Web application 43/41 ... Introduction to java web application + The core and basic of Java web server technologies + Web design vs server technologies Setup Environment : JDK: 1.7 or higher Servlet container: Tomcat 7, Glassfish... –Servlet, Web application 7/41 What a Servlet is • Servlets are small Java programs that run on a Web server and help to build dynamic Web pages • Servlets receive and respond to requests from Web. .. (such as HTML, JavaScript, Java, etc.) and reliant on a common web browser to render the application executable • Web applications are popular due to the ubiquity of web browsers, and the convenience