Calendar

Java server page jsp

Introduction to BEA WebLogic Server 5.1: Previous topic | Next topic | Contents

JSP (JavaServer Pages) is a standard for combining Java and HTML to provide dynamic content in web pages. JSP is part of Sun’s Java 2 Platform, Enterprise Edition. WebLogic Server 4.5 implements the JSP 1.1 specification.

JSP depends upon HTTP servlets-the JSP compiler generates the source code for an HTTP servlet from a .jsp file. The Java classes produced using the HTTP servlet and JSP are essentially equivalent. You can use either technology to create dynamic web pages.

JSP emphasizes page design with standard HTML formatting, so it is more convenient to use JSP for creating dynamic web pages than it is to write a Java HTTP servlet.

With JSP, you embed Java code in HTML using special JSP tags similar to HTML tags. You install the JSP page, which has a .jsp extension, into the WebLogic Server document root, just as you would a static HTML page. When WebLogic Server serves a JSP page to a client, it tests whether the file has been compiled. If not, it calls the WebLogic JSP compiler to compile the file into a servlet. A .jsp file is only recompiled when it has changed.

Although you can embed as much Java code as you like into a JSP page, using JavaBeans and Enterprise JavaBeans lets you concentrate on page design and presentation in the JSP page and encourages you to move application logic into reusable components. Using beans from JSP pages also makes it possible for web page designers to create and maintain dynamic web pages without having to learn Java programming.

Читайте также:  Vscode windows python virtualenv

JSP defines the following tags to enclose Java code fragments and directives for the JSP compiler:

declaration %> Contains class-level declarations of Java variables or methods. The variables and methods declared can be referenced within other JSP tags on the page.

scriptlet %> Contains a Java code fragment that executes when the JSP page executes.

relativeURL » %> Includes the contents of a file at compile time. The JSP compiler includes the file and processes its contents.

attributes %> Defines page-level attributes. Attributes are specified as name Courier»>value » pairs. Multiple attributes are separated by white space within the tag.

expression %> Writes the value of a Java expression into the response page.

expression %> —> Evaluates a Java expression within an HTML comment. The resulting comment is not rendered by the browser, but can be viewed in the page source.

comment —%> Comments the JSP source. Comments written within these tags are not included in the HTML output.

Specifies a JavaBean to be used on the JSP page.

Sets JavaBean properties.

Gets JavaBean properties.

JSP tags can also be written with XML tags. See Using WebLogic JSP for a table of equivalent tags.

JSP provides a number of implicit variables that can be referenced on any JSP page. The JSP compiler adds these class variable declarations to the generated servlet code:

HttpServletRequest request; // Request object
HttpServletResponse response; // Response object
HttpSession session; // Session object for the client
ServletConfig config; // ServletConfig for this JSP
ServletContext application; // Servlet context
PageContext pageContext; // Page context for this JSP
Object page; // Instance of the implementation
// class for this JSP (i.e., ‘this’)
JspWriter out; // PrintWriter to the browser

The request and response variables provide access to the HTTP request and response objects. A JSP page can extract information from the request and write information on the response using methods from the javax.servlet.http package.

The session variable allows WebLogic Server to save state for a client between requests on a JSP page. You must set the «session=true» attribute in the JSP page directive so that the JSP compiler includes support for sessions. Then you can save arbitrary values and retrieve them again on subsequent requests with the getValue() and putValue() methods on the session.

The config variable provides access to the javax.servlet.ServletConfig object for the JSP page. The methods on this object return initialization parameters for the page. With WebLogic Server, you define initialization parameters by setting an initArg property when you register a servlet in the weblogic.properties file. To use initialization arguments with JSP pages, you must register the JSP page as a servlet.

The application variable gives you access to information about the environment of the JSP page and provides methods for writing messages in the WebLogic Server log.

The pageContext variable provides access to a javax.servlet.jsp.PageContext object. You can use methods on this object to access various scoped namespaces, servlet-related objects, and common servlet-related functionality.

The page variable references the current instance of the JSP page. page is declared as a java.lang.Object , so you must cast it to the name of the generated servlet class name if you want to use it. An easier alternative is to just refer to the current instance of the page with the Java this keyword.

The out variable is a Java PrintWriter you can use to write HTML to the browser page from inside JSP tags.

The Calendar.jsp example is a simple JSP page that calls a JavaBean to display a calendar for the current month. The source for the JavaBean is in the file CalendarBean.java .

CalendarBean gets the current date and provides two get methods, getTodayString() and getHtmlMonth() . getTodayString() returns the current date in a String such as «Thursday, October 14, 1999» . getHtmlMonth() returns a String containing the calendar for the current month, formatted as an HTML table.

CalendarBean has one set method, setColor() , which is used to set the background color of the calendar.

Calendar.jsp shows how to specify a JavaBean, set its properties, and get its values from within a JSP page. The source of the JSP page is fairly simple, but it demonstrates how complicated web page elements can be delegated to reusable JavaBean components.

Here is the source for Calendar.jsp :

info=»Calendar JSP example»
contentType=»text/html»
%>

scope=»page»

/>

property=»Color» value=»#FFFFCC»/>

Today is property=»TodayString»/>


This page executed by
.

Copyright 1999-2000 © BEA Systems, Inc.
All Rights Reserved.

The tag identifies the CalendarBean class and assigns it an ID ( calendar ), which is used to reference it in other JSP tags on the page. The scope attribute specifies that the instance of this bean is stored in the current page. Setting the scope to «session» would tell WebLogic Server to store the bean instance in the session so that any changes persist over multiple requests on the JSP page.

The tag sets the background color of the HTML table. The JSP compiler translates this to a call to the CalendarBean.setColor() method.

The tags are translated into calls to CalendarBean.getTodayString() and CalendarBean.getHtmlCalendar() . The results of those calls are embedded in the HTML output.

Here is an example of the output from the Calendar.jsp JSP page:

Running the Calendar.jsp Example

To use Calendar.jsp , your WebLogic Server must be configured to serve JSP pages. Then you compile CalendarBean.java , copy Calendar.jsp into WebLogic Server’s document root, and call the page in a browser. Here are the steps:

For more help on these properties see Setting up WebLogic for JSP in «Using WebLogic JSP».

javac -d %SERVER_CLASSES% CalendarBean.java
javac -d $SERVER_CLASSES CalendarBean.java
http://localhost:7001/Calendar.jsp

You can find more about using WebLogic JSP in the developers guide Using WebLogic JSP.

See the Sun JSP page to find JSP specifications, tutorials, and examples.

Copyright © 2000 BEA Systems, Inc. All rights reserved.
Required browser: Netscape 4.0 or higher, or Microsoft Internet Explorer 4.0 or higher.

Источник

JSP Tutorial

JSP Tutorial

Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic, platform-independent method for building Web-based applications. JSP have access to the entire family of Java APIs, including the JDBC API to access enterprise databases. This tutorial will teach you how to use Java Server Pages to develop your web applications in simple and easy steps.

Why to Learn JSP?

JavaServer Pages often serve the same purpose as programs implemented using the Common Gateway Interface (CGI). But JSP offers several advantages in comparison with the CGI.

  • Performance is significantly better because JSP allows embedding Dynamic Elements in HTML Pages itself instead of having separate CGI files.
  • JSP are always compiled before they are processed by the server unlike CGI/Perl which requires the server to load an interpreter and the target script each time the page is requested.
  • JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has access to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP, etc.
  • JSP pages can be used in combination with servlets that handle the business logic, the model supported by Java servlet template engines.

Finally, JSP is an integral part of Java EE, a complete platform for enterprise class applications. This means that JSP can play a part in the simplest applications to the most complex and demanding.

Applications of JSP

As mentioned before, JSP is one of the most widely used language over the web. I’m going to list few of them here:

JSP vs. Active Server Pages (ASP)

The advantages of JSP are twofold. First, the dynamic part is written in Java, not Visual Basic or other MS specific language, so it is more powerful and easier to use. Second, it is portable to other operating systems and non-Microsoft Web servers.

JSP vs. Pure Servlets

It is more convenient to write (and to modify!) regular HTML than to have plenty of println statements that generate the HTML.

JSP vs. Server-Side Includes (SSI)

SSI is really only intended for simple inclusions, not for «real» programs that use form data, make database connections, and the like.

JSP vs. JavaScript

JavaScript can generate HTML dynamically on the client but can hardly interact with the web server to perform complex tasks like database access and image processing etc.

JSP vs. Static HTML

Regular HTML, of course, cannot contain dynamic information.

Audience

This tutorial has been prepared for the beginners to help them understand basic functionality of Java Server Pages (JSP) to develop your web applications. After completing this tutorial you will find yourself at a moderate level of expertise in using JSP from where you can take yourself to next levels.

Prerequisites

We assume you have little knowledge of how web applications work over HTTP, what is web server and what is web browsers. It will be great if you have some knowledge of web application development using any programming language.

Источник

Оцените статью