1 / 22

Java Server Pages

Java Server Pages. B.Ramamurthy. Topics for Discussion. Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP fundamentals JSP Examples with Netbeans Finish the semantic web topic. Java Server Pages.

jrowland
Download Presentation

Java Server Pages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Server Pages B.Ramamurthy

  2. Topics for Discussion Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP fundamentals JSP Examples with Netbeans Finish the semantic web topic

  3. Java Server Pages • Servlets are pure Java programs. They introduce dynamism into web pages by using programmatic content. • JSP technology is an extension/wrapper over the Java servlet technology. • JSP are text based documents. • We will focus only on JSP since it subsumes the servlet technology. • Two major components of JSP: • Static content: provided by HTML or XML • Dynamic content: generated by JSP tags and scriplets written in Java language to encapsulate the application logic and XHTML.

  4. JSP compilation into Servlets JSP Initial request Web Server J2EE Web Container Web Browser translation Java Servlets Subseq request

  5. Servlets • Client-server model: Client requests something of a server and the server performs action(s) and responds to the client. • This request-response model is the highest level of networking in Java. • A servlet extends the functionality of a server. • javax.servlet and javax.servlet.http package provide the classes and interfaces to define servlets. B.Ramamurthy

  6. What are servlets? • Servlets are server side components/programs that are primarily designed to use with HTTP protocol. • ~ provide secure access to a Website and to interact with databases on behalf of a client. • ~ dynamically create HTML documents to be displayed by the browser. • ~ can maintain unique session information for each client. B.Ramamurthy

  7. Support for servlets • Servlets are to servers what applets are to a client (web browser). • Servlets are normally executed of a web server. • Many web servers support servlets: Glassfish, Apache Tomcat, IIS. B.Ramamurthy

  8. Servlet API • It is good programming practice to start the design/definition with an interface. Conversely interfaces are good starting point to study an API. • Servlet interface: void init (ServletConfig config) ServletConfig getServletConfig() void service (ServletRequest req, ServletResponse res) String getServletInfo() void destroy() B.Ramamurthy

  9. Servlet API • Servlet interface is implemented by two abstract classes GenericServlet and HTTPServlet. • HTTPServlet is for interfacing with HTTP protocol and we will look at only this in our discussion today. B.Ramamurthy

  10. HTTPServlet • Two most common HTTP requests are GET and POST. • GET request gets information from the server. For example: retrieve an image or document. • POST requests are to send to the server the information from a HTML form which a client enters. For example: request to search internet, query database, send authentication information. B.Ramamurthy

  11. HTTPServlet Methods service: This is called when a request arrives at a server. This method in turn calls doXYZ. doGet : responds to HTTP GET ; get information to browser doPOST: responds to HTTP POST : send request to server doDelete : responds to HTTP DELETE : to delete a file on server! doOptions : responds to HTTP OPTIONS doPut : responds to HTTP PUT : to save a file doTrace: called in response to HTTP TRACE for debugging purposes B.Ramamurthy

  12. Your Servelet • Your servlet will typically extend class HTTPServlet. • You will override doGet, doPost and anyother methods of HTTPServlet. • Place it n webpages/WEB-INF/servlets Example:/web/faculty/bina/webpages/WEB-INF/servlets • Html file acessing the servlet in your regular web pages home. B.Ramamurthy

  13. Servlet Semantics • The webserver that executes that executes a servlet creates an HTTPServletRequest object and passes it to the servlet’s service method which in turn passes it to doGet. • This object contains request from the client. • Webserver executes the request and creates HTTPServletResponse object and passes this to the client using doPOST. • The object contains response to the client’s request. B.Ramamurthy

  14. HTTPServletRequest String getParameter (String name) Enumeration getParameterNames() String[] getParameterValues(String name) Cookie[] getCookies() HttpSession getSession(boolean create) B.Ramamurthy

  15. HTTPServletResponse void addCookie(Cookie cookie) ServletOutputStream getOutputStream() PrintWriter getWriter() void setContentType(String type) B.Ramamurthy

  16. More on JSP syntax and contents • HTML code for user interface lay out • JSP tags: declarations, actions, directives, expressions, scriplets • JSP implicit objects: a request object, response object, session object, config object • Javabeans: for logic that can be taken care of at the JSP level. • We will examine only JSP tags here.

  17. JSP Tags • Declaration: variable declaration <%! int age = 56 %> • Directive: ex: import classes <%@ page import = “java.util.*” %> • Scriplet: Java code <% if password(“xyz”) { %> <H1> Welcome <\H1> • Expression: regular expression using variables and constants • <%= param[3]+4 %> • Action: <jsp:usebean name =“cart” class=“com.sun.java.cart”

  18. Java Server Pages by Examples • JSPs combine static markup (HTML, XML) with special dynamic scripting tags. • Each JSP is translated into a servlet the first time it is invoked. Then on, the requests are serviced by the servlets. • Lets understand the building blocks of a JSP, namely, directives, scripting elements, and actions through a series of examples.

  19. Examples: Directives • <%@ %> • A directive configures the code generation that container will perform in creating a servlet. • Simple JSP showing access to a Java API class Date: simple.jsp • Using Page directives to define various page attributes: pageDirective.jsp • Directive to include other JSPs: includeDirective1.jsp, includeDirective2.jsp

  20. Examples: Scripting Elements • Declaration: <%! %> • A declaration is a block of code in a JSP that is used to define class-wide variables and methods in the generated servlet. • Declaring a piece of code: declaration.jsp

  21. Examples: Scripting Elements (contd.) • Scriplets: <% %> • A scriplet is a block of Java code that is executed during the request-processing time. • See scriplet.jsp • Expressions: sends a value of a Java expression back to the client. • <%= %> • See expression.jsp

  22. Examples: Standard Actions • Standard actions are well known tags that affect the run time behavior of the JSP and the response sent back to the client. • Some commonly used tag actions types are: <jsp:useBean> <jsp:setProperty> <jsp:getProperty> <jsp:param> <jsp:include> <jsp:forward> <jsp:plugin>

More Related