QuickStart Tool JavaServer Pages (JSP) PDF
QuickStart Tool JavaServer Pages (JSP) PDF
Getting a good start in any new technology or programming language often depends on finding the best available information. The Builder.com QuickStart Tools give you the information you need to quickly grasp the fundamentals of developing in a new IDE, using a new programming language, or working with a new development tool. JavaServer Pages (JSP) is a server-side scripting technology that uses the Java programming language to write dynamic content. JSP version 1.x gives developers the tools to build platform-independent Web-based applications. Besides explaining the basics, this Builder.com QuickStart Tool shows common tasks, exposes strengths and weaknesses, demonstrates some of the best uses of JSP, and lists a variety of other online and offline resources that can help you build a solid foundation of practical knowledge.
Table of contents Fundamentals ................................................................................................................3 Common tasks ..............................................................................................................5 Strengths ........................................................................................................................7 Weaknesses ..................................................................................................................7 Best uses ........................................................................................................................8 Online resources............................................................................................................8 Other resources ............................................................................................................9 Additional information ..................................................................................................9 About Builder.com ......................................................................................................10
Fundamentals
JavaServer Pages (JSP) is a server-side scripting technology that uses the Java programming language to write dynamic content. This content is written within tags and interpreted by an application server. When the page is accessed for the first time, pages are compiled into servlets, which deliver server-side dynamic content. JSP version 1.x was developed by Sun Microsystems as an extension of the Java Servlet technology. Sun Microsystems has made JSP specifications available to all developers free of cost as a component of the Java 2 Platform, Enterprise Edition (J2EE). It is useful for all developers and programmers who want to create dynamic Web pages for Web sites or portals.
Begin
Launch your favorite text editor and type the code shown in the following sections using scriptlet, declaration, and expression tags.
In the above code, JSPs scriptlet tag ( <% and %> ) is used for the Hello World program.
In the above code listing, the declaration tag ( <%! and %> ) is used to declare a string object (or variable) with the value Hello World. Next, the string value is displayed using the scriptlet tag. 3
1995-2004 CNET Networks, Inc. All Rights Reserved.
In the above code listing, the declaration tag is used to declare a string object (or variable) with the value Hello World. The string value is displayed using the expression tag (<%= and %>) instead of the scriptlet tag.
Common tasks
Common tasks refer to actions that are frequently performed by developers. Listed below are some of the common tasks associated with JSP version 1.x:
Task
Connecting to a MySQL database
Steps
JSP connects to a MySQL database using the MySQL Connector/J drivers.
<% try { Class.forName(com.mysql.jdbc.Driver).newInstance(); Connection con = DriverManager.getConnection (jdbc:mysql://<host-name>/<database-name>?user=<username> &password=<password>) } catch (Exception e) { out.println(e); } %>
The JDBC driver to load is com.mysql.jdbc.Driver. The connection string is in the format:
jdbc:mysql://<host-name>/<database-name>?user=<username> &password=<password>
The <host-name> is the name or IP address of the machine running the MySQL database server. The <database-name> is the name of the database to use, and <username> and <password> are the credentials required to connect to the database. Note that database connections may throw exceptions, so a try/catch block is included.
Figure A
Using a JavaBean
JSP can take advantage of a JavaBean by using the useBean tag as follows:
<jsp:useBean id=bean class=JavaBean />
Here bean is the designated identity and JavaBean is the name of the class file that contains the compiled code for the JavaBean. Setting JavaBean properties The properties of a JavaBean can be set within JSP using the setProperty tag. Suppose the JavaBean has a property named var and it has to be set to a value of 1. This is accomplished as follows:
<jsp:setProperty name=bean property=var value=1 />
An implicit object called session can store, retrieve, or remove objects in an HTTP session. To store an integer value of 25 in the session:
session.setAttribute(sessionVar,new Integer(25));
The JSP include tag can be used to include static HTML files:
<jsp:include page=header.html />
It can also be used to include dynamic content, such as another JSP page:
<jsp:include page=footer.jsp />
This capability is commonly used to insert consistent headers and footers across a Web sites pages. Accessing HTML form data Data posted to a JSP version 1.x file from an HTML form can be accessed using the request object. For example, the data typed in an HTML forms textfield named text can be accessed as:
request.getParameter(text);
Strengths Strength
Powerful technology for display of static and dynamic content
Description
JSP is used for developing static and dynamic Web pages. JSP uses text-based documents containing tags and constructs to describe how to process a request and construct a response. A JSP Web page also combines static tags using HTML and dynamic coding using complex methods for accessing Java Database Connectivity (JDBC) objects or Remote Method Invocation (RMI) objects. JSP version 1.x pages are compiled in the Java servlet class. These classes remain in the server memory for a longer duration; therefore, if the page is accessed again, recompilation is not required, which saves time and enhances application performance. JSP uses customized tag libraries, which provide an advantage to developers because they can work with familiar tools and constructs. JSP takes advantages of code reuse, because the same code is usable on different platforms. JSP scriptlets integrate with HTML code to create dynamic Web pages. JSP can be executed on any Web server. All major Web servers, including Apache and IIS, support JSP version 1.x.
Enhanced performance
Extensible Offers write once, run anywhere capability Easy integration with HTML Supported by all Web servers
Weaknesses Weakness
Preliminary knowledge of Java required Tags not rendered
Description
A developer working efficiently in JSP must have knowledge of Java technology. Various JSP authoring tools are supplied by vendors such as Macromedia. However, in this process, sometimes JSP version 1.x tags, or customized tag libraries created in an authoring tool, are not recognizable by different authoring tool vendors. This inconsistency sometimes results in non-rendered JSP tags. The debugging tools for JSP version 1.x are not mature enough to help the developers debug the code. Many authoring tools for JSP do not offer complete tag library support. In an environment with separate creative and technical teams, the creative team may not necessarily know JSP or Java syntax, and the technical team may have problems implementing the HTML files delivered by the creative team. Both teams lack a common language to describe their requirements for the rendering of each page in a JSP environment. JSP-based Internet applications are not as efficient as traditional desktop applications because the current software architecture supporting these applications can only receive real-time data by repeatedly refreshing the page. This repetition reduces the overall performance and scalability of the application. 7
1995-2004 CNET Networks, Inc. All Rights Reserved.
Underdeveloped debugging tool Shortage of full-service authoring tools No common language for creative and technical teams
Online resources
The JavaServer Pages(JSP) 1.2 Specification Tag Extensions: Design Patterns and Web Architectures
This PDF details the JSP tag specifications.
Web Application Development with JSP and XML Part I: Fast Track JSP
JSP allows you to separate front-end presentation from business logic (middle and back-end tiers). It is a great Rapid Application Development (RAD) approach to Web applications. This white paper provides a hands-on tutorial explaining how to develop modern Web applications.
Other resources
Core JSP
By Damon Hougland (Author), Aaron Tavistock (Author), 2000, ISBN: 0-13-088248-8
JavaServer Pages
By Larne Pekowsky (Author), 2000, ISBN: 0-201-70421-8
Additional information
JavaServer Pages (JSP) 1.0 Facilitate database access in your JSP application design with JDBC Implementing a form in a JSP Page Intro to JavaServer Pages: Learn the syntax basics Intro to JavaServer Pages: Create a JSP site Intro to JavaServer Pages: Utilize the implicit object set Template taglib ver. 1.3
About Builder.com
Thank you for downloading this Builder.com tool; we hope it helps you make better technology decisions. If you need help with the site or just want to add your comments, let us know. Builder.com provides actionable information, tools, trialware, and services to help Web and software developers get their jobs done. Builder.com serves the needs of developers on all major development platforms, including .NET and J2EE, and supplies the knowledge and tools every developer needs to build better and more robust applications. Free how-to articles: Covering everything from open source software to the latest .NET technologies, Builder articles give developers the breadth and depth of coverage they need to build better applications. Free e-newsletters: Keep up to date on any aspect of the developer industryfrom Web services to Visual Basicwith Builders e-newsletters, delivered right to your e-mail inbox. Free trialware: Weve collected all the latest trialware and free downloadscovering everything from application servers to HTML editorsto make your job easier. Discussion Center: Open a discussion thread on any article or column, or jump into preselected topics, such as Java, HTML, and career management. The fully searchable Discussion Center brings you the hottest discussions and threads. Your free Builder membership opens the door to a wealth of information. Our online developer community provides real-world solutions, the latest how-to articles, and discussions affecting developers everywhere. Get access to full-text books, exclusive downloads, and posting privileges to our discussion boards, all free!
10