postheadericon 26.Develop an application to write a "page-composite" JSP that includes other pages or passes control to another page. (Hint: Use <jsp:include> or <jsp:forward>).


index.jsp


<html>
<head>
<title>EX-26</title>
<style>
div
{
background:#ababff;
position:absolute;
width:1000px;
left:110px;

}

.abcH
{
top:0.5cm;
width:1000px;
border-style:solid;
}

.abcL
{
top:200px;

width:1000px;
}

.abcF
{
top:500px;
width:1000px;
border-style:solid;
}
</style>
</head>

<body bgcolor="#ababff">
<%@ include file="/header.jsp" %>
<jsp:include page="/login.jsp" />
<jsp:include page="/footer.jsp" />
<br>
</div>
</body>
</html>


header.jsp


<div class="abcH"  id="f1">
<br>
<table align="center">
<tr>
<td><h1>www.BipinRupadiya.blogspot.in<h1></td>
</tr>
</table>

</div>



login.jsp



<div class="abcL"  id="f1">
<br>
<form method="POST" action="result.jsp">
<table align="center">
<tr>
<td colspan=2 align=center> <h3>Login</h3><hr></td>
</tr>
<tr>
<td>User Name : </td>
<td><input type="text" name="txtUsr"></td>
</tr>
<tr>
<td>Password :</td>
<td><input type="password" name="txtPwd"></td>
</tr>
<tr>
<td colspan=2 align=center><hr>
<input type="submit" value="Login">
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
</div>


footer.jsp


<div class="abcF"  id="f2">
<br>
<table align="center">
<tr>
<td><h3>&copy; Mr. Bipin S Rupadiya<h3></td>
</tr>
</table>
</div>


result.jsp


<%
String d;
if(request.getParameter("txtUsr").equals("bipin") &&
request.getParameter("txtPwd").equals("rupadiya"))
d="welcome.jsp";
else
d="index.jsp";
%>
<jsp:forward page="<%= d%>" />


welcome.jsp


<html>
<head>
<title>EX-26</title>
<style>
div
{
background:#ababff;
position:absolute;
width:1000px;
left:110px;

}

.abcH
{
top:0.5cm;
width:1000px;
border-style:solid;
}

.abcL
{
top:200px;

width:1000px;
}

.abcF
{
top:500px;
width:1000px;
border-style:solid;
}
</style>
</head>

<body bgcolor="#ababff">
<%@ include file="/header.jsp" %>
<div class="abcL">
<center><h1>Welcome</h1><br>
<br>
<a href="index.jsp">Home</a>
</div>
<jsp:include page="/footer.jsp" />
<br>
</div>
</body>
</html>



postheadericon 27. You want to reduce the amount of Java coding in your JSP using a JavaBean component. (Hint: Use with the name of your bean).



Simple Interest Calculation Using Java Beans (Form Beans)

index.jsp


<html>
<head>
<title>EX-23</title>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:400px;
left:5in;
border-style:solid;
}
</style>

</head>
<body>

<div class="abc"  id="f1">
<br>
<form method="POST" action="result.jsp">
<table align="center">
<tr>
<td colspan=2 align=center><h3>Simple Interest</h3><hr>
</td>
</tr>
<tr>
<td>Amount : </td>
<td><input type="text" name="p"></td>
</tr>
<tr>
<td>Rate :</td>
<td><input type="text" name="r"></td>
</tr>
<tr>
<td>Time :</td>
<td><input type="text" name="n"></td>
</tr>

<tr>
<td colspan=2 align=center>
<hr>
<input type="submit" value="Calculate">
<input type="reset" value="Clear">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>



result.jsp


<html>
<head>
<title>EX-23</title>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:400px;
left:5in;
border-style:solid;
}
</style>

</head>
<BODY>
<CENTER>


<jsp:useBean id="si" class="bsr.SimpleIntrest" />

<jsp:setProperty name="si" property="*" />

<div class="abc"  id="f1">
<br>
<table width="50%" align="center">
<tr>
<th colspan=2><h3> Simple Interest</h3><hr></th>
</tr>
<tr>
<td>Amount : </td>
<td><jsp:getProperty name="si" property="p" /></td>
</tr>
<tr>
<td>Rate :</td>
<td><jsp:getProperty name="si" property="r" /></td>
</tr>
<tr>
<td>Time :</td>
<td><jsp:getProperty name="si" property="n" /></td>
</tr>
<tr>
<td>Interest :</td>
<td><jsp:getProperty name="si" property="intrest" /></td>
</tr>
<tr>
<th colspan=2> <hr><a href="index.jsp">Home</a></th>
</tr>
</table>
</form>
</div>
</body>
</html>



SimpleIntrest.java 


package bsr;
public class SimpleIntrest
{
private float p=1;
private float r=1;
private float n=1;

// Principal Amount
public float getP()
{
return(p);
}
public void setP(float v)
{
this.p = v;
}

// Rate of Interest
public float getR()
{
return(r);
}
public void setR(float v)
{
this.r = v;
}

// Number of Years
public float getN()
{
return(n);
}
public void setN(float v)
{
this.n = v;
}

//Calculate Interest
public float getIntrest()
{
return((p*r*n)/100);
}
}




postheadericon 23. Develop a interest calculation application in which user will provide all information in HTML form and that will be processed by servlet and response will be generated back to the user.



1. Using  Servlet

index.jsp


<html>
<head>
<title>EX-23</title>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:400px;
left:5in;
border-style:solid;
}
</style>

</head>
<body>

<div class="abc"  id="f1">
<br>
<form method="get" action="/Ex23/bipinrupadiya.blogspot.com">
<table align="center">
<tr>
<td colspan=2 align=center><h3>Simple Interest</h3><hr>
</td>
</tr>
<tr>
<td>Amount : </td>
<td><input type="text" name="p"></td>
</tr>
<tr>
<td>Rate :</td>
<td><input type="text" name="r"></td>
</tr>
<tr>
<td>Time :</td>
<td><input type="text" name="n"></td>
</tr>

<tr>
<td colspan=2 align=center>
<hr>
<input type="submit" value="Calculate">
<input type="reset" value="Clear">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>


Ex23.java


package bsr;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;

/** Ex23 simple Interest. by Bipin S rupadiya */

@WebServlet("/bipinrupadiya.blogspot.com")
public class Ex23 extends HttpServlet
{
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
    {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
     
        float p=Float.parseFloat(request.getParameter("p"));
        float r=Float.parseFloat(request.getParameter("r"));
        float n=Float.parseFloat(request.getParameter("n"));
        float i=(p*r*n)/100;
        out.println("<br><h1>Simple Interest will be : "+i);
    }
}



2. Using Java Beans (Form Beans)

index.jsp


<html>
<head>
<title>EX-23</title>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:400px;
left:5in;
border-style:solid;
}
</style>

</head>
<body>

<div class="abc"  id="f1">
<br>
<form method="POST" action="result.jsp">
<table align="center">
<tr>
<td colspan=2 align=center><h3>Simple Interest</h3><hr>
</td>
</tr>
<tr>
<td>Amount : </td>
<td><input type="text" name="p"></td>
</tr>
<tr>
<td>Rate :</td>
<td><input type="text" name="r"></td>
</tr>
<tr>
<td>Time :</td>
<td><input type="text" name="n"></td>
</tr>

<tr>
<td colspan=2 align=center>
<hr>
<input type="submit" value="Calculate">
<input type="reset" value="Clear">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>



result.jsp


<html>
<head>
<title>EX-23</title>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:400px;
left:5in;
border-style:solid;
}
</style>

</head>
<BODY>
<CENTER>


<jsp:useBean id="si" class="bsr.SimpleIntrest" />

<jsp:setProperty name="si" property="*" />

<div class="abc"  id="f1">
<br>
<table width="50%" align="center">
<tr>
<th colspan=2><h3> Simple Interest</h3><hr></th>
</tr>
<tr>
<td>Amount : </td>
<td><jsp:getProperty name="si" property="p" /></td>
</tr>
<tr>
<td>Rate :</td>
<td><jsp:getProperty name="si" property="r" /></td>
</tr>
<tr>
<td>Time :</td>
<td><jsp:getProperty name="si" property="n" /></td>
</tr>
<tr>
<td>Interest :</td>
<td><jsp:getProperty name="si" property="intrest" /></td>
</tr>
<tr>
<th colspan=2> <hr><a href="index.jsp">Home</a></th>
</tr>
</table>
</form>
</div>
</body>
</html>



SimpleIntrest.java [ JavaBean ]


package bsr;
public class SimpleIntrest
{
private float p=1;
private float r=1;
private float n=1;

// Principal Amount
public float getP()
{
return(p);
}
public void setP(float v)
{
this.p = v;
}

// Rate of Interest
public float getR()
{
return(r);
}
public void setR(float v)
{
this.r = v;
}

// Number of Years
public float getN()
{
return(n);
}
public void setN(float v)
{
this.n = v;
}

//Calculate Interest
public float getIntrest()
{
return((p*r*n)/100);
}
}



postheadericon 20. Write a JSP page, which uses the include directive to show its header and footer.



index.jsp


<html>
<head>
<title>EX-20</title>
<style>
div
{
background:#ababff;
position:absolute;
width:1000px;
left:110px;
border-style:solid;
}

.abcH
{
top:0.5cm;
width:1000px;
}

.abcF
{
top:500px;
width:1000px;
}
</style>
</head>

<body>
<%@ include file="/header.jsp" %>
<jsp:include page="/footer.jsp" />
<br>
</div>
</body>
</html>

header.jsp


<div class="abcH"  id="f1">
<br>
<table align="center">
<tr>
<td><h1>www.BipinRupadiya.blogspot.in<h1></td>
</tr>
</table>

</div>



footer.jsp


<div class="abcF"  id="f2">
<br>
<table align="center">
<tr>
<td><h3>&copy; Mr. Bipin S Rupadiya<h3></td>
</tr>
</table>
</div>



postheadericon 19. Write a simple JSP page to display a simple message (It may be a simple html page).



<html>
<head>
<title>EX-19</title>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:300px;
left:5in;
border-style:solid;
}
</style>
</head>

<body>

<div class="abc"  id="f1">
<center>
<br><br>
<b>Visit</b>
<br><br>
<%
out.println("<a href='www.bipinrupadiya.blogspot.in'>www.bipinrupadiya.blogspot.in</a>");
%>
<br><br><br>
</div>
</body>
</html>

postheadericon 18. Develop a Servlet to authenticate a user, where the loginid and password are available as request parameters. In case the authentication is successful, it should setup a new session and store the user's information in the session before forwarding to home.jsp, which displays the user's information like full name, address, etc.


index.jsp


<html>
<head>
<title>EX-18</title>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:300px;
left:5in;
border-style:solid;
}
</style>
</head>

<body>

<div class="abc"  id="f1">
<br>
<form method="POST" action="/Ex18/bipinrupadiya.blogspot.com">
<table align="center">
<tr>
<td>User Name : </td>
<td><input type="text" name="txtUsr"></td>
</tr>
<tr>
<td>Password :</td>
<td><input type="password" name="txtPwd"></td>
</tr>
<tr>
<td colspan=2 align=center>
<input type="submit" value="Login">
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>

home.jsp


<html>
<head>
<title>EX-18</title>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:300px;
left:5in;
border-style:solid;
}
</style>
</head>

<body>
<%
        HttpSession sessi=request.getSession();
%>
<div class="abc"  id="f1">

<center>
<h1><font color="#FFFFFF">Welcome <%= session.getAttribute("sFName") %> ...!</font></h1>
<table border="1" cellPadding="5px">
<tr>
<td>Name :</td>
<td>
<%= session.getAttribute("sFName") %>&nbsp;
<%= session.getAttribute("sMName") %>&nbsp;
<%= session.getAttribute("sLName") %>
</td>
</tr>
<tr>
<td>Date of Birth :</td>
<td><%= session.getAttribute("sDOB") %></td>
</tr>
<tr>
<td>Adsress :</td>
<td><%= session.getAttribute("sAddress") %></td>
</tr>

</table>
<%session.invalidate();%>
<br>
<a href="index.jsp"> Logout </a>

</div>
</body>
</html>

Ex18.java



import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Ex18 extends HttpServlet
{
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException
    {

response.setContentType("text/html");
        PrintWriter out=response.getWriter();

        String usr=request.getParameter("txtUsr");
        String pwd=request.getParameter("txtPwd");
       
        if( usr.equals("bipin") && pwd.equals("rupadiya") )
        {
   String myDiv;
out.println("<html><body><form>");
myDiv="<div style='background:#ababff; position:absolute; ";
myDiv=myDiv+"top:2in; hieght:200px; width:300px;left:5in;border-style:solid'>";
out.println(myDiv);
out.println("<form><table cellpadding'2px'>");
            out.println("<tr><td>First Name :</td><td><input type='text' name='txtFName' ></td></tr>");
out.println("<tr><td>MiddleName : </td><td><input type='text' name='txtMName' ></td></tr>");
out.println("<tr><td>Last Name : </td><td><input type='text' name='txtLName' ></td></tr>");
            out.println("<tr><td>Date of Birth : </td><td><input type='text' name='txtDOB' ></td></tr>");
out.println("<tr><td>Address : </td><td><input type='text' name='txtAddress'></td></tr>");
            out.println("<tr><td colspan='2'><input type='submit' value='OK'>");
out.println("<input type='reset' value='Clear'></td></tr>");
out.println("</td></form></div></body></html>");
}
        else
        {
            response.sendRedirect("index.jsp");
        }
    }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException
    {
HttpSession session=request.getSession();
session.setAttribute("sFName",request.getParameter("txtFName"));
session.setAttribute("sMName",request.getParameter("txtMName"));
session.setAttribute("sLName",request.getParameter("txtLName"));
session.setAttribute("sDOB",request.getParameter("txtDOB"));
session.setAttribute("sAddress",request.getParameter("txtAddress"));
response.sendRedirect("home.jsp");
    }  
}


postheadericon 17. Develop a Servlet which looks for cookies for username and password, and forwards to a home.jsp in case the cookies are valid and forwards to login.jsp, in case the cookies are not found or the cookies are not valid





index.jsp


<html>
<head>
<title>EX-17</title>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:300px;
left:5in;
border-style:solid;
}
</style>
</head>

<body>

<div class="abc"  id="f1">
<br>
<form method="POST" action="/Ex17/bipinrupadiya.blogspot.com">
<table align="center">
<tr>
<td>User Name : </td>
<td><input type="text" name="txtUsr"></td>
</tr>
<tr>
<td>Password :</td>
<td><input type="password" name="txtPwd"></td>
</tr>
<tr>
<td colspan=2 align=center>
<input type="checkbox" name="chkRM">
Remember Me
</td>
</tr>

<tr>
<td colspan=2 align=center>
<input type="submit" value="Login">
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>

home.jsp


<html>
<head>
<title>EX-17</title>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:300px;
left:5in;
border-style:solid;
}
</style>
</head>
<body>
<div class="abc"  id="f1">
<br>
<center>
<h1>WELCOME
<br>
</div>
</body>
</html>



Ex17.java


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Ex17 extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String usr = request.getParameter("txtUsr");
String pwd = request.getParameter("txtPwd");
String RM  = request.getParameter("chkRM");

if (RM != null)
{
Cookie cookie = new Cookie(usr,pwd);
cookie.setMaxAge(60*60*24*365);// 1 Year
response.addCookie(cookie);
}
Cookie[] cookies = request.getCookies();
if (cookies != null)
{
//Cookie("bipin","rupadiya");
//Cookie(usr,pwd);
String cPwd="x";
for(int i=0; i<cookies.length; i++) //user check
{
Cookie cookie = cookies[i];
if (usr.equals(cookie.getName()))
{
cPwd=cookie.getValue();
break;
}
}
if(cPwd.equals(pwd))//password check
{
response.sendRedirect("home.jsp");
}
else
{
//out.println("<script>alert('User Name or Password is incorrect. Please Try Again');location.href='login.jsp';</script>");
response.sendRedirect("index.jsp");
}
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
{
doGet(request,response);
}
}


Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.