Sie sind auf Seite 1von 8

16.1 Write a JSP page to display the current date and time.

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>

16.2 Write a JSP file, which displays the parameters passed to


the file.

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>

<input type=submit name=Submit>


<input type=reset name=Reset>
</Form>
</body>
</Html>

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>

17.2 Write a JSP page that will include in it a simple static


file with the help of <%@ include file=”?” %> and a simple JSP
page with the help of <jsp:include page=”?”/> tag.

Include.jsp
<html>
<body bgcolor="white">
<br>
<H1 align="center">JavaServer Pages 1.0</H1>
<H2 align="center">Include Example</H2>
<P>&nbsp;</P>
<P>&nbsp;</P>

<font color="red">

<%@ page buffer="5" autoFlush="false" %>

<p>In place evaluation of another JSP which gives you the current time:

<%@ include file="foo.jsp" %>

<p> <jsp:include page="/examples/jsp/samples/include/foo.html"


flush="true"/> by including the output of another JSP:

<jsp:include page="foo.jsp" flush="true"/>

:-)

</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.

1)First create a folder named myclass in


C:\JavaWebServer2.0\classes folder
2)Save CalendarBean.java in myclass folder
3)Compile CalendarBean.java so the class file is created

package myclass;
import java.util.Calendar;
import java.util.Date;

public class CalendarBean


{
private Calendar calendar;
public CalendarBean()
{
calendar=Calendar.getInstance();
}
public Date getTime()
{
return calendar.getTime();
}
public int getHour()
{
return calendar.get(Calendar.HOUR_OF_DAY);
}
public int getMinute()
{
return calendar.get(Calendar.MINUTE);
}
public int getSecond()
{
return calendar.get(Calendar.SECOND);
}
}

Save BeanTime.jsp in public_html


BeanTime.jsp
<html>
<body>
<jsp:useBean class="myclass.CalendarBean" id="cal" />
<pre>
Time:<jsp:getProperty name="cal" property="Time" /><br>
Hour:<jsp:getProperty name="cal" property="Hour" /><br>
Minute:<jsp:getProperty name="cal" property="Minute" /><br>
Seconds:<jsp:getProperty name="cal" property="Second" /><br>
</pre>
</body>
</html>

18.2 Create a multi-page registration form in which the user


input is spread across 3 pages. The data is stored in the session
with the help of Java Beans. After all the information is
entered, read the contents of the java bean and display the
contents on a new page.

1) Save RegisterBean.java in myclass folder


2) Compile RegisterBean.java so the class file is created

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;
}

public String getUsername()


{
return username;
}
public void setUsername(String username)
{
this.username=username;
}

public String getPassword()


{
return password;
}
public void setPassword(String password)
{
this.password=password;
}

public String getFname()


{
return fname;
}
public void setFname(String fname)
{
this.fname=fname;
}

public String getLname()


{
return lname;
}
public void setLname(String lname)
{
this.lname=lname;
}

public String getExperience()


{
return experience;
}
public void setExperience(String experience)
{
this.experience=experience;
}

public String getSkills()


{
return skills;
}
public void setSkills(String skills)
{
this.skills=skills;
}
}

Save all .jsp files in public_html

Page1.jsp

<%@ page import="java.util.*" %>


<html>
<body>
<form action="Controller.jsp">
<pre>
UserName: <input type=text name="username"> <br>
Password: <input type=password name="password"> <br>
E-mail: <input type=text name="email"> <br>
</pre>
<input type=hidden name="nextpage" value="Page2.jsp"><br>
<input type=submit value="continue">
</form>
</body>
</html>

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);
%>

Das könnte Ihnen auch gefallen