Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.

Similar presentations


Presentation on theme: "Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML."— Presentation transcript:

1 Bayu Priyambadha, S.Kom

2 Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML to Browser 2. Web Server reads file from disk browser Web Server

3 Dynamic Content  CGI( Common Gateway Interface )program generates HTML that is returned to Browser 1. Browser sends request to Web Server 5. Web Server sends HTML to Browser User Computer Server 2. Web Server loads CGI program from disk CGI Program 3. Web Server starts CGI program 4. CGI program generates and returns HTML browser Web Server

4 CGI has issues  performance Web Server must create new process for each request, limits scalability  maintenance presentation and business logic tightly coupled

5 Alternatives  FastCGI - persistent process  mod_perl - perl interpreter embedded in Apache web server  Server Extensions - Netscape(NSAPI), Microsoft(ISAPI)  Active Server Pages

6 Java Servlets  replaces CGI  a Java program  runs in Servlet Container or Engine  generates dynamic content(HTML, XML)  now part of J2EE architecture  good performance

7 Java Servlets Continued...  Advantages generally faster, more efficient than CGI runs as a thread not an OS process Convenience - Java API’s secure - does not run in a shell portable, vendor independent

8 Java Servlet Diagram  Extends Web Server 1. Browser sends request to Web Server 6. Web Server sends HTML to Browser 2. Web Server sends request to Servlet Engine Servlet 5. Servlet generates and returns HTML browser Web Server Servlet Container DB TopLink 4. Servlet can access database jdbc 3. Servlet Engine runs the servlet

9 Simple Servlet  public class HelloWorld extends HttpServlet { public void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response. setContentType(" text/ html"); PrintWriter out = response. getWriter(); out. println( " " ); out. println( " Hello World ”); out. println( " ”); out. println( " Hello World ”); out. println( " "); }

10 Servlet Issues  servlet generating HTML results in presentation and business logic tightly coupled with accompanying maintenance issues.

11 Java Server Pages(JSP)  Built on Servlet technology  simplified way to create pages containing dynamically generated content.  converted into servlet, compiled and run as a normal servlet  timestamp checked at specified interval, regenerated if necessary.

12 Java Server Pages(JSP) continued...  jsp page contains directives, comments, fixed template data (HTML, XML), jsp expression or tags, scriptlets

13 Java Server Pages(JSP) continued...  Can be invoked from a browser via URL or from a Servlet  Java code can be in the jsp source keep simple  Java code can be in a Java Bean separates User Interface from Business Logic Used via UseBean tag

14 Java Server Page Diagram  1. Browser sends request to Web Server 6. Web Server sends HTML to Browser 2. Web Server sends request to Servlet Engine Servlet 6. JSP(Servlet) returns HTML browser Web Server Servlet Container DB TopLink 5. JSP interacts with Java Bean(s) and/or Servlets jdbc 4. Servlet Container runs the JSP (Servlet) jsp JSP Engine Servlet source code Java Compiler JSP (Servlet) Java Bean 3. JSP Engine generates servlet

15 JSP scripting elements  There is more than one type of JSP “tag,” depending on what you want done with the Java  The expression is evaluated and the result is inserted into the HTML page  The code is inserted into the servlet's service method If code contains declarations, they become local variables of the service method This construction is called a scriptlet  The declarations are inserted into the servlet class, not into a method You can declare instance variables and/or static variables You can declare instance methods and/or static methods 15

16 Example JSP  Hello! The time is now  Notes: The tag is used, because we are computing a value and inserting it into the HTML The fully qualified name ( java.util.Date ) is used, instead of the short name ( Date ), because we haven’t yet talked about how to do import declarations 16

17 Variables  You can declare your own variables, as usual  JSP provides several predefined variables request : The HttpServletRequest parameter response : The HttpServletResponse parameter session : The HttpSession associated with the request, or null if there is none out : A JspWriter (like a PrintWriter ) used to send output to the client  Example: Your hostname: 17

18 Scriptlets  Scriptlets are enclosed in tags Scriptlets are executable code and do not directly affect the HTML Scriptlets may write into the HTML with out.print( value ) and out.println( value ) Example:  Scriptlets are inserted into the servlet exactly as written, and are not compiled until the entire servlet is compiled Example: Have a nice day! Have a lousy day! 18

19 Declarations  Use for declarations to be added to your servlet class, not to any particular method Caution: Servlets are multithreaded, so nonlocal variables must be handled with extreme care If declared with, variables are local and OK If declared with, variables probably need to be synchronized Data can also safely be put in the request or session objects  Example: Accesses to page since server reboot:  You can use to declare methods as easily as to declare variables 19

20 Directives  Directives affect the servlet class itself  A directive has the form: or  The most useful directive is page, which lets you import classes and packages Example: 20

21 The include directive  The include directive inserts another file into the file being parsed The included file is treated as just more JSP, hence it can include static HTML, scripting elements, actions, and directives  Syntax: The URL is treated as relative to the JSP page If the URL begins with a slash, it is treated as relative to the home directory of the Web server  The include directive is especially useful for inserting things like navigation bars 21

22 Actions  Actions are XML-syntax tags used to control the servlet engine  Inserts the indicated relative URL at execution time (not at compile time, like the include directive does) This is great for rapidly changing data  " /> Jump to the (static) URL or the (dynamically computed) JavaExpression resulting in a URL 22

23 JSP in XML  JSP can be embedded in XML as well as in HTML  Due to XML’s syntax rules, the tags must be different (but they do the same things)  HTML: XML: expression  HTML: XML: code  HTML: XML: declarations  HTML: XML: 23

24 Comments  You can put two kinds of comments in JSP: ○ This is an ordinary HTML comment, and forms part of the page that you send to the user ○ Hence, the user can see it by doing View source ○ JSP scriptlets in HTML comments will be executed ○ This kind of comment will be stripped out when the JSP is compiled ○ It’s intended for page developers; the user will never see it 24

25 Deployment Directories

26 <% Out.println(“Thank you....”); %>


Download ppt "Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML."

Similar presentations


Ads by Google