Introduction
A JavaServer Page (JSP) is a template for a web page that uses Java code to generate an HTML document dynamically.They are run on the server in a JSP container which translates them into equivalent servlets.They are text files similar to HTML pages with some additional tags which generate dynamic content.Servlets and JSP pages are intimately related.JSP has the following advantages over servlets:
- They are automatically recomplied when necessary.
- They are HTML like so they have greater compatibility with web development tools.
- Addressing JSP pages is simpler than addressing servlets.
Process Of Execution Of A Request
The following steps execute a JSP request from the time the request is received from client until it is processed and sent back to client.
Step1:- Request from Client: A JSP page has the extension as .jsp. The client request is made on the web browser by going to the .jsp extension JSP file.
Step2:-Request Sent To Server: The request received from client (web browser) is sent to the server side.
Step3:-JSP Servlet Engine: Since the extension of request made has .jsp extension, it indicates that it is a JSP file and therefore the web server passes the request received to the JSP Servlet Engine.
Step4:-Process of generating servlet: JSP files are compiled by the JSP engine into a servlet. This step creates the .jsp file as a Java servlet source file.
Step5:-Process of making class file: The source file from Step4 is compiled into a class file.
Step6:- Instantiation of Servlet: The instantiation of servlet is performed using the init and service methods. In these methods, the jspInit() method is defined by the developer and the jspService method is generated by the JSP engine.
Step7:-Output HTML: `The request received from client is executed and the output is sent as HTML.
Step8:- Client Receives the Output: The Client Receives the Output and thus the result namely the HTML gets displays in the client browser.
Life Cycle Of A JSP
Translation:-The first stage in the life cycle of a JSP is known as the translation phase.When a request is first made for a JSP (assuming it hasn’t been precompiled), the JSP engine will examine the JSP file to check that it’s correctly formed and the JSP syntax is correct. If the syntax check is successful then the JSP engine will translate the JSP into its page implementation class, which takes the form of a standard Java servlet. Once the page’s implementation servlet has been created it will be compiled into a class file by the JSP engine and will be ready for use. Each time a container receives a request, it first checks to see if the JSP file has changed since it was last translated. If it has, it’s retranslated so that the response is always generated by the most up-to-date implementation of the JSP file.
Initialization:-Once the translation phase has been completed, the JSP engine will need to load the generated class file and create an instance of the servlet in order to continue processing of the initial request. Therefore, the JSP engine works very closely with the servlet container and the JSP page implementation servlet and will typically load a single instance of the servlet into memory. As you may or may not be aware, the Java Servlet Specification provides two separate threading models that can be used. The models determine whether single or multiple instances of a servlet can exist. As with servlets, the default threading model is the multithreaded one that requires no additional work for the developer. To select the single-threaded model for your JSP, there’s an attribute of the page directive called isThreadSafe that must be set to false to serialize all requests to the implementation servlet behind the JSP:
<%@ page isThreadSafe="true" %>
Of course such directives must be placed in the JSP so that the JSP engine can make the necessary changes to the page implementation servlet during the translation phase.
Servicing:- Once the JSP and servlet containers have completed all of their preliminary work, the initial request can be serviced. There are three important methods that are generated in the page implementation servlet by the JSP engine during the translation phase. These three methods bear a striking resemblance to the init(), service(), and destroy() methods from the javax.servlet.Servlet interface. They’re the key methods involved in the life cycle of a servlet.
jspInit()
As the name suggests, this method is used for initializing the implementation servlet in an identical manner to the standard servlet init() method, which is used to initialize a servlet. The behavior of both methods can be regarded as identical and each is called exactly once.
Although this method is automatically generated during the translation phase, it’s possible to override this method in the JSP using a declaration. It can be used for initializing the JSP in order to open a database connection or initialize some session and context variables, for example.
<%! Connection conn = null; %>
<%!
public void jspInit() {
try {
conn = getConnection(…);
} catch (SQLException sqle){}
}
%>
_jspService()
This method provides all of the functionality for handling a request and returning a response to the client. All of the scriptlets and expressions end up inside this method, in the order in which they were declared inside the JSP. Notice that JSP declarations and directives aren’t included inside this method because they apply to the entire page, not just to a single request, and therefore exist outside the method. _jspService() may not be overridden in the JSP.
Destruction:- The final method worthy of explanation that is generated as a result of the translation phase is the jspDestroy() method. Like the destroy() method found in a normal servlet, this method is called by the servlet container when the page implementation servlet is about to be destroyed. This destruction could be for various reasons, such as the server being low on memory and wanting to free up some resources, but the most common reason would be when the servlet container is shutting down or being restarted.
Once this method has been called, the servlet can no longer serve any requests. Like the destroy() method, jspDestroy() is an excellent place to release or close resources such as database connections when the servlet is shutting down. To do this, simply provide an implementation of this method via a JSP method declaration. For example, to close the database connection you opened inside the jspInit() method, you would use the following:
<%!
public void jspDestroy() {
try {
conn.close();
} catch (SQLException sqle){}
conn = null;
}
%>
This is a simple JSP page that displays the current date:-
Code:
<html> <head> <title>Date</title> </head> <body> <h3> The date is <% out.println((new java.util.Date()).toString()); %> </h3> </body> </html>
Start Tomcat and go to http://localhost:8080/jsp/date.jsp
A lot of things happen behind the scenes when a JSP page is deployed in a web container and is first served to a client request. As mentioned before in the life cycle Deploying and serving a JSP page involves two distinct phases:
- Translation phase - In this phase the JSP page is transformed into a Java servlet and then compiled. This phase occurs only once for each JSP page and must be executed before the JSP page is served. The generated servlet class is known as a JSP page implementation class.
- Execution phase - This phase (also known as the request processing phase), is executed each time the JSP page is served by the web container to a client. Requests for the JSP page by a client results in the execution of the JSP page implementation class.
The class implements javax.servlet.jsp.HttpJspPage or javax.servlet.jsp.JspPage, both of which extend javax.servlet.Servlet. The following code is the source for the page implementation class of date.jsp.
The class extends HttpJspBase, which is a class provided by Tomcat that implements the HttpJspPage interface.
Code:
package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import org.apache.jasper.runtime.*; public class date$jsp extends HttpJspBase { public date$jsp() {} private static boolean _jspx_inited = false; /*When a request comes for the JSP page, the container creates an instance of the page and calls the jspInit() method on the page implementation object. */ public final void _jspx_init() throws org.apache.jasper.runtime.JspException {} /* The generated class overrides the _JSP pageservice() method. This method is called each time the JSP is accessed by a client browser. All the Java code and template text we have in our JSP pages normally go into this method. */ public void _JSP pageservice(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { JspFactory _jspxFactory = null; PageContext pageContext = null; HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; String _value = null; try { if (_jspx_inited == false) { synchronized (this) { if (_jspx_inited == false) { _jspx_init(); _jspx_inited = true; } } } _jspxFactory = JspFactory.getDefaultFactory(); response.setContentType("text/html;charset=ISO-8859-1"); pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true); //Here we initialize the implicit variables: application = pageContext.getServletContext(); config = pageContext.getServletConfig(); session = pageContext.getSession(); out = pageContext.getOut(); out.write("<html>\r\n<head>\r\n<title>Date</title>\r\n" + "</head>\r\n<body>\r\n<h3>\r\n" + "The date is\r\n"); out.println((new java.util.Date()).toString()); out.write("\r\n </h3>\r\n </body>\r\n</html>"); } catch (Throwable t) { if (out != null && out.getBufferSize() != 0) { out.clearBuffer(); } if (pageContext != null) { pageContext.handlePageException(t); } } finally { if (_jspxFactory != null) { _jspxFactory.releasePageContext(pageContext); } } } }
List Of Tags Used In Java Server Pages
Declarations
Declarations are used to define class wide variables and methods in the generated servlet. They are initialized when the JSP page is initialized and have instance scope in the generated servlet so that anything in a declaration is available throughout the JSP to other declarations, expressions and code. They do not produce any output that is sent back to the client.
Declarations in JSP pages are embedded between <%! and %> delimiters and its general syntax is
<%! Java variable and method declarations %>
for example:
ShowTimeD.jsp
Code:
<%@ page import=”java.text.*,java.util.*” %> <%! DateFormat fmt=new SimpleDateFormat(“hh:mm:ss aa”); String now=fmt.format(new Date()); %> <html> <head> <title>Declaration test page</title> <body> <h1> Declaration test page </h1> <p> The time is <%= now %></p> </body> </html>
Start Tomcat and go to http://localhost:8080/jsp/ShowTimeD.jsp
The time is 10:49:40 PM
But when the page is refreshed the output remains the same:
The time is 10:49:40 PM
The time is 10:49:40 PM
The time is 10:49:40 PM
This is because the declaration in the JSP is not placed in the _jspService() method of the generated servlet.They are inserted directly in the class itself to define static or instance variables and methods.
Another Example:-
Code:
<html> <body> <%! private int example = 0 ; private int test = 5 ; %> <%@page contentType="text/html" %> <%! int cnt=0; private int getCount(){ //increment cnt and return the value cnt++; return cnt; } %> <p>Values of Cnt are:</p> <p><%=getCount()%></p> <p><%=getCount()%></p> <p><%=getCount()%></p> <p><%=getCount()%></p> <p><%=getCount()%></p> <p><%=getCount()%></p> </body> </html>
Scriptlets
Scriptlets are blocks of Java code that is executed during request processing time.The contents of scriptlets go within the _JSP pageservice() method. Scriptlets are embedded between <% and %> delimiters and its general syntax is
<% valid java code statements %>
for example:
Code:
<% int x = 10; int y = 20; int z = 10 * 20; %>
Code:
<HTML> <BODY> <CENTER> <H3>ASCII Table</H3> <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"> <% StringBuffer sb = new StringBuffer(); sb.append("<TR>"); sb.append("<TH WIDTH=40> </TH>"); for (int col = 0; col < 16; col++) { sb.append("<TH>"); sb.append(Integer.toHexString(col)); sb.append("</TH>"); } sb.append("</TR>"); for (int row = 0; row < 16; row++) { sb.append("<TR>"); sb.append("<TH>"); sb.append(Integer.toHexString(row)); sb.append("</TH>"); for (int col = 0; col < 16; col++) { char c = (char)(row * 16 + col); sb.append("<TD WIDTH=32 ALIGN=CENTER>"); sb.append(c); sb.append("</TD>"); } sb.append("</TR>"); } out.println(sb); %> </TABLE> </CENTER> </BODY> </HTML>
Start Tomcat and go to http://localhost:8080/jsp/ASCII_Table.jsp
Expressions
An Expression is a shorthand notation for a scriptlet that sends the value of a java expression back to the client.The expression is evaluated at HTTP request processing time and if the output is a Java primitive, the value of the primitive is printed back to the browser. If the output is a Java object, the result of calling the toString() method on the object is written back to the browser.
An expression is enclosed in <%= and %> tags and its general syntax is
<%= Java expression to be evaluated %>
For example:
This expression would print the string 10 to the client:
Code:
<%= Math.sqrt(100) %>
10
Another Example:
Code:
<html> <head> <title> Expression test page </title> </head> <body> <h1> Expression test page </h1> <%! int i=0; %> <% i++; %> HELLO WORLD!! <%= "This JSP has been accessed " + i + "times" %> </body> </html>
An int variable I is declared with an initial value 0. Each time this instance of the generated servlet is called for example when the browser requests http://localhost:8080/jsp/expression.jsp
The variable I is incremented. Finally the value of i is printed using an expression.
Start Tomcat and go to http://localhost:8080/jsp/expression.jsp
The first time the client types the above url i =0.
The next time i is 1.
Directives
Directives are messages sent by the JSP author to the JSP container to aid the container in the process of page translation. These directives are used by the JSP container to import tag libraries, import required classes, set output buffering options, and include content from external files.
Directives begin with <%@ and end with %> and the general syntax is:
<%@ directivename attribute=”value” attribute=”value” %>
The JSP specification defines three main directives:
- page - provides general information about the page, such as the scripting language that is used, content type, or buffer size .
- include - used to include the contents of external files.
- taglib - used to import custom actions defined in tag libraries.
The page Directive:- The page directive sets a number of page dependent properties that can be used during the translation phase by the container. A JSP page can contain any number of page directives. However only the import attribute should appear more than once.
The following table shows the different attributes associated with the page directive:
Attribute ---------> Description
1) language -------> Defines the server-side scripting language used in the JSP. The only language that is currently supported is Java.
2) extends --------> Defines the name of the class the page implementation class should extend. This attribute is generally not used by page authors. It is left to the container to decide the ancestor of the page implementation class.
3) import ---------> Allows us to import packages and classes to be used within scriptlets. The packages and classes are defined as a comma separated list.
4) session --------> Defines whether the page should participate in a session. The session implicit variable is available for scripting only if the value of this attribute is set to true. The default value is true.
5) buffer ---------> Defines the buffer size for the JSP in kilobytes. If set to none, the output of the JSP is not buffered.
6) autoFlush ------> If set to true the buffer will be flushed when the maximum limit is reached. Otherwise an IllegalStateException is thrown.
7) isThreadSafe ---> If set to false, the page implementation class will implement the SingleThreadModel interface. The default value is true.
8) info -----------> The value of this attribute is returned by the getServletInfo() method of the page implementation class.
9) errorPage ------> Defines the relative URI of the web resource to which the response should be forwarded if an exception is thrown in the JSP page.
10) contentType ---> Defines the MIME type for the output response. The default value is text/html.
11) isErrorPage ---> Should be set to true for JSP pages that are defined as error pages.
12) pageEncoding --> Defines the character encoding for the page.
The following snippet shows an example of the page directive:
Code:
<%@ page language="java" buffer="10Kb" autoFlush="true" errorPage="/error.jsp" import="java.util.*” %> <html> <head> <title> Page Directive Tset Page </title> </head> <body> <h1>Page Directive Test Page</h1> This is a JSP page to test the page directive </body> </html>
- The scripting language is set as Java
- The buffer size is set to 10 kilo bytes
- The container will flush the contents if it exceeds the specified size
- The error page is defined as error.jsp
- The JSP page may use classes and interfaces from the java.util package within its scriptlets
The include Directive:-The include directive instructs the container to include the content of a resource in the current JSP during the translation phase. The contents of the included file specified by the directive is included in the including JSP page. For example:
<%@ include file="filename" %>
The included file is searched relative to the location of the current JSP unless it is preceded with a forward slash.
Write this code in notepad and save it include1.jsp. Keep this file in the webapps/jsp folder.
Code:
<html> <head> <title>Include Directive Test Page 1</title> </head> <body> <h1> Include Directive Test Page 1</h1> <%@ include file="copy.html" %> </body> </html> webapps/jsp/copy.html <html> <head> <title>Included Page</title> </head> <body> <p> © 2010 JSP</p> </body> </html>
Taglib:- The taglib directive allows the page to use tag extensions(custom tags). It names the tag library that contains compiled Java code defining the tags to be used. The engine uses this tag library to find out what to do when it comes across the custom tags in the JSP.
The syntax of taglib directive is
<%@ taglib uri=”tagLibraryURI” prefix=”tagPrefix” %>
uri= A URI(uniform resource Identifier) that identifies the tag library descriptor.
Prefix= Defines the prefix string in prefix:tagname that is used to define the custom tag.
Actions
Action tag is used to transfer the control between pages and is also used to enable the use of server side JavaBeans. Instead of using Java code, the programmer uses special JSP action tags to either link to a Java Bean set its properties, or get its properties.
General syntax of Action Tag is:-
<jsp:action attributes />
In the above statement jsp is a keyword.
There are many action tags that are available for Java Server Pages. The most commonly used action tags are three of them and they are namely:
- Include
- Forward
- UseBean
Syntax, attributes and usage of above three action tags will be given in the next part.
JSP Implicit Objects
JSP provides a set of implicit objects that can be used to access many server-side objects that represent the incoming request, response, and session objects. These objects are actually defined in the _JSP pageservice() method of the page implementation class and initialized with appropriate references.
JSP defines four scopes for the objects that can be used by the JSP authors:
Scope --------> Description
page ---------> Objects can be accessed only within the JSP page in which they are referenced.
request ------> Objects can be accessed within all the pages that serve the current request. These include pages that are forwarded to, and included in, the original JSP page to which the request was routed.
session ------> Objects can only be accessed within the JSP pages accessed within the session for which the objects are defined.
application --> Application scope objects can be accessed by all JSP pages in a given context.
The following relation lists the implicit objects that can be used in scriptlets:-
request Object is Protocol dependent sub type of
javax.servlet.ServletRequest
withrequest
scope - A reference to the current request.response Object is Protocol dependent sub type of
javax.servlet.ServletResponse
withpage
scope - The response to the request.pageContext Object is
javax.servlet.jsp.PageContext
type with page
scope - Provides a common point to access the request, response, session, and application, associated with the page being served.session Object is
javax.servlet.http.HttpSession
type with session
scope - The session associated with the current request.application Object is
javax.servlet.ServletContext
type with application
scope - The servlet context to which the page belongs.out Object is
javax.servlet.jsp.JspWriter
type with page
scope - The object that writes to the response output stream.config Object is
javax.servlet.ServletConfig
type with page
scope - The servlet configuration for the current page.Page Object is
java.lang.Object
type with page
scope - An instance of the page implementation class that is serving the request (synonymous with the this keyword if Java is used as the scripting language).exception Object is
java.lang.Throwable
type with page
scope - Available with JSP pages that act as error pages for other JSP pages.The following JSP page illustrates the use of implicit objects in JSP pages. Save this as webapps/jsp/Implicit.jsp:-
Code:
<html> <head> <title>Implicit Objects</title> </head> <body style="font-family:verdana;font-size:10pt"> <p> Using Request parameters...<br> //We print the request parameter name: Using Request parameters...<br> Name: <%= request.getParameter("name") %> </p> //We print a sentence using the out implicit variable: <p> <% out.println("This is printed using the out implicit variable"); %> </p> //We store and retrieve an object of type String using the session implicit variable: <p> Storing a string to the session...<br> <% session.setAttribute("name", "harsh"); %> Retrieving the string from session...<br> <b>Name:</b> <%= session.getAttribute("name") %> </p> //Store and retrieve an object of type String in the servlet context using the application implicit variable: <p> Storing a string to the application...<br> <% application.setAttribute("name", "harsh"); %> Retrieving the string from application...<br> <b>Name:</b> <%= application.getAttribute("name") %> </p> //Finally, store and retrieve an object of type String in the page context using the pageContext implicit variable: <p> Storing a string to the page context...<br> <% pageContext.setAttribute("name", "harsh"); %> Retrieving the string from page context...</br> <b>Name:</b> <%= pageContext.getAttribute("name") %> </p> </body> </html>
JSP Actions
JSP actions are processed during the request processing phase (as opposed to directives, which are processed during the translation phase). The JSP specification defines a few standard actions that must be supported by all compliant web containers. JSP also provides a powerful framework for developing custom actions, which are included in a JSP page using the taglib directive.
1) The jsp:include Action:- The JSP specification defines the include action for including static and dynamic resources in a JSP page. With the include directive the contents of the included resource is substituted into the JSP page at translation phase but with the include action the response of the included resource is added to the current response output stream during the request processing phase.
The following code shows how we can use the include action:
Code:
<jsp:include page="includedPage.jsp" flush=”true” />
Page= The resource to include.
Flush= this is optional with the default value of false. If the value is true the buffer in the output Stream is flushed before the inclusion is performed.
This action will include the output of processing includedPage.jsp within the output of the JSP page during the request processing phase.
There are some important points to note regarding the include action and directive:
Changing the content of the included resource used in the include action is automatically reflected the next time the including JSP is accessed. The include directive is normally used for including both dynamic and static resources, the include action is used for including only dynamic resources.
Consider the Following example:
Webapps\jsp\includeAction.jsp
Code:
<html> <head> <title> Include Action Test Page </title> </head> <body> <h1>Include Action Test Page</h1> <h2>Using the include directive</h2> <%@ include file="include2.html" %> <%@ include file="include2.jsp" %> <h2>Using the include action</h2> <jsp:include page="include2.html" flush="true" /> <jsp:include page="include2.jsp" flush="true" /> </body> </html>
Code:
<html> <head> <title>Included HTML</title> </head> <body> This is some static text in the included HTML file </body> </html>
Code:
<html> <head> <title>Included JSP</title> </head> <body> <h3> The date is <% out.println((new java.util.Date()).toString()); %> </h3> </body> </html>
1) The jsp:forward Action:- The JSP specification defines the forward action to be used for forwarding the response to other web application resources. The forward action is the same as forwarding to resources using the RequestDispatcher interface.
The following code shows how to use the forward action:
Code:
<jsp:forward page="Forwarded.html"/>
We can only forward to a resource if content is not committed to the response output stream from the current JSP page. If content is already committed, an IllegalStateException will be thrown. To avoid this we can set a high buffer size for the forwarding JSP page.
Forward.html
Code:
<html> <head> <title>Forward Action Test Page</title> </head> <body> <h1>Forward Action Test Page</h1> <form method="post" action="forward.jsp"> <p>Please enter your username: <input type="text" name="username"> <br> and passowrd: <input type="password" name="password"> </p> <p><input type="submit" value="Log In"> </form> </body> </html>
Code:
html> <head> <title>Forwarded JSP</title> </head> <body> <% if ((request.getParameter("username").equals("Admin")) && (request.getParameter("password").equals("Admin"))){ %> <jsp:forward page="forward2.jsp" /> <% } else { %> <%@ include file="forward.html" %> <% } %> </body> </html>
Code:
<html> <head> <title> Forward action test: Login Successful</title> </head> <body> <h1>Forward action test: Login Successful</h1> <p> Welcome , <%= request.getParameter("username") %></p> </body> </html>
Then login with username=Admin and password=Admin
3) The jsp: param Action:-
The JSP param action can be used in conjunction with the include and forward actions to pass additional request parameters to the included or forwarded resource. The param tag needs to be embedded in the body of the include or forward tag.
The following code shows how we can use the param action:
Code:
<jsp:forward page="Param2.jsp"> <jsp: param name="name" value="Techgeek"/> </jsp:forward>
Using JavaBeans with JSP Pages
The JSP specification defines a powerful standard action that helps to separate presentation and content by allowing page authors to interact with JavaBean components that are stored as page, request, session and application attributes. The useBean tag along with the getProperty and setProperty tags allows JSP pages to interact with JavaBean components.
4) The jsp:useBean Action:-
The useBean action creates or finds a Java object in a specified scope. The object is also made available in the current JSP page as a scripting variable. The syntax for the useBean action is:
Code:
<jsp:useBean id="name" scope="page | request | session | application" class="className" type="typeName" | bean="beanName" type="typeName" | type="typeName" />
First, the container tries to find an object by the specified name in the specified scope.
If an object is found, a scripting variable is introduced with the name specified by the id attribute and type specified by the type attribute. If the type attribute is not specified the value of the class attribute is used. The variables value is initialized to the reference to the object that is found. The body of the tag is then ignored.
If the object is not found and both class and beanName are not present, an InstantiationException is thrown.
If the class defines a non-abstract class with public no-argument constructor an object of that class is instantiated and stored in the specified scope using the specified ID. A scripting variable is introduced with the name specified by the id attribute and type specified by the class attribute. If the value of the class attribute specifies an interface, a non-public class or a class without a public no-argument constructor, an InstantiationException is thrown. The body of useBean of the tag is then executed.
If the beanName attribute is used then the java.beans.Bean.instantiate() method is called, passing the value of the attribute beanName. A scripting variable is introduced with the name specified by the id attribute and type specified by the type attribute. The body useBean of the tag is then executed.
For example:
Code:
<jsp:useBean id="myName" class="java.lang.String" scope="request"> </jsp:useBean>
5) The jsp:getProperty Action:-
The getProperty action can be used in conjunction with the useBean action to get the value of the properties of the bean defined by the useBean action. For example:
Code:
<jsp:getProperty name="myBean" property="firstName"/>
6) The jsp:setProperty Action:-
The setProperty action can be used in conjunction with the useBean action to set the properties of a bean. We can even get the container to populate the bean properties from the request parameters without specifying the property names. In such cases the container will match the bean property names with the request parameter names. The syntax for the setProperty action is shown below:
Code:
<jsp:setProperty name="beanName" property="*" | property="propertyName" | property="propertyName" param="paramName" | property="propertyName" value="value" />
Code:
<jsp:setProperty name="myBean" property="name" value="harsh"/>
Code:
<jsp:setProperty name="myBean" property="name1" param="name2"/>
Code:
<jsp:setProperty name="myBean" property="name1"/>
Code:
<jsp:setProperty name="myBean" property="*"/>
Webapps\jsp\beans.html
Code:
<html> <head> <title>useBean Action Test Page</title> </head> <body> <h1>useBean Action Test Page</h1> <form method="post" action="beans.jsp"> <p> Please enter your username: <input type="text" name="name"> <br> What is your favourite programming language? <select name="language"> <option value="Java">Java</option> <option value="C++">C++</option> <option value="Perl">Perl</option> <option value="C">C</option> </select> </p> <input type="submit" value="Submit Information"> </form> </body> </html>
Code:
<html> <head> <title>useBean Action Test Page</title> </head> <body> <h1>useBean Action Test Page</h1> <form method="post" action="beans.jsp"> <p>Please enter your username: <input type="text" name="name"> <br> What is your favourite programming language? <select name="language"> <option value="Java">Java</option> <option value="C++">C++</option> <option value="Perl">Perl</option> <option value="C">C</option> </select> </p> <input type="submit" value="Submit Information"> </form> </body> </html>
webapps\jsp\com\LanguageBean.class
Code:
package com; public class LanguageBean { private String name; private String language; public LanguageBean(){ } public void setName(String name){ this.name=name; } public String getName() { return name; } public void setLanguage(String language){ this.language=language; } public String getLanguage() { return language; } }
6) The jsp: plugin Action:-
The plugin action enables the JSP container to render appropriate HTML to initiate the download of the Java plugin and the execution of the specified applet or bean, depending on the browser type. The plugin standard action allows us to embed applets and beans in a browser-neutral manner as the container takes care of the user agent specific issues. For example:
Code:
<jsp: plugin type="applet" code="MyApplet.class" codebase="/"> <jsp: params> <jsp: param name="myParam" value="123"/> </jsp: params> <jsp:fallback><b>Unable to load applet</b></jsp:fallback> </jsp: plugin>
No comments:
Post a Comment