1 / 27

JSP Java Server Pages

JSP Java Server Pages. Reference: http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html original: by Ramamurthy Enhanced and Updated by Dr. Peri Sastry. JSP = html + Servlet + html. -JSP [Java Server Pages] are html pages embedded with Java code

jdoty
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. JSPJava Server Pages Reference: http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-JSP.html original: by Ramamurthy Enhanced and Updated by Dr. PeriSastry

  2. JSP = html + Servlet + html -JSP [Java Server Pages] are html pages embedded with Java code JSP files must have the extension .jsp JSP is translated into a Java servlet , compiled by web server and served(first-time). On Later calls, compiled servlets are used, if JSP is NOT modified, thus quickening execution

  3. JSP (Java Server Pages) • JSP (Java Server Pages) is an alternate way of creating servlets • JSP is written as ordinary HTML, with a little Java mixed in • The Java is enclosed in special tags, such as<% ... %> • The HTML is known as the template text • JSP files must have the extension .jsp • JSP is translated into a Java servlet, which is then compiled • JSPs need NOT be compiled by you, Web Server tomcat does it automatically. • The browser or other client sees only the resultant final html page. • Tomcat knows how to handle JSP code snippets and html

  4. A simple JSP example Type the following in notepad, save as hello.jsp (all files type) into folderc:\tomcat7\webapps\ROOT and start tomcat. Then type in address bar of browser http://localhost:8080/hello.jsp and see the result <html > <head> <title>Dr Peri Sastrys Hello jsp</title> </head> <!-- end of head section. This is html comment --> <body bgcolor=cyan > <center> <h1> Hello , from html<br /> <% out.println("Hello World<br> "); out.println("Dr. Peri Sastry <br>Good morning, <br>Welcome from JSP."); %> </h1> </center> </body> </html>

  5. Review: A “Hello World” servlet(from the Tomcat installation documentation) public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">\n"; out.println(docType + "<HTML>\n" + "<HEAD><TITLE>Hello</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1>Hello World</H1>\n" + "</BODY></HTML>"); } } This is mostly Java with a little HTML mixed-in

  6. Review: Servlets • The purpose of a servlet is to create a Web page in response to a client request • Servlets are written in Java, with a little HTML mixed in • The HTML is enclosed in System.out.println( )statements • Servlets are pure Java programs. They introduce dynamism into web pages by using programmatic content.

  7. Java Server Pages • JSP technology is an extension/wrapper over the Java servlet technology. • JSP are text based documents. • We will focus mainly 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 scriptlets (java code) written in Java language to encapsulate the application logic.

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

  9. <HTML><BODY>Hello!  The time is now <br><%= new java.util.Date() %></BODY></HTML> 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 One more Example JSP

  10. More on JSP syntax and contents • HTML code for user interface lay out • JSP tags: declarations, actions, directives, expressions, scriptlets • JSP implicit objects: request object, response object, out object, session object, config object etc., • Javabeans: for logic that can be taken care of at the JSP level.[used in shopping cart example] • We will examine only JSP tags now.

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

  12. Java Server Pages by Examples • JSPs combines 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.

  13. How to prepare and run the examples? • Simple JSPs can be typed into .jsp type files using your favorite editor. • Create a directory called JSPExamples in the tomcat7/webapps/ROOT directory of tomcat. Store the example JSPs there. • Start the tomcat server. • Run the JSP from your browser using the command: • http://localhost:8080/JSPExamples/xyz.jsp • For complex examples with actions and beans you will have to create web Archive file (WAR)and deploy it.

  14. 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 <% out.println(“Date is <br> “ %> <%= new java.util.Date() %>- Using Page directives to define various page attributes: <%@ page import="java.sql.*"%> - Directive to include other text files, html files and JSPs] <%@ include file=“header.txt“ %> Other Examples:<% String name = request.getParameter("UserName"); session = request.getSession(true);session.setAttribute("UserName",name);suname= (String) session.getAttribute("UserName"); response.sendRedirect("UserMainPage.jsp"); %>

  15. 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. • Naming a piece of code: success.jsp

  16. Examples: Scripting Elements (contd.) • Scriplets: <% %> • A scriplet is a block of Java code that is executed during the request-processing time. • See con_mysql.txt in allmems.jsp • Expressions: sends a value of a Java expression back to the client. • <%= %> example <= new java.util.Date() %> • See <%@ include file="con_mysql.txt" %> in allmems.jsp

  17. 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> refer http://www.javatpoint.com/jsp-include-action <jsp:forward> <jsp:plugin>

  18. JSP scripting elements • There is more than one type of JSP “tag,” depending on what you want done with the Java • <%= expression %> • The expression is evaluated and the result is inserted into the HTML page • <% code %> • The code is inserted into the servlet's service method • This construction is called a scriptlet • <%! declarations %> • The declarations are inserted into the servlet class, not into a method

  19. 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: <%= request.getRemoteHost() %>

  20. Scriptlets • Scriptlets are enclosed in <% ... %> tags • Scriptlets do not produce a value that is inserted directly into the HTML (as is done with <%= ... %>) • Scriptlets are Java code that may write into the HTML • Example:<% String queryData = request.getQueryString(); out.println("Attached GET data: " + queryData);%> • Scriptlets are inserted into the servlet exactly as written, and are not compiled until the entire servlet is compiled • Example:<%if (Math.random() < 0.5) { %> Have a <B>nice</B> day!<% } else { %>Have a <B>lousy</B> day!<%}%>

  21. 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 • Data can also safely be put in the request or session objects • Example:<%! private int accessCount = 0; %> Accesses to page since server reboot: <%= ++accessCount %> • You can use<%! ... %> to declare methods as easily as to declare variables

  22. Directives • Directives affect the servlet class itself • A directive has the form:<%@ directiveattribute="value" %>or<%@ directiveattribute1="value1" attribute2="value2" ...attributeN="valueN" %> • The most useful directive is page, which lets you import packages • Example:<%@ page import="java.util.*" %>

  23. 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: <%@ include file="URL" %> • 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

  24. Actions • Actions are XML-syntax tags used to control the servlet engine • <jsp:include page="URL" flush="true" /> • Inserts the indicated relative URL at execution time (not at compile time, like the include directive does) • This is great for rapidly changing data • <jsp:forward page="URL" /><jsp:forward page="<%= JavaExpression %>" /> • Jump to the (static) URL or the (dynamically computed) JavaExpression resulting in a URL

  25. 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: <%=expression %>XML: <jsp:expression>expression</jsp:expression> • HTML: <%code%>XML: <jsp:scriptlet>code</jsp:scriptlet> • HTML: <%!declarations%>XML: <jsp:declaration>declarations</jsp:declaration> • HTML: <%@ include file=URL%>XML: <jsp:directive.include file="URL"/>

  26. Example: JSP Action and Using beans (not EJB) • Create beans.html files that displays to the user a choice of programming languages to choose from. Store it public_html/JSPExamples/beans.html • Create the file beans.jsp that deals with the request that has two parameters name of the user and the language. Store it in public_html/JSPExamples/beans.jsp • Create the beans file that is a java bean with just set and get properties: call it LanguageBean.java. Store it in public_html/JSPExamples/src/com/wrox/beans/LanguageBeans.java • Compile this using javac command. javac LangaugeBeans.java • Create the web application and run it as a web application.

  27. Any Questions ? • Thank You

More Related