When the bill amount exceeds Rs. 1000 then else part of calc.jsp gets executed and it forwards the request to discount.jsp page . [Read more…] about Bill Discount JSP Program
JSP Directive
JSP Session
JSTL
JSP Elements
Java Server Pages
Bill Discount JSP Program
JSP:forward tag
Standard syntax
<j sp: forward page= “desturl” />
This tag is used to forward the request from one JSP program to another JSP program, which participate in the chaining. This tag internally uses rd.forward(-,-) method. The task of this tag is similar to that of the forward mode of the servlet chaining. By using this tag, it can forward the request from source JSP program to the destination web resource program. But this tag discards the HTML output of the source JSP program and displays the HTML output of the destination JSP program.
“input.htm”
<body bgcolor=”cyan” >
<center>
<form action=”Square.jsp”>
ENTER NUMBER <input type=”text” name=”t1″ />
<br>
<input type=”submit” value=”Find” />
<input type=”reset” value=”Reset” />
</form>
input.html takes the request from user and then forwards it to Square.jsp page.
“Square.jsp”
<center>
<font color=”red” size=”5″>
SQUARE OF <%=request.getParameter(“t1”) %> IS:
<%
String num=request.getParameter(“t1”) ;
int n=Integer.parseInt(num);
if(n<50)
{
out.println(n*n);
}
else
{
%>
<jsp:forward page=”Cube.jsp”>
<jsp:param name=”num” value=”<%=n %>” />
</jsp:forward>
<%
}
%>
After getting the given number from the input.htrnl page, Square.jsp, calculates the
square of the program.
When an user enters a number more than 50, then the else block of Square.jsp gets
executed, where the following logic is written:
<jsp:forward page=”Cube.jsp” >
<jsp:param name::;”num” value::”<%:=n %>” />
</jsp:forward>
for forwarding the request to Cube.jsp page. Then Cube.jsp code gets executed.
“Cube.jsp”
<center>
<font color=”red” size=”5″>
CUBE OF <%=request.getParameter(“num”) %> IS:
<%
String num=request.getParameter(“num”);
int n=Integer.parseInt(num);
int z=n*n*n;
out.println(z);
%>
</font>
</center>
JSP Action Include Tag
Standard syntax
<% @ include file= “desturl” %>
XML syntax
<JSP: directive. include file= “desturl” />
This tag is given to include the code of the destination web resource program to the source code of the JSP equivalent servlet program that belongs to the source JSP program. In this case, the destination web resource will not be executed separately, rather by using this concept, the code of the destination web resource program is included within the JSP’ equivalent servlet program of the source JSP program.
The directive include tag does not perform output inclusion rather it performs code inclusion.
The <]SP: include> tag is given to include the output of the destination web resource program. The <]SP: include> tag is used for output, not the code of the destination web resource program. In this case, there are two separate ]SP equivalent servlet programs, which are generated and executed separately and the output generated for the destination web resource program aSp program) that is included in the output of the source web resource program which is also a ]SP program. So, we can say that <]SP: include>tag is a tag based alternate for “rd. include” of the servlet program. These action tags of ]SP can be used only with XML syntax as compared to the other tags of the JSP.
“First.jsp”
<center>
<font color=”red” size=”5″ >
<br><br>
BEGINNING OF FIRST JSP
<BR>
PASSING SOME DATA FROM FIRST.JSP TO SECOND. JSP
<BR></FONT>
<jsp:include page=”Second.jsp” >
<jsp:param name=”name” value=”Neeraj Dadwal” />
<jsp:param name=”roll” value=”1″ />
</jsp:include>
<BR>
<font color=”red”>END OF FIRST JSP</font>
</center>
“Second.jsp”
<%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c” %>
<html>
<head>
<title> jsp include</title>
</head>
<body>
<center>
<font color=”green” size=”5″ >
<br><br>
BEGINNING OF SECOND JSP
<br>
NAME : <%=request.getParameter(“name”) %>
<br>
END OF SECOND JSP
<br>
</font>
</center>
</body>
</html>
Here, the output comes from First.jsp by including the output of Second.jsp inside its body.
Servlet to JSP Communication
The intention is to introduce the communication in between the servlet and JSP. The welcome page is designed by the HTML code.
<html>
<title>Srv-Jsp Communication</title>
<form action=”a.jsp”>
<body>
<center><font color=”black” size=”8″>Enter A Number:</font>
<input type=’text’size=’50’ name=”t1″><br><br>
<input type=’submit’ value=’submit’ name=”b1″ />
<input type=’reset’ value=’reset’ name=’b2’/>
</center>
</body>
</form>
</html>
Output:
“a.jsp”
<html>
<%
try
{
int no=Integer.parseInt(request.getParameter(“t1”));
int res=no*no;
if(res>=100)
{
out.println(“<center><font color=’red’ size=’8′>”);
out.println(“Square of The Number is=”+res);
out.println(“</font></center>”);
}
else
{
out.println(“<br><br><center><font color=’red’ size=’6′>Your selection Does
not Vary The Condition..”);
}
}
catch(Exception e)
{
System.out.println(e);
}
%>
</html>
Output:
Explanation of the above program
The aim of this program is to find out the square of the number if the result is greater than 100, then it goes to the JSP program, otherwise it goes to servlet program.
As the JSP program communicates, with the servlet program, the URL pattern of the servlet program is passed into the form action.
• int no=Integer.parseInt(request.getParameter(“tl”));
The number which is passed that is read by the getParameter() and assign in the no variable of int type.
• int res=no*no;
The square of the number is calculated and assigned to the res variable of int type.
• if(res>=100)
{
out.println(“<center><font color=’red’ size=’8′>”) ;
out.println(“Square Of The Number Is=”+res);
out.println(“</font></center>”) ;
}
If the result requested is greater than 100, then it calculates the square of the number.
• else
{
RequestDispatcher=request.getRequestDispatcher{“/Srv·);
rd.forward(request,response);
}
If the result is less than 100,we pass the request then through the RequestDispatcher object to the servlet program, where the cube of the number is calculated.
• catch(Exception e)
{
System.out.println(e) ;
}
The exception is handled by the catch block.
The square of the number 25 is 625 and it is greater than 100, so the request goes to the SP page and processing is done.
But if the square of the number is greater than or less than 100, then the request goes to the servlet program and calculates the cube of the number.
This time we pass the number 2 and the square of the number is 4 and it is less than 100, so the request goes to the Servlet program through the RequestDispatcher object that’s why we pass the URL pattern of the servlet program in RequestDispatch method.
Database To JSP Communication
In the below JSP program the aim is to understand how JSP communicates with the database. The designing is done by HTML.
HTML Program
<html>
<title>DB-JSP</title>
<form action= “DB-JSP.jsp”>
<body bgcolor= ‘white’>
<h1 align=”center”><font color= ‘Black’ size=’8′ >Search Employee Details</font></h1>
<br><Center><font color= ‘Gray’ size= ‘6’ >Enter Employee Id</font>
<input type=’text ‘ size= ’20’ name=’t1′>
<input type=’Submit’ value=’Submit’ name=”b1″ />
<input type=’Reset’ value= ‘RESET’ name= ‘b2’/>
</center>
</body>
</form>
</html>
Output:
JSP Program
<%@ page import=”java.sql.*” %>
<%
try
{
String driver = “com.mysql.jdbc.Driver”;
String url = “jdbc:mysql://localhost/dbase”;
String uid = “root”;
String psw = “root”;
Class.forName(driver) ;
Connection con=DriverManager.getConnection (url,uid,psw) ;
Statement st=con.createStatement ();
String empno=request.getParameter(“t1”);
ResultSet rs = st.executeQuery(“select * from emp where EMPNO=”+empno);
if (rs.next ())
{
out.println(“<center><table border=’15’ color=’red’><tr><th>EMPNAME
</th><th>EMPNO</th><th>SAL</th>
<th>EMPADD</th><th>MAILID</th><th>PHNO.</th>< /tr>”);
out.println(“<tr><td>”+rs.getString(1)+”</td>”);
out.println(“<td>”+rs.getString(2)+”</td>”);
out.println(“<td>”+rs.getString(3)+”</td>”);
out.println(“<td>”+rs.getString(4)+”</td>”) ;
out.println(“<td>”+rs.getString(5)+”</td>”) ;
out.println(“<td>”+rs.getString(6)+”</td></tr>”);
out.println(“</table></center>”);
}
else
{
out.println(“<center><font color=’red’ size=’7′>Invalid Id Try Again</font></center>”);
}
}
catch(Exception ee)
{
System.out.println(ee);
}
%>
Output:
Explanation of the above program
• Class.forName(“oracle.jdbe.driver.Oracle Driver”);
It loads the type-4 driver.
• Connection
con=DriverManager.getConnection(” jdbe:oraele:thin:@localhost:1521:xe”,”system”,”manager”);
It establishes the connection.
• Statement st=con.createStatement();
It creates the statement.
• String empno=request.getParameter(“tl”);
It reads the employee number through the getParameter(); of the request interface.
• ResuItSet rs=st.exeeuteQuery(“select * from EMP where EMPNO=” +empno);
This statement executes the query and retrieves the information from the EMP table and stores all the value in the ResultSet object.
• if(rs.next())
If (rs.next) returns true then a table will be formed and all the values which are retrieved from the EMP table of the database are stored in that table and displayed on the browser through the out.println () ;.
else{
• out.println(“<center><font color=’red’ size=’7′>Invalid Id Try Again</font></center>”);
If (rs.next) returns falls then it print “Invalid Id Try again” on the browser window as the response.
• catch(Exception ee)
{
• System.out.println(ee);
}
The generated exception is handled by the catch block.
Html To JSP Communication
When we send any request to the JSP program from the browser window, we pass the request URL in the browser window. In this process to send the data along with the request, the programmer also requires adding the query string to request URL explicitly. But this task is highly of technical. For that a graphical user interface is required which can generate a request which mayor may not hold the data. For this purpose, the programmer can use either HTML form or hyperlink to generate the request with or without data. The example of HTML to JSP communication is given below:
Program:
<html>
<title>HTML TO JSP COMM</title>
<body>
<center>
<font color=”blue” size=”8″><b>Date & Time is :</b></Font></center>
<h3 align=”center”><%java.util.Date d=new java.util.Date();out.println(d.toString());%>
</center>
</body>
</html>
Explanation of the above program
In order to display the date and time within the scriptlet tag (<%——-%>) we use the Date class, which prints the current date and time in the browser window. This Date class present in the util package, that’s why it needs to be imported.
<% java.util.Date d=new java.util.Date();
out.println(d.toString()) ;%>
Jsp readingTextFile Example
In this Example we will discuss about how to read text file with the JSP objects. First we create a text file that contains strings in it. In the program we use input and output stream for execute the expression.
We put the text file in the WEB/INF folder where it will be fetched in the program we just give the reference of this folder. For storing the content of a file we also use the buffered reader function. readLine method also used to read the content of text file stored in WEB/INF folder.
<%@ page import=”java.io.* “%>
<html>
<head>
<title><ReadingTextFile> In JSP</title>
</head>
<body bgcolor=”#E1EDF3″>
<h3 align=”center”><br><%
String fileName = “/WEB-INF/Reading.txt”;
InputStream instrm = application.getResourceAsStream(fileName);
try
{
if(instrm == null)
{
response.setStatus(response.SC_NOT_FOUND);
}
Else
{
BufferedReader bfrdr = new BufferedReader((new InputStreamReader(instrm)));
String cntnt;
while((cntnt= bfrdr.readLine())!= null)
{
out.println(cntnt+”<br>”);
}
}
}
catch(IOException e1)
{
out.println(e1.getMessage());
}
%></h3></br>
</body>
</html>
Jsp serverSnoop Example
This Example is use to get the Information about Server’s Information. Like port number, server name.
<html>
<head>
<title><SnoopServer></title>
</head>
<body bgcolor=”#CAE0EC”>
<table border=”1″ align=”center” cellpadding=”2px” cellspacing=”2px” align=”center”>
<tr>
<span><h2 align=”center”>Server Information</h2></td></span>
</tr>
<tr>
<td>Server Name:</td> <td><%=request.getServerName() %></td>
</tr>
<tr>
<td>ServerPort: </td> <td><%=request.getServerPort() %></td>
</tr>
<tr>
<td>Query String: </td> <td><%= request.getQueryString() %></td>
</tr>
<tr>
<td>Servlet Path: </td> <td><%= request.getServletPath() %></td>
</tr>
<tr>
<td>Server Protocol:</td> <td><%= request.getProtocol() %></td>
</tr>
<tr>
<td>Server Scheme:</td> <td><%=request.getScheme() %></td>
</tr>
<tr>
<td>RemotePort = </td> <td> <%=request.getRemotePort() %></td>
</tr>
<tr>
<td>Remote Host = </td> <td> <%=request.getRemoteHost() %></td>
</tr>
<tr>
<td>Request URI: = </td> <td><%= request.getRequestURI() %></td>
</tr>
<tr>
<td>Request Method: = </td> <td><%= request.getMethod() %></td>
</tr>
<tr>
<td>Content Length: = </td> <td><%= request.getContentLength() %></td>
</tr>
</table>
</body>
</html>
jsp pageCount Example
In this example we will define that about page counter every time a user visit the page will be counted. In this we made a program where i static click variable initialize and the value declare 0. If any user visit site any tym it will display the visit number. With help of for loop it will increase the visits one by one.
<html>
<head>
<title><Jsp Page Counter></title>
</head>
<body bgcolor=”#F8E4E4″>
<%!static int click=0; %>
<%
if (click == 0)
{
%>
<br><h3 align=”center”><% click ++;
out.println(“Welcome To Java Server Page Counter”); %> </br>
<% out.println(“Visitor Number : “);%> <%=click %>.</h3>
<%
click++;
}
else
{
%>
<br><h3 align=”center”><% out.print(“Welcome…!”);%></br>
<h3 align=”center”><% out.print(“You are Visitor Number : “);%> <%=click %>.</h3>
<%
click++;
}
%>
</body>
</html>
Jsp ElIgnored Example
In this example we will explain about ElAttribute EL(Expression Language). As jsp used all the Expressions that are linked with EL attributes by default. Bt if user need to ignore them manually user has to change page directive ‘true’.
This is the main concept which we are discussing ignoring the ElExpressions. In the program that we present the condition is to concat the two strings with each other bt when in the program page directive will true it will ignore the expression of jsp function. The page directive has to be false that is by default in the jsp attribute. And we also use concat function in the program so far….
<%@ page isELIgnored=”false” %>
<%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c” %>
<html>
<head>
<title><isELIgnored> In JSP</title>
</head>
<body>
<br><h3 align=”center”>The Demo Of “isELIgnored Attribute” In JSP</h3></br>
<c:set var=”jsp1″ value=”Jakarta”/>
<c:set var=”jsp2″ value=”java”/>
<c:choose>
<c:when test=”${jsp1 eq ‘Jakarta’}”>
<c:set var=”concat” value=”${jsp1}${jsp2}” />
<h3 align=”center”><br><c:out value=”${concat}”></h3></br></c:out>
</c:when>
<c:otherwise>
<h3 align=”center”>”${jsp1}”</h3>
</c:otherwise>
</c:choose>
</body>
</html>
Jsp Expression Example
Jsp expressions known as those kind of expressions that are containing the scripting language values. To Display the expression value we create a HTML tags page that will use to fill the required fields that will be displayed according to need.
Here we redirect the html page on an actual page where all the functions will be elaborate. As for this we made two forms like first which will use as the HTML code then second that will take the reference of first page. The value which it will receive from first form will displayed on the web browser as Output both form contains the required attribute and java packages to for io exceptions etc..let’s take a look on the program we made.
<html>
<head>
<title><jsp_Expression></title>
</head>
<body>
<form action=”Expression.jsp” method=”get”>>
<table cellpadding=”2px” cellspacing=”1px” bgcolor=”#f7f9fb” width=”400px”align=”center”>
<tr>
<td align=”center” colspan=”2″>
<span class=”text”><h2>Identity</h2></span></td>
</tr>
<tr>
<td align=”center” width=”60%”>User Name: <input type=”text” name=”text”maxlength=”40″/></td>
</tr>
<tr>
<td align=”center”><input type=”submit” value=”Submit” /></td>
</tr>
</form>
</table>
</body>
</html>
Expression.jsp
<html>
<head>
<title><Expression></title>
</head>
<body>
<%String identity= request.getParameter(“text”);%>
<%
if(identity!=null)
{
%>
<b><br><br><h2 align=”center”>
<%=identity %></h2></b></br>
<%
}
%>
</body>
</html>
Jsp getStatus Example
In this example we will explain the get status method() this method is referred to HttpServletResponse interface. The status we set will be diaplayed.
For example in the program we make a demo like to get status with domain name (web sites).here we also made two form first as designing page of demo example and the second one is to show all the criteria it does on the condition. let’s take a look…..
<html>
<head>
<style>
.label {
font-family:”Palatino Linotype”, “Book Antiqua”, Palatino, serif;
font-size:14px;
color:#0e0e0e;
}
.border {
border:solid 2px #0448e0;
margin-top:70px;
}
.text {
font-family:”MS Serif”, “New York”, serif;
font-size:16px;
color:#070200;
}
</style>
<title>status</title>
</head>
<body>
<form action=”JavaExampleJSP_getStatus.jsp” method=”get”>
<table cellpadding=”2px” cellspacing=”1px” bgcolor=”#F4F5F7″ width=”400px” class=”border” align=”center”>
<tr>
<td colspan=”2″ bgcolor=”#0066FF”> </td>
</tr>
<tr>
<td colspan=”2″ class=”label”> </td>
</tr>
<tr>
<td align=”center” colspan=”2″>
<span class=”text”><b>Enter Contents</b></span>
</td>
</tr>
<tr>
<td colspan=”2″ class=”label”> </td>
</tr>
<tr>
<td class=”label” align=”right” width=”50%”><b>Enter Web Site Name:</b>
<br>(eg. google,yahoo)</br></td>
<td align=”left” width=”50%”><input type=”text” name=”ws” maxlength=”20″/></td>
</tr>
<tr>
<td class=”label” align=”right” width=”50″><b>Enter Domain Name:</b>
<br>(like in,org,com)</br></td>
<td align=”left” width=”50″><input type=”text” name=”dn” maxlength=”20″ /></td>
</tr>
<tr>
<td class=”label” align=”right”> </td>
<td align=”left”><input type=”submit” value=”Submit” /></td>
</tr>
<tr>
<td colspan=”2″ class=”label”> </td>
</tr>
</table>
</form>
</body>
</html>
getStatus.jsp
<%@ page import=”java.net.URL, java.util.*” %>
<html>
<head>
<title><getStatus> In JSP</title>
</head>
<body>
<%
String web = request.getParameter(“ws”);
String domain = request.getParameter(“dn”);
URL Nwurl = new URL(“http://”+web+”.”+domain);
String url = Nwurl.toString();
If(web != null)
{
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader(“Location”, url);
}
%>
</body>
</html>
Jsp setDateHeader Example
This example will explain that the HttpServletResponse setDateHeader() method is used to set date with header name.in the example we use util package that will allow us to access the mandatory library for date initializing on the web browser as output.
<%@ page import=”java.util.*” %>
<html>
<head>
<title><dateHeader>In Jsp</title>
</head>
<body>
<center><br><b>
<%
long dt = new Date().getTime();
response.setDateHeader(“Date”, dt);
out.println(“Date is : “+response.getHeader(“Date”));
%></b></center></br>
</body>
</html>
Jsp headerRequest Example
Header Request is a request information that the user make request to the server. This will display the header information like local host name, request path, query generation. Here we made a program that will demonstrate the header information with as output on the web browser. Some packages like enumeration also declared for coding.
<%@ page import=”java.util.Enumeration” %>
<html>
<head>
<title><Header Request In JSP></title>
</head>
<body bgcolor=”#F3EDE9″>
<table border=”1″ align=”center” cellpadding=”2px” cellspacing=”2px align=”center”>
<tr>
<span><h2 align=”center”>Header’s Request info</h2></span>
</tr>
<%! Enumeration en, hdr;
String hdrname, hdrvl;
%>
<%
en = request.getHeaderNames();
while (en.hasMoreElements())
{
hdrname = (String) en.nextElement();
hdr = request.getHeaders(hdrname);
if(hdr != null)
{
while(hdr.hasMoreElements())
{
hdrvl= (String) hdr.nextElement();
}
}
%>
<tr>
<td align=”center” ><%= hdrname %></td>
<td align=”center”><%= hdrvl %></td>
</tr>
<%
}
%>
</table>
</body>
</html>
jsp:useBean tag Example
Jsp Beans known as a property attribute that is been used for the accessing the user of object the content may be java class or package that is been used to access the class field of object.
In simple words if we explain jsp beans this attribute use to access the property from a class file file must be java type or other as mentioned. In this example we use to display the property on the web browser we made a package named ‘p1’ in this we made a java file which class file will be use to access the data. In java file we create a program that is been use for getting and putting the value from property contents. Then we create a jsp file that will use the class file of java type while executing for output on the web browser. Attributes that are use to initialize like set property or beans id…let’s take a look on program.
<%@page import=”p1.jspbean”%>
<html>
<head>
<title><jsp_bean></title>
</head>
<body>
<jsp:useBean id=”bean” class=”p1.jspbean”>
<br><h3 align=”center”><jsp:setProperty name=”bean” property=”str” value = “Java Server Pages” /></center></br>
</jsp:useBean>
<p> <jsp:getProperty name=”bean” property=”str” /></p>
</body>
</html>
Java File used In Program.
package p1;
public class jspbean
{
String str;
public String getStr()
{
return str;
}
public void setStr(String str)
{
this.str = str;
}
}
JSP Forward Action Tag Example
In this Example we will learn how to forward the client request or content on another web page. That requested content may be like HTML page, JSP or Servlet etc. The Main criteria of this example is to show how the content forward on another web page.
For this we also use two JSP forms in first we will use to enter the content which will have to forward on another web page as required in first form we use to code for design the web page. More we use HTML coding for that kind of designing criteria. We use attribute like forward page or param name for content referring. In the second page we create main functionality coding from that the output will be displayed on the web browser. The first form will use the redirection of the second one so that will execute both on the web browser as output. Some required JSTL tags or the java functions also use for this form…Lets Take a Look on The programmes…
<html>
<head>
<title><JSP_forward></title>
</head>
<body>
<jsp:forward page=”next.jsp”>
<jsp:param name= “U_Name” value=”Yaduvanshi Ash”/>
</jsp:forward>
</body>
</html>
Next.jsp
<html>
<head>
<title><Forwarded_Action Example in JSP></title>
</head>
<body>
<%
String name= request.getParameter(“U_Name”);
%>
<%
if(name != null)
{
%>
<b><br><br><h2 align=”center”>
<%=name%></h2></b></br>
<%}
%>
</body>
</html>
Jsp appletPlugin Example
In this Example we will discuss that we can include an applet or JavaBeans component in a JSP page by using the jsp:plugin
element.
This will generates the HTML content that will contain the client-browser constructs. In the program we use java class for fetching the result an applet code also.Whenever this will be executed the appropriate output on the web browser.
<jsp:plugin type=”applet” code=”Pluginjsp.class” codebase=”p1″ width = “250” height = “250”>
<jsp:fallback>
<p>Applet can’t be load</p>
</jsp:fallback>
</jsp:plugin>
Pluginjsp.java
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.io.*;
public class Pluginjsp extends Applet
{
public void paint(Graphics gr)
{
Calendar cl = new GregorianCalendar();
String hr = String.valueOf(cl.get(Calendar.HOUR));
String mnt = String.valueOf(cl.get(Calendar.MINUTE));
String scnd = String.valueOf(cl.get(Calendar.SECOND));
gr.drawString(hr + “:” + mnt + “:” + scnd, 20, 30);
}
}
Jsp getProperty Example
This example will explain that how we get the property from the beans components with getter method using. It will display the value on output destination. To implify this program we just got the java file and will fetch the property from java class file.
This java file only contain the getter method that is been used to access the output. We also made two form first one is for designing and will take the reference of 2nd form and second will does the rest of work to execute the condition it self.
<html>
<head>
<title>getProperty</title>
</head>
<body bg color=“#D4E0DF”>
<formaction=“JavaExampleJSP_getProperty.jsp” method=“get”>
<tablecellpadding=“2px” cellspacing=“1px” bgcolor=“#f7f9fb” width=“400px“align=“center”>
<tr>
<tdalign=“center” colspan=“2“>
<spanclass=“text”><h2>Employye’s Info</h2></span></td>
</tr>
<tr>
<tdalign=“center” width=“60%”>Enter Employee Name: <inputtype=“text” name=“nm” maxlength=“40“/></td>
</tr>
<tr>
<tdalign=“center” width=“60%”>Enter Employee’s Job: <inputtype=“text” name=“jb” maxlength=“40“/></td>
</tr>
<tr>
<tdalign=“center” width=“60%”>Enter Employee’s Company: <inputtype=“text” name=“cm” maxlength=“40“/></td>
</tr>
<tr>
<td><center><inputtype=“submit” value=“submit” /></center></td>
</tr>
</table>
</form>
</body>
</html>
getProperty.jsp
<html>
<head>
<title><getProperty>Example In JSP</title>
</head>
<body bg color="#D4E0DF">
<%
String empName = request.getParameter("nm");
String job = request.getParameter("jb");
String company = request.getParameter("cm");
If(empName != null)
{
%>
<jsp:useBean id="wrkr" class="p2.emp" scope="request"/>
<jsp:setProperty property="empName" value="<%=empName %>" name="wrkr"/>
<jsp:setProperty property="job" value="<%=job %>" name="wrkr"/>
<jsp:setProperty property="company" value="<%=company %>" name="wrkr"/>
<h3 align="center">Employee Details are :</h3>
<table cellpadding="2px" bgcolor="#f7f9fb" width="400px"align="center">
<tr>
<td align="center">Employee Name: </td>
<td align="center"><jsp:getProperty property="empName" name="wrkr"/></td>
</tr>
<tr>
<td align="center">Employee Job: </td>
<td align="center"><jsp:getProperty property="job" name="wrkr"/></td>
</tr>
<tr>
<td align="center">Employee's Company: </td>
<td align="center"><jsp:getProperty property="company" name="wrkr"/></td>
</tr>
</table>
<%
}
%>
</body>
</html>
Emp.java
package p2;
public class emp
{
String empName;
String job;
String company;
public String getEmpName()
{
return empName;
}
public void setEmpName(String empName)
{
this.empName = empName;
}
public String getJob()
{
return job;
}
public void setJob(String job)
{
this.job = job;
}
public String getCompany()
{
return company;
}
public void setCompany(String company)
{
this.company = company;
}
}
JSP Directive tags | Types of Directive tags
Directive tags are placed inside the JSP page to provide message to the JSP container.
Directive tags have this syntax:
<%@ directive {attr=”value”}* %>. White space is optional after the ‘<%@’ and before ‘%>’. This syntax is easy to type. Directives do not produce any output into the current out stream. There are three directives: the page, include directives and taglib.
Directive tags are those tags of JSP which do not produce output into current out stream and are placed only for messaging to the JSP container.
We are going to discuss directive tags and their utilization like importing packages, error page configuration and getting implicit object exception.
Types of Directive tags
Directive tags affect the overall structure of servlet that is generated from the JSP. These tags are used to set global values such as class declarations, methods to be implemented, output content type, etc. These tags provide directions by generating equivalent Java code in Java equivalent servlet. These are three types of directive tags which are as follows:
1. page directive
2. include directive
3. taglib directive
Page Directive
This tag provides global information to the JSP program. The programmer can use page directive multiple times in a JSP page. The page directive used on any part of the JSP page is automatically applied to the entire translation unit. It is a good programming style to place the page directive at the beginning of the JSP page.
The syntax as follows:
XML based syntax
<jsp:directive.page attributes/>
Include Directive
This tag is used to include the code of destination program to source JSP program. So the request given to source JSP program executes its code and the included code. The included directives add the text of the included file to the JSP page without any processing or modification. The included file can be static or dynamic.
Syntax of include directive:
<%@include file = “destination resouce/file name”%>
XML based syntax:
<jsp:directive.include file=”destination/file name”%>
The behaviour of include with respect to recompilation is container dependent, that means some servers recognize without recompilation and some do not.
Taglib Directive
The taglib directive in a JSP page declares that the page uses a tag library. It identifies the tag library using a URI and associates a tag prefix that differentiates the usage of actions in the library. If a JSP container implementation cannot locate a tag library description, then that results in fatal translation error.
The taglib directive tag is used to declare a custom tag library in a JSP page. The programmer creates his own tag to make Java code less JSP program. Xml syntax is not available for it. Syntax of taglib directive
<%@taglib uri=”some name” prefix=”unique name”%>
HTTP Session with Cookies
HttpSession object allocates memory on the server and remembers client data across the multiple requests in the form of session attribute values. HttpSession object is one per browser window (client) so each HttpSession object can be used to remember client data during a session. [Read more…] about HTTP Session with Cookies
HTTP Session with Url Rewriting
• The limitation with third technique is, if cookies are restricted from coming to browser then third technique fails to perform session tracking because it uses in memory cookie to send session id to browser from web application along with response and to bring session id to web application from browser window along with request. [Read more…] about HTTP Session with Url Rewriting
HTTP COOKIES in JSP
Cookies are the small textual information which allocate the memory at client side by remembering client data across the multiple requests during a session. The web resource programs of web application create cookies at server side but these cookies come to client side along with the response and allocate memory at client side. Cookies of client side go back to their web application along with the request given by clients (browser windows to web resource programs). [Read more…] about HTTP COOKIES in JSP
What is Session Tracking?
The form page prepared using .html file is called source form page (fixed content).The form page that comes as response generated by servlet or JSP program is called dynamic form page (dynamic content). Instead of asking all the details of end user in a single form page, it is recommended to ask those details in multiple form pages.
However, one big problem with it is that, the details entered in a particular form page cannot be accessed or retrieved in any other or even in the successive pages, because HTTP is a stateless protocol. This means every time a client sends a request a new connection is established and the previous request data lost. For example, in online shopping when we want to add a number of items to our cart, the online store’s server have to make a record of the items which will be used during purchase. This is where session tracking comes to play. Session tracking is tracking of client data throughout the session and across the form pages.
Figure shows static form page that the end user needs to read and answer some unnecessary question, when he selects or deselects the marital status checkbox. To overcome the problem, the working with dynamic form page is shown.
In Figure the content of form page2 is dynamic and generated by Srv1 program based on the marital status value of —– or request1 data.
All web applications are stateless web applications by default, which means —- cannot remember client data across the multiple requests during a session.
A session is a set of continuous and related operations (requests) — on the web application by a client (browser window / end user).
By stateless behavior of web application we mean that while processing current request in any web resource program, we cannot use previous request data, which means while processing request2 we cannot use request2 data as shown in Figure.
Web applications are stateless because the protocol http is given as stateless protocol. According to this, one new connection will be created between browser window and web server for every request and this connection will be closed automatically, once related response goes to the browser window.
Example
If browser window gives 10 requests to a web application, 10 connections will be created between browser window and web server. Due to this one connection related to a client’s request data cannot be used in an other connection related to the client’s request processing (that means we cannot use previous request data in web resource program while processing current request).
By placing <form> tag, <input> tag in pw.println( ) statements of servlet program we can generated dynamic form page from that servlet program. In naukri.com registration process multiple forms will be there. The first form page is static form page and other form pages are dynamic form pages. Based on the data given in form page1 the questions in form page2 will be rendered. Similarly, based on data in form page2 the questions in form page3 will be rendered.
Web applications are stateless because they are using a stateless protocol called.
Hidden Form Fields in JSP
An invisible text box of the form page is called a hidden box. When a form page is submitted, the hidden box value goes to web resource program along with the request parameter value. [Read more…] about Hidden Form Fields in JSP