Demo Programs: (To Be Explained and Shown in The Class) JSP PRG 1.example For Displaying Date and Time
Demo Programs: (To Be Explained and Shown in The Class) JSP PRG 1.example For Displaying Date and Time
<html>
<head>
<title>Hello</title>
</head>
<body bgcolor = "cyan">
<h1>
<font face =Arial>
<%
String s1=request.getParameter("name");
if(s1.equals("sunitech"))
out.println("Hello World");
else
out.println("Hello, "+s1);
%>
</font>
</h1>
</body>
</html>
<html>
<pre>
<h3>Sum</h3>
<form action='http://localhost:8090/sun/f22.jsp'>
<% if ((request.getParameter("t1")!= null) && (request.getParameter("t2")!=null))
{%>
<%@
page import="java.util.*"
session="false"
contentType="text/html"
errorPage="ERRORPAGE.jsp"
%>
<html>
<body>
<%!
int fact(int n)
{
int s=1;
for(int i=1;i<=n;i++)
s=s*i;
return s;
}
%>
<!-- Factorials --!>
10! = <%= fact(5) %><br>
20! = <%= fact(6) %><br>
<%--
30! = <%= fact(7) %><br>
40! = <%= fact(8) %><br>
50! = <%= fact(9) %><br> --%>
</body></html>
<hr>
<h2> <i>
I am from SuniTech save it in xyz.html
</i></h2>
<hr>
Screen</a></h1>");
}
else
{
if(n!=null && c!= null )
{
session.putValue("name", n);
session.putValue("bgcolor", c);
}
else
{
n = (String)session.getValue("name");
c = (String)session.getValue("bgcolor");
cnt = ((Integer)session.getValue("count")).intValue();
}
session.putValue("count", new Integer(++cnt));
%>
<body bgcolor= <%= c %> >
<h1> Hello <%= n %>
U requested this page <%= cnt %> times. </h1>
<h3> See, I've Remembered So Much ! </h3>
<h2> <a href=SessionDemoJSP.jsp > Next Page </a>
<p><a href=SessionDemoJSP.jsp?out=yes > Log Off </a></h2></body>
<%
}
}
%>
<% }
%>
</body>
</html>
<%@ page import="java.util.*"%> viewlist.jsp
<html>
<body>
<h3>You Have Purchased the Following Items:</h3>
<table border=1 bordercolor=red>
<%
Enumeration enum=session.getAttributeNames();
while(enum.hasMoreElements()){
String name=(String)enum.nextElement();
String val =(String) session.getAttribute(name);
out.println("<tr><td>" + name + "<td>" + val);
}
%>
</table>
<a href="list.jsp">Continue Shopping</a> <a href="logout.html">Finalize
Order</a><br>
</body>
</html>
<html> logout.html
<body>
<pre>
<form action="logout.jsp">
Name :<input type=text name="name">
CreditCard Number:<input type=text name="cardno">
Shipment Address :<textarea rows=5 cols=20></textarea>
<input type=submit>
</pre>
</body>
</html>
JSP PRG 10. EXAMPLE FOR GETTING EMPLOYEE LIST FROM DATABASE
<html>
<%@ page import="java.sql.*" %>
<pre><center>
<h3>Employee Details</h3>
<table border=1 bgcolor=yellow>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:sun");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from emp");
%>
<%
out.println("<tr><th>EmpNo</th><th>Name</th><th>Age</th><th>Dept</th></tr>");
while(rs.next())
{
out.println("<tr><td>" + rs.getString(1)+"</td><td>"+
rs.getString(2)+"</td><td>"+rs.getString(3)+"</td><td>"+rs.getString(4)+"</td></tr>");
}
%>
</table>
</pre></center>
</html>
JSP PRG 11. EXAMPLE FOR SETTING AND READING BEAN PROPERTIES
FROM JSP.
<%
response.setHeader("Cache-Control","no-cache");
%>
<jsp:useBean id="MyBean" class="sunitech.MyBean" />
<b>Get Property 1</b> <jsp:getProperty name="MyBean" property="name" /> <br>
<%-- Setting Bean Properties --%>
<jsp:setProperty name="MyBean" property="name" value="xxx
" /> <br>
<%-- Reading Bean Properties --%>
<b>Get Property 2 (getProperty way) : </b> <jsp:getProperty name="MyBean"
property="name" /> <br>
<b>Get Property 3 (getName way) : </b> <%=MyBean.getName()%>
JSP PRG 14. EXAMPLE FOR GETTING BEAN VALUE FROM SESSION AND
STOPSESSION.
<%
response.setHeader("Cache-Control","no-cache");
%>
<jsp:useBean id="MyBean" scope="session" class="sunitech.MyBean" />
<b>Propery value NAME from Session bean : </b> <%=MyBean.getName()%>
<b>Propery value DESIGNATION from Session bean : </b> <%=MyBean.getDesig()
%>
<% sessioninvalidate.jsp
response.setHeader("Cache-Control","no-cache");
session.invalidate();
%>
<h2>Session Invalidated</h2>
15a:
jsp file inside ec\1.jsp
tag handler file inside ec\WEB-INF\classes\hello\hai\one.java
tag description inside ec\WEB-INF\sun.tld
1.jsp
<%@ taglib uri="/WEB-INF/sun.tld" prefix="sunworld" %>
<html>
<body>
<center>
<h2>Custom Tag Example</h2>
<sunworld:1 />
</center>
</body>
</html>
one.java
package hello.hai;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class one extends TagSupport {
public int doStartTag() {
try {
JspWriter out = pageContext.getOut();
out.print("<font color=red size=+3>Sun iTech</font>");
} catch(IOException e)
{
System.out.println("Error" +e);
}
return(SKIP_BODY);
}
}
sun.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tag>
<name>1</name>
<tagclass>hello.hai.one</tagclass>
<info>Outputs Sun iTech in Blue</info>
</tag>
</taglib>
15b:
2.jsp
<%@ taglib uri="/WEB-INF/sun.tld" prefix="sunworld" %>
<html>
<body>
<center>
<h2>Custom Tag Demo</h2>
<sunworld:2 size="7" />
</center>
</body>
</html>
sun.tld
<tag>
<name>2</name>
<tagclass>hello.hai.two</tagclass>
<info>Outputs Sun iTech in Red</info>
<attribute>
<name>size</name>
<required>false</required>
</attribute>
</tag>
two.java
package hello.hai;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class two extends TagSupport {
private int size = 3;
public int doStartTag() {
try {
JspWriter out = pageContext.getOut();
out.print("<font color=blue size=+" + size + ">Sun
iTech</font>");
} catch(IOException e)
{
System.out.println("Error: " +e);
}
return(SKIP_BODY);
}
public void setSize(String s) {
try {
size = Integer.parseInt(s);
} catch(Exception e)
{
}
}
15c:
jsp file inside ec\3.jsp
tag handler file inside ec\WEB-INF\classes\hello\hai\three.java
tag description inside ec\WEB-INF\sun.tld
three.jsp
<%@ taglib uri="/WEB-INF/sun.tld" prefix="sunworld" %>
<html>
<body>
<center>
<h2>Custom Tag Demo</h2>
<sunworld:repeatTag times="7"><font size=+3>Sun
iTech</font><br></sunworld:repeatTag>
</center>
</body>
</html>
sun.tld
<tag>
<name>repeatTag</name>
<tagclass>hello.hai.RepeatTag</tagclass>
<bodycontent>JSP</bodycontent>
<attribute>
<name>times</name>
<required>false</required>
</attribute>
</tag>
three.java
package hello.hai;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;
public class three extends TagSupport
{
private int times = 3;
public void setTimes(int l) {
times = l;
}
public int doStartTag() {
if (times >= 1)
return EVAL_BODY_INCLUDE;
else
return SKIP_BODY;
}
public int doAfterBody() {
if ( times-- > 1 )
return EVAL_BODY_AGAIN;
else
return SKIP_BODY;
}
}
login.html
<html>
<body>
<form action="http://localhost:8070/sun/mark" method="get">
<pre>
Enter Batch Code :<input type="text" name="c"><br>
Enter Marks :<input type="text" name="m"><br>
<input type="submit">
</pre>
</form>
</body>
</html>
InvalidStudent.jsp
<b><h1>YOU ARE NOT A STUDENT!!!!!!!!!<h1></b>
NegativeMark.jsp
<b> <h1>MARK IS TOO LOW PLEASE WRITE RE-EXAM</h1></b>
GoodMark.jsp
<b><h1> Good!!!! Placement Department is waiting 4 u---</h1></b>
Marks.java
import DB.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Marks extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws
ServletException, IOException
{
String code=(String)req.getParameter("c");
Student s=Student.getCode(code);
String m="";
m=(String)req.getParameter("m");
if(!m.equals("")){
s.setMark(Integer.parseInt(m.trim()));
}
String ad="";
PrintWriter out=res.getWriter();
if (s.getCode().equals(""))
{
ad = "/WEB-INF/StudentDetails/InvalidStudent.jsp";
}
else if (s.getMark() < 0 || s.getMark() <=60)
{
ad = "/WEB-INF/StudentDetails/NegativeMark.jsp";
req.setAttribute("badStudent", s);
}
else if ( s.getMark() > 60)
{
ad = "/WEB-INF/StudentDetails/GoodMark.jsp";
}
if(ad.equals(""))
{
ad= "/login.html";
}
RequestDispatcher dispatcher =req.getRequestDispatcher(ad);
dispatcher.forward(req, res);
}
}
inside classes/DB/Student.java
Student.java
package DB;
public class Student {
private String c;
private int mark;
public Student(String co) {
c = co;
mark=0;
}
public String getCode() {
return c;
}
public int getMark() {
return mark;
}
public void setCode(String v) {
c = v;
}
public void setMark(int v) {
mark = v;
}
public static Student getCode(String code)
{
return new Student(code);
}
}