postheadericon 16. Assume that the information regarding the marks for all the subjects of a student in the last exam are available in a database, Develop a Servlet which takes the enrollment number of a student as a request parameter and displays the marksheet for the student.



1. Create Database in MySql / Oracle / Access


You can choose any database. Here, I provide the solution to connect database like MySQL, Oracle,    and Microsoft Access




2. Create Table IN MySQL
create table MARK (EnNo VARCHAR(10),Code VARCHAR(10),Name VARCHAR(10),Mark INT(3));


3. Create Table IN Oracle
create table MARK (EnNo VARCHAR(10),Code VARCHAR(10),Name VARCHAR(10),Mark NUMBER (3));


    


4. Create Web Application in tomcat using following code              


index.jsp





<HTML>  
<HEAD>  
<TITLE>Ex-16 : www.BipinRupadiya.blogspot.in</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>
<center>
<h3>
<FORM METHOD=POST ACTION="Ex16.com">
Enrollment No.: <input type=TEXT name = "txtEnNo" >
<BR><BR><input type="submit" value="Show Result">
</FORM>
</div>
</BODY>
</HTML>


Ex16.java





import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import javax.servlet.annotation.WebServlet;

@WebServlet("/Ex16.com")
public class Ex16 extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
String enNo = request.getParameter("txtEnNo");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        try
{
//Connect To MySql
/* Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wtad","root","admin");

*/
//Connect To Oracle

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","scott","tiger");


//Connect To Access
/*
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:test","bipin","rupadiya");
*/


String sql = "select * from MARK where EnNo = ?";
PreparedStatement ps=con.prepareStatement(sql);
ps.setString(1, enNo);

ResultSet rs= ps.executeQuery();

out.println("<HTML><BODY bgcolor='#ababff'>");
            out.println("<center><h1> Marksheet</h1>");
            out.println("<TABLE BORDER=1 cellpadding='5px' width=50%");

out.println("<TR><TH colspan=3> Enrollment No.:"+enNo+"</TH></TR>");
out.println("<TR><TH>CODE </TH> <TH>NAME </TH> <TH> GRADE </TH></TR>");

            if(rs == null)
            {  
                out.println("<h1> Result Not Found... </h1>");
            }
            else
            {
while(rs.next())
                {
                    out.println("<TR>");
                        out.println("<TD>"+rs.getString(2)+"</TD>");
out.println("<TD>"+rs.getString(3)+"</TD>");
out.println("<TD>"+rs.getInt(4)+"</TD>");                          
                    out.println("</TR>");
                }
out.println("<TR><TH colspan=3> <a href='index.jsp'>Home</a> </TH></TR>");
out.println("</TABLE></CENTER></BODY></HTML>");
                rs.close();
                ps.close();
                con.close();
            }
        }
        catch(SQLException e)
        {
                out.println(e);
        }
catch(ClassNotFoundException e)
{
out.println(e);
}
        catch(Exception e)
        {
            out.println(e);
        }    

    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doGet(request, response);
    }
}






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>



Total Pageviews

© BipinRupadiya.com. Powered by Blogger.