CREATE A SIMPLE SERVLET PROGRAM USING HttpServlet

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Hello extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out; String title = "Example of HttpServlet"; // set content type and other response header fields first response.setContentType("text/html"); // then write the data of the response out = response.getWriter(); out.println("<HTML><HEAD><TITLE>"); out.println(title); out.println("</TITLE></HEAD><BODY bgcolor=\"#FFFFFF\">"); out.println("<H1>" + title + "</H1>"); out.println("<H2>Hello WORLD!<br>"); out.println("</BODY></HTML>"); out.close(); } }

OUTPUT:

CREATE A SIMPLE FORM AND DISPLAY THE VALUES USING SERVLET PROGRAM
<html><body><form action="http://192.168.7.151/servlets/user" method=”get”> name:<input type="text" name=”pname”> age:<input type="text" name=”pid”> <input type="submit" name="submit" value="submit"></form></body></html> import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class getform extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {try { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String name=request.getParameter("pname"); String id=request.getParameter("pid"); out.println("<html>"); out.println("<head>"); out.println("<title>Retrieve</title>"); out.println("</head>"); out.println("<body>"); out.println("NAme::"+ name+"<br>"); out.println("Id::"+i d+"<br>"); out.println("</body>"); out.println("</html>");*/ }catch(Exception e){ System.out.println("Error"+e); } }}

OUTPUT:-

SIMPLE JSP PAGE THAT SHOWS DATE PROPERTY
< HTML> <BODY> <% // This scriptlet declares and initializes "date" System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); %> Hello! The time is now <% // This scriptlet generates HTML output out.println( String.valueOf( date )); %> </BODY> </HTML>

CONNECT A JSP PAGE TO DATABASE:
<%@ page language="java" import="java.sql.*,java.io.*" %> <%Connection con=null; Statement st=null; ResultSet rs=null; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:ss"); st=con.createStatement(); rs=st.executeQuery("select * from emp"); while(rs.next()) { out.println(rs.getString(1)); out.println(rs.getString(2)); } } catch(Exception e) { System.out.println(e); } %>

OUTPUT:-

CONNECT A SERVLET TO DATABASE :
import java.sql.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class dbcon extends HttpServlet { Connection con=null; Statement st=null; ResultSet rs=null; PrintWriter out; public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:ss"); st=con.createStatement(); rs=st.executeQuery("select * from emp"); res.setContentType("text/html"); out=res.getWriter(); while(rs.next()) { out.println(rs.getString(1)); out.println(rs.getString(2)); } } catch(Exception e) { out.println(e); } } }

OUTPUT:

SIMPLE SERVLET PROGRAM OF REQUEST HEADER USING GENERIC SERVLET
import java.io.*; import java.util.*; import javax.servlet.*; import java.lang.*; public class ServerSnoop extends GenericServlet { public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { res.setContentType("text/plain"); PrintWriter out = res.getWriter(); out.println("req.getServerName(): " + req.getServerName()); out.println("req.getServerPort(): " + req.getServerPort()); out.println("getServletContext().getServerInfo(): " + getServletContext().getServerInfo()); out.println("getServerInfo() version: " +getServletContext().getServerInfo()); out.println("getServletContext().getAttribute(\"attribute\"): " + getServletContext().getAttribute("attribute")); } }

OUTPUT:

CREATE A SIMPLE XML FILE THAT CONTAINS MUSIC CATALOG
<?xml version="1.0" encoding="UTF-8"?> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> <CD> <TITLE>Hide your heart</TITLE> <ARTIST>Bonnie Tyler</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>CBS Records</COMPANY> <PRICE>9.90</PRICE> <YEAR>1988</YEAR> </CD></CATALOG>

OUTPUT:

CREATE AN INTERNAL DTD FOR ADDRESS
<?xml version="1.0"?> <!DOCTYPE address [ <!ELEMENT address (first,middle,last)> <!ELEMENT first (#PCDATA)> <!ELEMENT middle (#PCDATA)> <!ELEMENT last (#PCDATA)> ]> <address> <first>h-175</first> <middle>first floor,sector-22</middle> <last>noida,u.p.</last> </address> OUTPUT:

EXTERNAL DTD FOR ADDRESS
//store in “add.dtd” <!ELEMENT address (first,middle,last)> <!ELEMENT first (#PCDATA)> <!ELEMENT middle (#PCDATA)> <!ELEMENT last (#PCDATA)> //save as xml file <!DOCTYPE address SYSTEM "add.dtd"> <address> <first>h-175</first> <middle>first floor,sector-22</middle> <last>noida,u.p.</last> </address>

OUTPUT:

PARSER EXAMPLE
parser.html <html><body><script type="text/javascript"> var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.load("note.xml") document.write("The first XML element in the file contains: ") document.write(xmlDoc.documentElement.childNodes.item(0).text) </script></body></html> note.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>

OUTPUT:

Steps for Creating a simple bean
Creating a simple Bean Compiling and saving the Bean into a Java Archive (JAR) file • Loading the Bean into the ToolBox • Dropping a Bean instance into the BeanBox • Inspecting the Bean's properties, methods, and events • Generating an introspection report

The Bean will be named SimpleBean. Here are the steps to create it and view it in the BeanBox: Write the SimpleBean code. Put it in a file named SimpleBean.java, in the directory of the choice. Here's the code: 1. impot java.awt.*; 2. import java.io.Serializable; 3. 4. public class SimpleBean extends Canvas 5. implements Serializable 6. { 7. //Constructor sets inherited properties 8. public SimpleBean(){ 9. setSize(60,40); 10. setBackground(Color.red); 11. } 12. } SimpleBean extends the java.awt.Canvas component. SimpleBean also implements the java.io.Serializable interface, a requirement for all Beans. SimpleBean sets the background color and component size. Make sure the CLASSPATH environment variable is set to point to all needed .class (or .jar) files.

Compile the Bean: javac SimpleBean.java This produces the class file SimpleBean.class Create a manifest file. Use the favorite text editor to create a file, we'll call it manifest.tmp, that contains the following text: Name: SimpleBean.class Java-Bean: True Create the JAR file. The JAR file will contain the manifest and the SimpleBean class file: jar cfm SimpleBean.jar manifest.tmp SimpleBean.class Load the JAR file into the ToolBox. Select the File|LoadJar... menu item. This will bring up a file browser. Navigate to the SimpleBean.jar location and select it. SimpleBean will appear at the bottom of the ToolBox. (Note that when the BeanBox is started, all Beans in JAR files in the beans/jars directory are automatically loaded into the ToolBox). Drop a SimpleBean instance into the BeanBox. Click on the word SimpleBean in the ToolBox. The cursor will change to a crosshair. Move the cursor to a spot within the BeanBox and click. SimpleBean will appear as a painted rectangle with a hatched border. This border means that SimpleBean is selected. The SimpleBean properties will appear in the Properties sheet. You can resize SimpleBean, because it inherits from Canvas, by dragging a corner. You will see the cursor change to a right angle when over a corner. You can also reposition SimpleBean within the BeanBox by dragging on any non-corner portion of the hatched border. You will see the cursor change to crossed arrows when in position to move the Bean.

EXAMPLE OF ACTION ELEMENT: USEBEAN, GETPROPERTY
<%@ page language="java" contentType="text/html" %> <html> <body color="pink"> <jsp:useBean id="c" class="java.util.Date"/> the current DATE AND TIME is <ul> <li>Date <jsp:getProperty name="c" property="date"/> <li>month<jsp:getProperty name="c" property="month"/> <li>year <jsp:getProperty name="c" property="year"/> <li>minutes <jsp:getProperty name="c" property="minutes"/> <li>seconds <jsp:getProperty name="c" property="seconds"/> </ul> </body> </html>

OUTPUT:

JSP PAGE THAT USES REQUEST IMPLICIT OBJECT:
<%@page contentType="text/html" import="java.util.*" %> <html><body><p><font size="5" color="#800000">Request Information:</font></p> <div align="left"><table border="0" cellpadding="0" cellspacing="0" width="70%" bgcolor="#EEFFCA"><tr><td width="33%"><b><font color="#800000">Request Method:</font></b></td><td width="67%"><font color="#FF0000"><%=request.getMethod() %></font></td></tr> <tr><td width="33%"><b><font color="#800000">Request URI:</font></b></td> <td width="67%"><fontcolor="#FF0000">< %=request.getRequestURI()%></font></td> </tr><tr><td width="33%"><b><font color="#800000">Request Protocol:</font></b></td><td width="67%"><font color="#FF0000">< %=request.getProtocol()%></font></td></tr> <tr><td width="33%"><b><font color="#800000">Path Info:</font></b></td> <td width="67%"><font color="#FF0000"><%=request.getPathInfo() %></font></td> </tr> <tr><td width="33%"><b><font color="#800000">Path translated:</font></b></td> <td width="67%"><fontcolor="#FF0000">< %=request.getPathTranslated()%></font></td> </tr> <tr><td width="33%"><b><font color="#800000">Query String:</font></b></td> <td width="67%"><fontcolor="#FF0000">< %=request.getQueryString()%></font></td> </tr><tr>

<td width="33%"><b><font color="#800000">Content length:</font></b></td> <td width="67%"><font color="#FF0000">< %=request.getContentLength()%></font></td></tr> <tr><td width="33%"><b><font color="#800000">Content type:</font></b></td> <td width="67%"><fontcolor="#FF0000">< %=request.getContentType()%></font></td> </tr><tr> <td width="33%"><b><font color="#800000">Server name:</font></b></td><td width="67%"><font color="#FF0000">< %=request.getServerName()%></font></td> </tr><tr> <td width="33%"><b><font color="#800000">Server port:</font></b></td> <td width="67%"><font color="#FF0000">< %=request.getServerPort()%></font></td> </tr><tr> <td width="33%"><b><font color="#800000">Remote user:</font></b></td> <td width="67%"><font color="#FF0000">< %=request.getRemoteUser()%></font></td> </tr><tr><td width="33%"><b><font color="#800000">Remoteaddress:</font></b></td> <td width="67%"><font color="#FF0000">< %=request.getRemoteAddr()%></font></td></tr> <tr><td width="33%"><b><font color="#800000">Remote host:</font></b></td> <tdwidth="67%"><fontcolor="#FF0000">< %=request.getRemoteHost()%></font></td> </tr><tr><td width="33%"><b><font color="#800000">Authorization scheme:</font></b></td><td width="67%"><font color="#FF0000">< %=request.getAuthType()%></font></td></tr></table></div> </body></html>

OUTPUT:

JSP PROGRAM TO DEMONSTRATE CONDITIONAL PROCESSING
<%@ page language="java" %> <html> <body> <jsp:useBean id="clock" class="java.util.Date"/>

<% if (clock.getHours()<=12 ){%> good morning <% } else if ((clock.getHours() >12) && (clock.getHours() <=15)) {%> good day <%}else {%> good evening <%}%> </body> </html>

OUTPUT:

BIO DATA FORM USING HTML TAGS:
<html> <head><title>Bio Data Form</title> </head> <body bgcolor="pink">

<h2><marquee><b><u>BIO DATA FORM</u></b></marquee></h2><br><br> <b>NAME <input type="text" name="t7" size="40"> <br> <p></p> <table> <tr> <td> <b>PERMANENT ADD <BR> <input type="text" name="t1" size="40"><br> <input type="text" name="t2" size="40"><br> <input type="text" name="t3" size="40"><br> Pin <input type="text" name="t8" size="10"> </td> <td> </td> <td> <b>CORRESSPONDENCE ADD<BR> <input type="text" name="t4" size="40"><br> <input type="text" name="t5" size="40"><br> <input type="text" name="t6" size="40"><br> Pin <input type="text" name="t9" size="10"> </td> </tr> </table> <p></p> Tel.<input type="text" name="t10" size="40"><br> Mb.<input type="text" name="t11" size="40"><br><hr> <p><b><u> PERSONAL INFORMATION</u></p> Date of Birth: <input type="text" name="t12" size="2"> <input type="text" name="t13" size="2"> <input type="text" name="t14" size="2"><br>

<form> Marital Status:<input type="radio" value="single" name="status" accesskey="s">Single <input type="radio" value="married" name="status" accesskey="ma">Married </form> <form> Sex : <input id="male" accesskey="m" type="radio" name="Sex" value="Male" >Male <input id="female" accesskey="f" type="radio"name="Sex" value="Female" >Female </form> <form> Nationality: <input type="text" name="t15" size="20"><br><br> </form> <p><b>Language</b></p> <form align="center"> speak&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp <input type="text" name="t16" size="5"> <input type="text" name="t17" size="5"> <input type="text" name="t18" size="5"><br> write &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp <input type="text" name="t16" size="5"> <input type="text" name="t17" size="5"> <input type="text" name="t18" size="5"><br> understand&nbsp <input type="text" name="t16" size="5"> <input type="text" name="t17" size="5"> <input type="text" name="t18" size="5"> </form><hr><br> <B><u>ACADEMICS</u></b><br><br> <form> Maximum Qualification <select id="mySelect"> <option>UG</option> <option>PG</option> <option>MS</option>

<option>Phd</option> </select> <br> 12th percentage &nbsp<input type="text" name="t19" size="4">% &nbsp&nbsp&nbsp&nbsp passing year <input type="text" name="t19" size="4"> <br> 10th percentage&nbsp <input type="text" name="t20" size="4">% &nbsp&nbsp&nbsp&nbsp passing year <input type="text" name="t19" size="4"><br> Other Activities&nbsp <input type="text" name="t21" size="20"> <br> </form> <hr> <b><u> DETAILS OF EXPERIENCE</u></b><BR> <form> <table border="1"> <tr> <td>S.no.</td> <td>Organisation</td> <td>Designation</td> <td>Job Responsbilities</td> <td>period</td> </tr> <tr> <td><input type="text" size="2"></td> <td><input type="text" size="15"></td> <td><input type="text" size="15"></td> <td><input type="text" size="15"></td> <td><input type="text" size="10"></td> </tr> <tr> <td><input type="text" size="2"></td> <td><input type="text" size="15"></td> <td><input type="text" size="15"></td> <td><input type="text" size="15"></td> <td><input type="text" size="10"></td> </tr><tr> <td><input type="text" size="2"></td> <td><input type="text" size="15"></td> <td><input type="text" size="15"></td>

<td><input type="text" size="15"></td> <td><input type="text" size="10"></td> </tr> </table> </form> <p><b><u>ADDITIONAL INFORMATIONS</u></b></p> <FORM> <input type="text" size="50"></form> </body> </html>

TABLE USING SOME PROPERTIES:

<html> <body> <p> Each table starts with a table tag. Each table row starts with a tr tag. Each table data starts with a td tag. </p> <h4>One column:</h4> <table border="1"> <tr> <td>100</td> </tr> </table> <h4>One row and three columns:</h4> <table border="1"> <tr> <td>100</td> <td>200</td> <td>300</td> </tr> </table> </body> </html>

OUTPUT:

JAVASCRIPT CODE FOR WINDOW OBJECT:

<html><head> <script language="javascript"> var r; function f() { r = open('a4.html','windownameistest' , 'height=500 width=500') } function f1() { if (r.closed) alert("already closed") } else {r.close() } } </script> </head> <body> <input type="button" name="n1" value="openwindow" onclick="f()"> <input type="button" name="n1" value="closewindow" onclick="f1()"> </body> </html>

OUTPUT:

JAVASCRIPT CODE TO IMPLEMENT DOCUMENT OBJECT:
<html> <head> <title>properties</title> <script language="javascript"> function f() { document.write("location :"+ location +"<br>"); document.fgColor="0000ff" document.write("filecreateddate :==" +document.fileCreatedDate +"<br>"); document.write("fileupdateddate :=="+document.fileUpdatedDate+"<br>"); document.write("filelastmodified :=="+document.lastModified+"<br>"); document.write("mimetype :== "+document.mimeType+"<br>"); document.write("protocol :=="+document.protocol +"<br>"); document.write("referrer :=="+document.referrer +"<br>"); document.write("URL:=="+document.URL+"<br>"); } </script> </head> <body alink="green" vlink="orange" > <input type="button" name="n1" value="click" onclick='f()'> <a href="a.html"> click </a> </body> </html> <!-- location :file:///C:/javascript2/documentobject.html filecreateddate :==08/13/2008 fileupdateddate :== filelastmodified :==08/13/2008 14:43:12 mimetype :== HTML Document protocol :==File Protocol referrer :== URL:==file://C:\javascript2\documentobject.html title:== -->

OUTPUT:

JAVA SCRIPT CODE FOR NAVIGATOR OBJECT:
<html> <head> <title> navigator</title> <script language="javascript"> function f() { document.write("appcodename"+navigator.appCodename+"<br>") document.write("<br>") document.write("appname"+ navigator.appName+"<br>") document.write("<br>") document.write("appversion"+navigator.appVersion) document.write("<br>") document.write("language"+navigator.language+"<br>") document.write("platform"+navigator.platform+"<br>") document.write("useragent"+navigator.userAgent) } </script> </head> <body> <input type="button" name="n1" value="v1" onclick='f()'> </body> </html>

OUTPUT:

JAVA SCRIPT CODE FOR FRAME:
<frameset cols="50%,50%"> <frame src ="a.html" name="a"> <frame src ="b.html" name="b"> <frameset>

OUTPUT:

JAVA SCRIPT CODE FOR HISTORY OBJECT:
<html> <head> <title> loaction object-replace </title> <script language="javascript"> function f() { z=open("a.html ","first","height=100 width=200") z.location.href="b.html" z.location.href="c.html" } </script> <body> <FORM> <INPUT TYPE="button" VALUE="click to open pages" onClick="f()"> <INPUT TYPE="button" VALUE="Go Back" onClick="z.history.back()"> INPUT TYPE="button" VALUE="forward" onClick="z.history.forward()"> <INPUT TYPE="button" VALUE="-1" onClick="z.history.go(-1)"> </FORM> </body> </head> </html>

OUTPUT:

EVENT HANDLING: - CLICK EVENT
<html> <head> <title>eventler returning a value</title> <script language="javascript"> function function3() { return confirm("do you want to confirm"); } </script> </head> <body> <form name="n1"> <a href="http:\\www.yahoo.com" onclick='return("false")' ondblclick='funtion3()'>visit </a> </form> </body> </html>

EVENT HANDLING:- BLUR EVENT
<html><head> <title>blur</title> <script language="javascript"> function f1() { window.document.bgColor="red" alert('focused') } function f2() { window.document.bgColor="green" alert('lostfocus') } </script> </head> <body> <form name="n1"> <input type="text" name="t1" value="v1" onBlur='f2()' onfocus='f1()'> </form> </body> </html>

OUTPUT:

EVENT HANDLING:- CHANGE EVENT
<html> <head> <title>blur</title> <script language="javascript"> function f() {alert(window.document.n1.t1.value) window.document.n1.t1.value="changed" } </script> </head> <body> <form name="n1"> <input type="text" name="t1" value="v1" onchange='f()'> </form></body></html>

OUTPUT:

EVENT HANDLING:- FOCUS EVENT
<html><head> <script type="text/javascript" language="JavaScript"> s1 = new String(myForm.myText.value) function changeVal() { s1 = "I'm feeling focused" document.myForm.myText.value = s1.toUpperCase() } </script></head><body> <form name="myForm"> <input type="text" name="myText" value="Give me focus" onFocus = "changeVal()"> </form></body></html>

OUTPUT:

EVENT HANDLING:- MOUSE OVER
<html><head> <title>event-onmouseover</title> <script language="javascript"> function f1() { alert(window.document.n1.s1.value()) } </script> </head> <body> <A HREF=""onMouseOver="document.bgColor='black'" onmouseout= "document.bgColor='pink'" >Black</a> <A HREF=""onMouseOver="document.bgColor='green'">Green</a> <A HREF=""onMouseOver="document.bgColor='yellow'">Yellow</a> <A HREF=""onMouseOver="document.bgColor='red'">Red</a> <A HREF=""onMouseOver="document.bgColor='brown'">Brown</a> <A HREF=""onMouseOver="document.bgColor='white'">White</a> </body></html>

OUTPUT:-

EVENT HANDLING:- MOUSE OUT
<html><head> <title>event-onclickonmouseover</title> <script language="javascript"> function function3(ff) { window.document.n1.v1.value=ff } </script> </head> <body> <!-THIS IS A COMMENT TAG //--> <form name="n1"> <input type="text" name="v1" value="jjjjj"> <a href="seven.html" onmouseover='function3("over")' onmouseout='function3("out")'> click </a> </form></body></html>

OUTPUT:

CREATE A TUTORIAL:
<html> <head></head> <body> <h1>TUTORIAL ON TABLES <h4>a feature common to many j2ee application is for a user to recall information that is stored in database. To store information in efficient manner we use tabls. </body> </html>

OUTPUT:

TWO HTML THAT ARE LINKED WITH EACH OTHER
“a.html” <html><head></head> <body> <a href="b.html"> click here </a> </body></html> “b.html” <html><head></head> <body> </B>hello</B> </body></html>

OUTPUT:

XML WITH CSS catalog.css
CATALOG { background-color: #ffffff; width: 100%; } CD { display: block; margin-bottom: 30pt; margin-left: 0; } TITLE { color: #FF0000;

font-size: 20pt; } ARTIST { color: #0000FF; font-size: 20pt; } COUNTRY,PRICE,YEAR,COMPANY { display: block; color: #000000; margin-left: 20pt; }

catalog.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/css" href="catalog.css"?> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> <CD> <TITLE>Hide your heart</TITLE> <ARTIST>Bonnie Tyler</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>CBS Records</COMPANY> <PRICE>9.90</PRICE> <YEAR>1988</YEAR> </CD> </CATALOG>

OUTPUT:

Sign up to vote on this title
UsefulNot useful