<HTML> <HEAD> <TITLE>Using Superclass Variables With Subclassed Objects</TITLE> </HEAD> <BODY> <H1>Using Superclass Variables With Subclassed Objects</H1> <%! javax.servlet.jsp.JspWriter localOut; class BaseClass { public void start() throws java.io.IOException { localOut.println("Starting...<BR>"); } } class DerivedClass1 extends BaseClass { public void fly() throws java.io.IOException { localOut.println("Flying...<BR>"); } } class DerivedClass2 extends DerivedClass1 { public void fly() throws java.io.IOException { localOut.println("Flying...<BR>"); } } %> <% localOut = out; out.println(); out.println("Creating a DerivedClass2 object...<BR>"); BaseClass p = new DerivedClass2(); p.start(); %> </BODY> </HTML>
Showing posts with label index.jsp. Show all posts
Showing posts with label index.jsp. Show all posts
Saturday, 17 December 2011
JSP Examples Tutorial - Using Superclass Variables With Subclassed Objects
JSP Examples Tutorial - Using Abstract Classes in JSP
<HTML> <HEAD> <TITLE>Using Abstract Classes</TITLE> </HEAD> <BODY> <H1>Using Abstract Classes</H1> <%! javax.servlet.jsp.JspWriter localOut; abstract class a { abstract String getText() throws java.io.IOException; public void printem() throws java.io.IOException { localOut.println(getText()); } } class b extends a { String getText() throws java.io.IOException { return "Hello from JSP!"; } } %> <% localOut = out; b bObject = new b(); bObject.printem(); %> </BODY> </HTML>
JSP Examples Tutorial - Overriding Methods in derived class
<HTML> <HEAD> <TITLE>Overriding Methods in derived class</TITLE> </HEAD> <BODY> <H1>Overriding Methods</H1> <%! javax.servlet.jsp.JspWriter localOut; class BaseClass { public void print() throws java.io.IOException { localOut.println("print in base...<BR>"); } } class DerivedClass extends BaseClass { public void print() throws java.io.IOException { localOut.println("print in Derived class...<BR>"); } } %> <% localOut = out; out.println("Creating an animal object...<BR>"); BaseClass a = new BaseClass(); a.print(); out.println(); out.println("Creating a trout object...<BR>"); DerivedClass t = new DerivedClass(); t.print(); %> </BODY> </HTML>
JSP Examples Tutorial - Overloading Methods
<HTML> <HEAD> <TITLE>Overloading Methods</TITLE> </HEAD> <BODY> <H1>Overloading Methods</H1> <%! javax.servlet.jsp.JspWriter localOut; void printText() throws java.io.IOException { localOut.println("Hello!<BR>"); } void printText(String s) throws java.io.IOException { localOut.println(s + "<BR>"); } %> <% localOut = out; printText(); printText("Hello from JSP!"); %> </BODY> </HTML>
JSP Examples Tutorial - Creating a Java Interface
<HTML> <HEAD> <TITLE>Creating a Java Interface</TITLE> </HEAD> <BODY> <H1>Creating a Java Interface</H1> <%! javax.servlet.jsp.JspWriter localOut; interface Printem { void printText() throws java.io.IOException; } class a implements Printem { public void printText() throws java.io.IOException { localOut.println("Hello from JSP!"); } } %> <% localOut = out; a printer = new a(); printer.printText(); %> </BODY> </HTML>
JSP Examples Tutorial - Calling Superclass Constructors
<HTML> <HEAD> <TITLE>Calling Superclass Constructors</TITLE> </HEAD> <BODY> <H1>Calling Superclass Constructors</H1> <%! javax.servlet.jsp.JspWriter localOut; class a { a() throws java.io.IOException { localOut.println("In a\"s constructor...<BR>"); } } class b extends a { b() throws java.io.IOException { localOut.println("In b\"s constructor...<BR>"); } } %> <% localOut = out; b obj = new b(); %> </BODY> </HTML>
What Is a JSP Pages?
A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format, such as HTML, SVG, WML, and XML; and JSP elements, which construct dynamic content. A syntax card and reference for the JSP elements are available at:
http://java.sun.com/products/jsp/technical.html#syntax
The source code for this example is in the
j2eetutorial/examples/src/web/date directory created when you unzip the tutorial bundle. The JSP page index.jsp used to create the form appears below; it is a typical mixture of static HTML markup and JSP elements. If you have developed Web pages, you are probably familiar with the HTML document structure statements (<head>, <body>, and so on) and the HTML statements that create a form (<form>) and a menu (<select>). The lines in bold in the example code contain the following types of JSP constructs:- Directives (<%@ page ... %>) import classes in the
java.utilpackage and theMyLocalesclass, and set the content type returned by the page. - The jsp:useBean element creates an object containing a collection of locales and initializes a variable that points to that object.
- Scriptlets (<% ... %> ) retrieve the value of the
localerequest parameter, iterate over a collection of locale names, and conditionally insert HTML text into the output. - Expressions (<%= ... %>) insert the value of the locale name into the response.
- The jsp:include element sends a request to another page (
date.jsp) and includes the response in the response from the calling page.<%@ page import="java.util.*,MyLocales" %> <%@ page contentType="text/html; charset=ISO-8859-5" %> <html> <head><title>Localized Dates</title></head> <body bgcolor="white"> <jsp:useBean id="locales" scope="application" class="MyLocales"/> <form name="localeForm" action="index.jsp" method="post"> <b>Locale:</b> <select name=locale> <% String selectedLocale = request.getParameter("locale"); Iterator i = locales.getLocaleNames().iterator(); while (i.hasNext()) { String locale = (String)i.next(); if (selectedLocale != null && selectedLocale.equals(locale)) { %> <option selected><%=locale%></option> <% } else { %> <option><%=locale%></option> <% } } %> </select> <input type="submit" name="Submit" value="Get Date"> </form> <jsp:include page="date.jsp"/> </body> </html>
- Go to
j2eetutorial/examplesand build the example by executingant date. - Create a J2EE application called
DateApp. - Create the WAR and add the Web components to the
DateAppapplication.- Select File
New
Web Component. - Select
DateAppfrom the Create New WAR File In Application combo box. - Enter
DateWARin the WAR Display Name field. - Click Edit.
- Navigate to
j2eetutorial/examples/build/web/date. Selectindex.jsp,date.jsp,MyDate.class, andMyLocales.classand click Add. Then click Finish. - Click Next.
- Click JSP In The Web Component radio button, and then click Next.
- Select
index.jspfrom the JSP Filename combo box. Click Finish.
- Select File
- Enter the context root.
- Deploy the application.
- Invoke the URL
http://<host>:8000/datein a browser.
Subscribe to:
Posts (Atom)