16.1 Write A JSP Page To Display The Current Date and Time. Date - JSP
16.1 Write A JSP Page To Display The Current Date and Time. Date - JSP
16.1 Write A JSP Page To Display The Current Date and Time. Date - JSP
Date.jsp
<Html>
<Head>
<Title>JSP Expressions</Title>
</Head>
<Body>
<H2>JSP Expressions</H2>
<ul>
<li>Current time: <%= new java.util.Date() %>
<li>Server: <%= application.getServerInfo() %>
<li>Session Id: <%= session.getId() %>
<li>The <code>test param</code> form parameter: <%=
request.getParameter("testParam")%>
</ul>
</Body>
</Html>
Register.jsp
<Html>
<Head>
<Title>Register</Title>
</Head>
<form method=get action="http://localhost:8080/StudentInfo.jsp">
<table border=1>
<tr><td>Name:</td><td> <input type=text name=txtName></td>
<tr><td>Age: </td><td><input type=text name=txtAge></td>
<tr><td>Tel Nos: </td><td><input type=text name=txtTelNo></td>
<tr><td><input type=submit></td><td> <input type=reset></td>
</table>
</form>
</html>
StudentInfo.jsp
<html>
<head>
<Title>Student Info</Title>
</Head>
<Body>
<table border=1>
<tr><td>Name</td><td> <%= request.getParameter("txtName")
%></td></tr>
<tr><td>Age</td><td><%= request.getParameter("txtAge")
%></td></tr>
<tr><td>Tel No</td><td><%= request.getParameter("txtTelNo")
%></td></tr>
</table>
</body>
</html>
17.1 Write a JSP page, which displays three text boxes for
Department Number, Department Name and Location. On click of the
submit button call another JSP page which will enter the values
in the database with the help of PreparedStatement class. Also
use jspInit() and jspDestroy() to open and close the connection.
(Register.jsp).
DeptForm.jsp
<html>
<Head>
<title> Department Form </title>
</head>
<body>
<form method=GET
action="http://localhost:8080/Register1.jsp">
<table>
<tr>
<td>
DepartmentNo:
</td>
<td>
<input type=text name=txtNo>
</td>
</tr>
<tr>
<td>
DepartmentName:
</td>
<td>
<input type=text name=txtName>
</td>
</tr>
<tr>
<td>
Location:
</td>
<td>
<input type=text name=txtLoc>
</td>
</tr>
</table>
Register1.jsp
<%@ page import="java.sql.*" %>
<%! String a,b,c,d,e,f,Query; %>
<%! Connection con;
Statement st;
PreparedStatement ps; %>
<%! int i,num; %>
<%!
public void jspInit()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:ty289");
st=con.createStatement();
Query="insert into Dept values(?,?,?)";
ps=con.prepareStatement(Query);
}
catch(Exception e){System.out.println("Error: "+e.getMessage());}
}
%>
<%
a=(String)request.getParameter("txtNo");
b=(String)request.getParameter("txtName");
c=(String)request.getParameter("txtLoc");
ps.setInt(1,Integer.parseInt(a));
ps.setString(2,b);
ps.setString(3,c);
ps.executeUpdate();
con.commit();
ResultSet rs=st.executeQuery("select * from Dept");
%>
<html><body>
<table border=1>
<tr><th>Dept No </th><th>Dept Name</th><th>Location</th></tr>
<%
while(rs.next())
{
%>
<tr>
<% for(int j=0;j<=2;j++)
{
Object obj=rs.getObject(j+1);
%>
<td><%=obj.toString()%></td>
<%
}
}
%>
</tr>
</table>
<%!
public void jspDestroy()
{ try
{
ps.close();
st.close();
}
catch(Exception e){System.out.println("Error:
"+e.getMessage());}
}%>
</body>
</html>
Include.jsp
<html>
<body bgcolor="white">
<br>
<H1 align="center">JavaServer Pages 1.0</H1>
<H2 align="center">Include Example</H2>
<P> </P>
<P> </P>
<font color="red">
<p>In place evaluation of another JSP which gives you the current time:
:-)
</html>
foo.jsp
<body bgcolor="white">
<font color="red">
<%= System.currentTimeMillis() %>
18.1 Create a java bean that gives information about the current
time. The bean has getter properties for time, hour, minute, and
second. Write a JSP page that uses the bean and display all the
information.
package myclass;
import java.util.Calendar;
import java.util.Date;
RegisterBean.java
package myclass;
public class RegisterBean
{
private String username,password,email,fname;
private String lname,experience,skills;
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email=email;
}
Page1.jsp
Page2.jsp
<%@ page import="java.util.*" %>
<html>
<body>
<form action="Controller.jsp">
<pre>
FirstName: <input type=text name="fname"> <br>
LastName: <input type=text name="lname"> <br>
</pre>
<input type=hidden name="nextpage" value="Page3.jsp"> <br>
<input type=submit value="continue">
</form>
</body>
</html>
Page3.jsp
<%@ page import="java.util.*" %>
<html>
<body>
<form action="Controller.jsp">
<pre>
Work Experience: <input type=text name="experience"> <br>
Skills: <input type=text name="skills"> <br>
</pre>
<input type=hidden name="nextpage" value="Page4.jsp"> <br>
<input type=submit value="continue">
</form>
</body>
</html>
Page4.jsp
<html>
<body>
<!--
<jsp:useBean class="myclass.RegisterBean" id="rbean" scope="session" />
-->
<h1>You Have Submitted the following report</h1>
<pre>
UserName: <jsp:getProperty name="rbean" property="username" /><br>
Password: <jsp:getProperty name="rbean" property="password" /> <br>
Email: <jsp:getProperty name="rbean" property="email" /> <br>
FirstName: <jsp:getProperty name="rbean" property="fname" /> <br>
LastName: <jsp:getProperty name="rbean" property="lname" /> <br>
Work Exp.: <jsp:getProperty name="rbean" property="experience" /><br>
Skills: <jsp:getProperty name="rbean" property="skills" /> <br>
</pre>
</body>
</html>
Controller.jsp
<jsp:useBean class="myclass.RegisterBean" id="rbean" scope="session" />
<jsp:setProperty name="rbean" property="*" />
<%
String nextpage=request.getParameter("nextpage");
response.sendRedirect(nextpage);
%>