Unit-11: JSP Programming

5th Semester

 
1.Write a JSP program to display the current server time.
Jsp program:
<HTML>
<BODY>
<%
java.util.Calendar now = java.util.Calendar.getInstance();
int hour = now.get( java.util.Calendar.HOUR_OF_DAY);
int minute = now.get( java.util.Calendar.MINUTE);
int second=now.get( java.util.Calendar.SECOND);
%>
<h1>Current Server Time</h1>
<%
out.println(hour +”:”+minute+”:”+second);
%>
</BODY>
</HTML>
 
2.JSP Implicit Object
Crete a HTML form with two textfied for entering first name and last name, and a submit button.Create a JSP page that reads the data from html form and displays it.
Html file: index.html
<HTML>
<BODY>
<FORM ACTION=”display.jsp” METHOD=”post”>
First Name: <INPUT TYPE=TEXT Name=fn>
<BR>
Last Name: <INPUT TYPE=TEXT Name=ln>
<BR>
<INPUT TYPE=SUBMIT VALUE=”OK”>
</FORM>
</BODY>
</HTML>
JSP program
<HTML>
<BODY>
<%
String firstName = request.getParameter(“fn”);
String lastName = request.getParameter(“ln”);
out.println(“First name: ” + firstName);
%>
<br>
<%
out.println(“Last name: ” + lastName);
%>
</BODY>
</HTML>
3.Create a JSP page that implements session object to implement a counter.
<HTML>
<BODY>
<%
String counterAttribute = (String) session.getAttribute(“counter”);
int count = 0;
try
{
count = Integer.parseInt(counterAttribute);
}
catch (Exception e) { }
count++;
session.setAttribute(“counter”, Integer.toString(count));
out.println(“This is the ” + count + “th time you visited this page in this session.”);
%>
</BODY>
</HTML>
4. Include Directive
Example:
A Simple JSP Page that Includes Two Files
<%@ page session=”false” %>
<%@ page import=”java.util.Calendar” %>
<%@ include file=”includes/Header.html” %>
<%
out.println(“Current time: ” + Calendar.getInstance().getTime());
%>
<%@ include file=”includes/Footer.html” %>
The Header.html File
<HTML> <HEAD> <TITLE>Welcome</TITLE> <BODY>
The Footer.html File
</BODY> </HTML>
5.Create a suitable user interface that allow users to input the data for a student. When the user press submit button pass these information to the JSP for storing these data in the table tbl_student.[Assume DSN: NAG]
Html file
<html>
<body>
<form name=”myform” method=”post” action=”db.jsp”>
ID:
<input type=”text” name=”id”>
<br>Name:
<input type=”text” name=”name”>
<br>Address:
<input type=”text” name=”add”>
<br>Phone Number:
<input type=”text” name=”phone”>
<br>
<input type=”submit” value=”Save”>
</form>
</body>
</html>
db.jsp file
<%@ page import=”java.sql.*” %>
<%
String id=request.getParameter(“id”);
String name=request.getParameter(“name”);
String add=request.getParameter(“add”);
String ph=request.getParameter(“phone”);
try
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
String url = “jdbc:odbc:Nag”;
Connection conn = DriverManager.getConnection(url,””,””);
Statement st = conn.createStatement();
st.executeUpdate(“insert into tbl_student values (‘”+id+”‘,'”+name+”‘,'”+add+”‘,'”+ph+”‘)”);
out.println(“Record insert successfull”);
}
catch (Exception e)
{
out.println(“Error Occured”+e);
}
%>
6.Create a JSP page that connects to the database db_student and displays all the records stored in the table tbl_student.
JSP file
<%@ page import=”java.sql.*” %>
<html>
<body bgcolor=orange>
<TABLE border=1>
<TR>
<TH>ID</TH>
<TH>Name</TH>
<TH>Address</TH>
<TH>PhoneNumber</TH>
</TR>
<%
try
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
String url = “jdbc:odbc:Nag”;
Connection conn = DriverManager.getConnection(url,””,””);
Statement st = conn.createStatement();
Statement stmt = conn.createStatement();
ResultSet rs;
rs=stmt.executeQuery(“Select * from tbl_student”);
while (rs.next())
{
out.println(“<TR>”);
out.println(“<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(“</TR>”);
}
rs.close();
stmt.close();
conn.close();
}
catch (Exception e)
{
out.println(“Error Occured”+e);
}
%>
</TABLE>
</body>
</html>
 
8.Write a bean class having the property id and name, whose code is given below Save it as StudentBean.java.StudentBean.java file
package mypack.student;
public class StudentBean
{
private int id;
public String name;
public void setId(int id)
{
this.id=id;
}
public int getId()
{
return id;
}
public void setName(String name)
{
this.name=name;
}
public String getName()
{
return name;
}
}
2. Compile the bean to obtain a class file called StudentBean.class.
3. Create the following directory structure
C:\apache-tomcat-8.0.14\webapps\myJSPApp\WEB-INF\classes\mypack\student
Note: The deployment must take into account the package name. In this case, you need to create a directory called
mypack under the classes directory. Under com, create a directory named student. Copy your CalculatorBean.class
file to this student directory.
4. Save the file with .class extension i.e. StudentBean.class in student sub directory.
5. Now create a UI with two textfield and submit button using JSP/html page to prompt user to input the information
Then save it with .jsp or .html extension i.e. ui.html in myJSPApp directory.
Ui.html file
<html>
<body>
<form name=”myfrom” method=”post” action=”resp.jsp”>
Student Id:
<input type=”text” name=”id”>
<br>
Student Name:
<input type=”text” name=”nm”>
<br>
<input type=”submit” name=”s”>
</form>
</body>
</html>
6. Now create JSP page resp.jsp that is invoked when user clicks the submit button. This page first makes use of the
previous bean class created and then set the bean property and finally display the property value as response.
Resp.jsp file
<jsp:useBean id=”theBean” class=”mypack.student.StudentBean”/>
<jsp:setProperty name=”theBean” property=”id” param=”id”/>
<jsp:setProperty name=”theBean” property=”name” param=”nm”/>
Student Id=
<jsp:getProperty name=”theBean” property=”id”/>
Student Name=
<jsp:getProperty name=”theBean” property=”name”/>

Leave a Reply

Your email address will not be published. Required fields are marked *