JSP Element | Syntax | Interpretation | Notes |
JSP Expression | <%= expression %> | Expression is evaluated and placed in output. | XML equivalent is <jsp:expression> expression </jsp:expression>. Predefined variables are request, response, out, session, application,config, and pageContext (available in scriptlets also). |
JSP Scriptlet | <% code %> | Code is inserted in service method. | XML equivalent is <jsp:scriptlet> code </jsp:scriptlet>. |
JSP Declaration | <%! code %> | Code is inserted in body of servlet class, outside of service method. | XML equivalent is <jsp:declaration> code </jsp:declaration>. |
JSP page Directive | <%@ page att=”val” %> | Directions to the servlet engine about general setup. | XML equivalent is <jsp:directive.page att=”val”\>. Legal attributes, with default values in bold, are:import=”package.class“contentType=”MIME-Type” isThreadSafe=”true|false” session=”true|false” buffer=”sizekb|none” autoflush=”true|false” extends=”package.class” info=”message” errorPage=”url” isErrorPage=”true|false” language=”java” |
JSP include Directive | <%@ include file=”url” %> | A file on the local system to be included when the JSP page is translated into a servlet. | XML equivalent is <jsp:directive.include file=”url”\>. The URL must be a relative one. Use the jsp:include action to include a file at request time instead of translation time. |
JSP Comment | <%– comment –%> | Comment; ignored when JSP page is translated into servlet. | If you want a comment in the resultant HTML, use regular HTML comment syntax of <– comment –>. |
The jsp:includeAction | <jsp:includepage=”relative URL”
flush=”true”/> |
Includes a file at the time the page is requested. | If you want to include the file at the time the page is translated, use the page directive with the includeattribute instead. Warning: on some servers, the included file must be an HTML file or JSP file, as determined by the server (usually based on the file extension). |
The jsp:useBeanAction | <jsp:useBean att=val*/> or <jsp:useBean att=val*> … </jsp:useBean> |
Find or build a Java Bean. | Possible attributes are:id=”name”
scope=”page|request|session|application” class=”package.class” type=”package.class” beanName=”package.class“ |
Thejsp:setPropertyAction | <jsp:setProperty att=val*/> | Set bean properties, either explicitly or by designating that value comes from a request parameter. | Legal attributes arename=”beanName”
property=”propertyName|*” param=”parameterName” value=”val“ |
Thejsp:getPropertyAction | <jsp:getPropertyname=”propertyName”
value=”val“/> |
Retrieve and output bean properties. | |
The jsp:forward Action | <jsp:forwardpage=”relative URL“/> | Forwards request to another page. | |
The jsp:plugin Action | <jsp:pluginattribute=”value“*>
… </jsp:plugin> |
Generates OBJECT or EMBED tags, as appropriate to the browser type, asking that an applet be run using the Java Plugin. |
Sharing Bean : page scope
c:\tomcat\webapps\mca4\WEB-INF\classes\bd\StudInfo.java
c:\tomcat\webapps\mca4\WEB-INF\classes\bd\StudInfo.class
package bd; public class StudInfo { private String name="abc"; private int age=12; private String gender="male"; public void setName (String name) { this.name = name; } public String getName() { return name; } public void setAge (int age) { this.age = age; } public int getAge() { return age; } public void setGender (String gender) { this.gender = gender; } public String getGender() { return gender; } }
c:\tomcat\webapps\mca4\form.html
<html> <body> <form action="beanpagescope.jsp"> Enter Name : <input type="text" name="name"> Enter Age : <input type="text" name="age"> Gender : <input type="radio" name="gender" value="m">Male <input type="radio" name="gender" value="f">Feale <input type="submit"> </form> </body> </html>
c:\tomcat\webapps\mca4\beanpagescope.jsp
<jsp:useBean id="studInfoBean" class="bd.StudInfo" scope="page"/> <jsp:setProperty name="studInfoBean" property="*"/> Name : <jsp:getProperty name="studInfoBean" property="name"/> Age :<jsp:getProperty name="studInfoBean" property="age"/> Gender :<jsp:getProperty name="studInfoBean" property="gender"/>
Note : Request parameter and Bean property must match.
Default value of scope is “page”
<jsp:useBean> action example
Storing form data to bean
c:\tomcat\webapps\mca4\WEB-INF\classes\bd\StudInfo.java
c:\tomcat\webapps\mca4\WEB-INF\classes\bd\StudInfo.class
package bd; public class StudInfo { private String name="abc"; private int age=12; private String gender="male"; public void setName (String name) { this.name = name; } public String getName() { return name; } public void setAge (int age) { this.age = age; } public int getAge() { return age; } public void setGender (String gender) { this.gender = gender; } public String getGender() { return gender; } }
c:\tomcat\webapps\mca4\form.html
<html> <body> <form action="beanprocess.jsp"> Enter Name : <input type="text" name="name"> Enter Age : <input type="text" name="age"> Gender : <input type="radio" name="gender" value="m">Male <input type="radio" name="gender" value="f">Feale <input type="submit"> </form> </body> </html>
c:\tomcat\webapps\mca4\beanprocess.jsp
<jsp:useBean id="studInfoBean" class="bd.StudInfo"/> <jsp:setProperty name="studInfoBean" property="name" value='<%=request.getParameter("name")%>'/> Name : <jsp:getProperty name="studInfoBean" property="name"/> <jsp:setProperty name="studInfoBean" property="age" value='<%=Integer.parseInt(request.getParameter("age"))%>'/> Age :<jsp:getProperty name="studInfoBean" property="age"/> <jsp:setProperty name="studInfoBean" property="gender" value='<%=request.getParameter("gender")%>'/> Gender :<jsp:getProperty name="studInfoBean" property="gender"/>
<jsp:useBean> action example to create simple bean
c:\tomcat\webapps\mca4\WEB-INF\classes\bd\StudInfo.java
c:\tomcat\webapps\mca4\WEB-INF\classes\bd\StudInfo.class
package bd; public class StudInfo { private String name = ""; public void setName(String name) { this.name = name; } public String getName() { return (name); } }
c:\tomcat\webapps\mca4\beandemo.jsp
<jsp:useBean id="studBean" class="bd.StudInfo"/> <jsp:setProperty name="studBean" property="name" value="abc"/> Name : <jsp:getProperty name="studBean" property="name"/> <% studBean.setName("def"); studBean.getName(); %>
<jsp:plugin> action example to access applet in jsp page
c:\tomcat\webapps\mca4\applet\Applet1.java
c:\tomcat\webapps\mca4\applet\Applet1.class
<span style="line-height: 1.4;">import java.awt.*;</span> import java.applet.*; public class applet1 extends Applet { public void paint(Graphics g) { g.drawString("Test",80,20); } }
c:\tomcat\webapps\mca4\appletplugin.jsp
<jsp:plugin type="applet" code="applet1.class" codebase="applet" height="300" width="300"/>
Here, code –> specify class file of applet
codebase –> sub-directory where applet class file is stored
type –> applet to display applet