1 / 22

JSP java server pages

JSP java server pages.

finna
Download Presentation

JSP 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. JSP java server pages

  2. A JSP is an HTML file that has Java servlet code embedded in it in special tags . When you run a JSP, all the HTML is automatically sent as part of the response, along with any HTML that’s created by the Java code you embed in the JSP file. As a result, JSP spares you the chore of writing all those out.println statements. A Java Server Page is an HTML document that’s saved in a file with the extension .jsp instead of .htm or .html. Unlike servlet class files, you can store a JSP file in any directory that’s available to the Web server.

  3. The first time a user requests a JSP file, the JSP file is run through a translator program that converts the file into a Java servlet program and compiles it. All the HTML from the original JSP file is converted to out.print statements that send the HTML to the response, and the Java statements from the JSP file are incorporated into the servlet program. Then, the servlet program is executed and the results sent back to the browser.

  4. When you create a JSP, you intermix special JSP elements with your normal HTML. You can include fourtypes of JSP elements:✦ Directives: Directives let you do things such as specify what import statements the servlet requires, specify whether the servlet is thread-safe, and include other source files in the servlet.<%@ page attribute=value %>✦ Expressions: An expression can be any Java expression. Expressions assume the following form:<%= expression %>✦ Scriptlets: A scriptlet is a sequence of Java statements that are inserted directly into the servlet code generated for the JSP. Scriptlets have the following form:<% statements %>✦ Declarations: A declaration is Java code that is placed in the servletclass outside of any methods. You use declarations to create class variablesor define methods that can be called by scriptlets or expressions. Declarations take on this form:<%! statements %>

  5. Directives <%@ page import=“java.util.*” %>

  6. Expressions <%=new java.util.Date()%> ----------- or --------------- <%@ page import=”java.util” %> <%=new Date()%> <%= request.getParameter(“Name”)%>

  7. Expressions Example <html> <head> <title>Input JSP</title> </head> <body> <form action=”InputJSP.jsp” method=”post”> Enter some text:&nbsp; <input type=”text” name=”Text”> <br><br> <input type=”submit” value=”Submit”> </form><br> <h3>You entered:&nbsp; <%= request.getParameter(“Text”)%></h3> </body> </html>

  8. Scriptlets Example-1 <html> <%@ page import=”java.text.*” %> <%@ page import=”java.util.*” %> <head> <title>Date JSP</title> </head> <body> <h1> Today is <% DateFormat df = DateFormat.getDateInstance(DateFormat.FULL); Date today = new Date(); String msg = df.format(today); out.println(msg); %> </h1> <h1>Have a nice day!</h1> </body> </html>

  9. Scriptlets Example-2 <html> <head> <title> Can’t you see I’m trying to work here? </title> </head> <body> <% for (int i = 0; i < 12; i++) { % > All work and no play makes Jack a dull boy.<br> <% } %> </body> </html>

  10. Declarations Example-1 <html> <%@ page import=”java.text.*” %> <%@ page import=”java.util.*” %> <head> <title>Counter JSP</title> </head> <body> <h1> This JSP has been displayed <%= count++ %> time. </h1> </body> </html> <%! private static int count = 1; %>

  11. Declarations Example-2 <html> <%@ page import=”java.text.*” %> <%@ page import=”java.util.*” %> <head> <title>Date JSP</title> </head> <body> <h1> Today is <%= getDate() %></h1> <h1>Have a nice day!</h1> </body> </html> <%! private String getDate() { DateFormat df =DateFormat.getDateInstance(DateFormat.FULL); Date today = new Date(); return df.format(today); } %>

  12. Exercise

  13. JavaBeans

  14. A JavaBean is any Java class that conforms to the following rules: ✦ It must have an empty constructor. That is, a constructor that accepts ✦ It must have no public instance variables. All the instance variables defined by the class must be either private or protected. ✦ It must provide methods named getProperty and setProperty to get and set the value of any properties the class provides, except for boolean properties that use isProperty to get the property value.

  15. Example-1 JavaBeans

  16. A JSP page that uses a bean

  17. Scope of A JavaBeans

  18. Example-2 JavaBeans

  19. A JSP page that uses a bean

More Related