Sunteți pe pagina 1din 37

CHALAPATHI INSTITUTE OF TECHNOLOGY

Week 1: Write a java script program to validate user registration form which contains the following elements a) Username(text box) b)new password c.confirm password d.gender(radio buttons) e.highest qualification(combo box) f.hobbies (check box) <html> <head> <title>Week 1 User Registration Form</title> <script type="text/javascript"> function validate() { var x=document.f1.tura.value; var y=document.f1.tpass.value; var y1=document.f1.tconfpass.value; var gen; var err=0; if(x.length==0) { window.alert("User name should not be blank "); err=1; } if(y.length<6) { window.alert("password should not be less than 6 characters"); err=1; } if(y!=y1) { window.alert("Password mismatch, Try again!"); err=1; } if(document.f1.g1[0].checked) gen="Male"; else if(document.f1.g1[1].checked) gen="Female"; else { alert("You must select your gender!"); err=1; } if(document.f1.qual.selectedIndex==0) { alert("You must select your qualification!"); err=1; } if(err==0) alert("Registraion Successfully Completed!!"); }
Web Technologies Lab 1 CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

</script> </head> <body bgcolor="aqua"> <center><h2>User Registration Form</h2> <p> <form name="f1" onSubmit="validate()"> <table border=2> <tr> <th>User name</th> <td><input type="text" name="tura" size=30></td> </tr> <tr> <th>Password</th> <td><input type="password" name="tpass" size="30"></td> </tr> <tr> <th>Confirm password</th> <td><input type="password" name="tconfpass" size="30"></td> </tr> <tr> <th>Gender</th> <td> <input type="radio" name="g1" value="f">Female <input type="radio" name="g1" value="m">Male </td> </tr> <tr> <th>Heighest qualification</th> <td> <select name="qual" size=1> <option value="Select">--Select--</option> <option value="B.Tech">B.Tech</option> <option value="M.C.A">M.C.A</option> <option value="M.Tech">M.Tech</option> <option value="Ph.D">Ph.D</option> <option value="Others">Others</option> </select> </td> </tr> <tr> <th>Hobbies</th> <td><input type="checkbox" name="h1">Writing <input type="checkbox" name="h2">Reading <input type="checkbox" name="h3">Studying </td> </tr> <tr><td>&nbsp;</td><td> <input type=submit value="Submit"> <input type=reset value="Reset"> </td></tr>
Web Technologies Lab 2 CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

</table> </form></center> </body> </html> OUTPUT

Web Technologies Lab

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

Week 2: a) Write a java program to handle mouse events for each event display a message box and change the document color. <HTML> <HEAD> <TITLE>Week 2-A Mouse Events in JavaScript </TITLE> </HEAD> <BODY> <center><h1>Mouse Events in JavaScript</h1> <form name="f1"> <input type="button" value="Single Click Example" name="b1" onclick="click1();"><br><br> <input type="button" value="Double Click Example" name="b2" ondblclick="click2();"><br><br> <input type="button" value="MouseOver Example" name="b3" onmouseover="Msover();"><br><br> <input type="button" value="MouseDown Example" name="b4" onmousedown="Msdown();"><br><br> <input type="button" value="MouseUp Example" name="b5" onmouseup="Msup();"> </form> </center> <script type="text/javascript"> var clr; function click1() { alert("Event identified: Mouse Click"); document.bgColor="Red"; } function click2() { alert("Event identified: Mouse Double Click"); document.bgColor="Green"; } function Msover() { alert("Event identified: Mouse Over"); document.bgColor="Blue"; } function Msdown() { alert("Event identified: Mouse Down"); document.bgColor="Aqua"; } function Msup() { alert("Event identified: Mouse Up"); document.bgColor="Yellow"; } </script> </BODY> </HTML>
Web Technologies Lab 4 CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

OUTPUT

Web Technologies Lab

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

b) Write a java script program to find whether a given string is palindrome or not <HTML> <HEAD> <TITLE> Week 2-B Palindrome in JavaScript</TITLE> <script type="text/javascript"> function isPalindrome() { var word=document.f1.t1.value; var flag=0; if(word.length==0) alert("Please enter text..."); else { var i; for (i = 0; i < word.length; i++) if (word.charAt(i) != word.charAt(word.length-1-i)) flag=1; } if(flag==0) document.writeln(word+" is <b>PALINDROME</b>"); else document.writeln(word+" is <b><u>NOT</u> PALINDROME</b>"); } </script> </HEAD> <BODY> <center> <h1>Palindrome Test in JavaScript</h1> <br> <form name="f1"> Enter Text: <input type="text" name="t1" size="20"> <br> <input type="button" name="b1" value="Check Palindrome" onclick="isPalindrome();"> </BODY> </HTML>

Web Technologies Lab

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

OUTPUT

Web Technologies Lab

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

Week 3 Write a DHTML document which applies gradient effect eo an iamge (sunset.jpg) for a given color and strength <HTML> <HEAD> <TITLE>Week 3-A Gradient Effect on Image : DHTML</TITLE> </HEAD> <BODY> <img src="Sunset.jpg" width="300" height="200"> <hr> <div style="position: absolute; top=10; left=400; filter=alpha(style=2,opacity=100,finishopacity=2)"; <img src="Sunset.jpg" width="300" height="200"> /div> <table width="100%"> <tr><td><h1>Normal Picture</h1></td> <td><h1>Gradient Effect</h1></td> </tr> </table> </BODY></HTML>

Web Technologies Lab

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

b) write a DHTML document which applies a sine wave effect to a given text <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Week 3-B Text Effects in DHTML</TITLE> </HEAD> <BODY> <h3>Normal Text: </h3> <H1>WEB TECHNOLOGIES</H1> <br> <hr> <h3>Wave Filter:</h3> <br> <span style="position=absolute; filter: wave(add=0, freq=1, phase=15, strength=13); fontsize:3em">WEB TECHNOLOGIES</span> </BODY> </HTML> Output:

Web Technologies Lab

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

Week 4: Write a program for parsing an xml document employee.xml (which contains the attributes empid,empname,designation,salary using DOM parser. DOMValidateDTD.java import java.io.*; import org.w3c.dom.*; import org.xml.sax.*; import javax.xml.parsers.*; import javax.xml.validation.*; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; public class DOMValidateDTD { public static void main(String args[]) { try{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new org.xml.sax.ErrorHandler() { //Ignore the fatal errors public void fatalError(SAXParseException exception) throws SAXException { } //Validation errors public void error(SAXParseException e) throws SAXParseException { System.out.println("Error at " +e.getLineNumber() + " line."); System.out.println(e.getMessage()); System.exit(0); } //Show warnings public void warning(SAXParseException err) throws SAXParseException{ System.out.println(err.getMessage()); System.exit(0); } }); Document xmlDocument = builder.parse(new FileInputStream("Employeexy.xml")); DOMSource source = new DOMSource(xmlDocument); StreamResult result = new StreamResult(System.out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "Employee.dtd"); transformer.transform(source, result); } catch (Exception e) { System.out.println(e.getMessage()); } } }

Web Technologies Lab

10

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

Output:

Web Technologies Lab

11

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

Week 5: Create an xml document employee.xml (which contains the attributesempid,empname,designation,salary).Write a DTD to validate the above xml file and display the XML file using XSL in the following format. Employee.xml <?xml version = "1.0" ?> <?xml-stylesheet type="text/xsl" href="empinfo.xsl"?> <!DOCTYPE Employees SYSTEM "Employee.dtd"> <Employees> <Employee> <Emp_Id> 001 </Emp_Id> <Emp_Name> Ramu </Emp_Name> <Emp_Designation> Assoc.Prof </Emp_Designation> <Emp_Salary> 40000 </Emp_Salary> </Employee> <Employee> <Emp_Id>002</Emp_Id> <Emp_Name>RAJU</Emp_Name> <Emp_Designation>ASST.PROF</Emp_Designation> <Emp_Salary>24000</Emp_Salary> </Employee> <Employee> <Emp_Id> 003 </Emp_Id> <Emp_Name> SITA </Emp_Name> <Emp_Designation>PROFESSOR</Emp_Designation> <Emp_Salary>70000</Emp_Salary> </Employee> </Employees> empinfo.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <html> <body> <table border="1" width="400:px"> <tr> <th>Employee ID</th> <th>Employee Name</th> <th>Designation</th> <th>Salary</th> </tr> <xsl:for-each select="Employees/Employee"> <tr style="color:red"> <td><xsl:value-of select="Emp_Id"/></td> <td><xsl:value-of select="Emp_Name"/></td> <td><xsl:value-of select="Emp_Designation"/></td>
Web Technologies Lab 12 CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

<td><xsl:value-of select="Emp_Salary"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> Employee.dtd <!ELEMENT Employees (Employee*)> <!ELEMENT Employee (Emp_Id*, Emp_Name*, Emp_Designation*, Emp_Salary*)> <!ELEMENT Emp_Id (#PCDATA)> <!ELEMENT Emp_Name (#PCDATA)> <!ELEMENT Emp_Designation (#PCDATA)> <!ELEMENT Emp_Salary (#PCDATA)> Output:

Web Technologies Lab

13

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

WEEK 6: Develop a java bean with are filled with a color. The shape of the area depends on the property shape. If it is set to true then the shape of the area is square and it is circle, if it is false. The color of the area should be changed dynamically for every mouse click. The color should also be changed if we change the color in the property window. Step: 1) Colors.java //This is simple bean changing the color on mouse click event package sunw.demo.colors; import java.awt.*; import java.awt.event.*; public class Colors extends Canvas { transient private Color color; private boolean rectangular; public Colors() { addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent me) { change(); } }); rectangular = false; setSize(200, 100); change(); } public boolean getRectangular() {return rectangular; } public void setRectangular(boolean flag) { this.rectangular = flag; repaint(); } public void change() { color = randomColor(); repaint(); } private Color randomColor() { int r = (int)(255*Math.random()); int g = (int)(255*Math.random()); int b = (int)(255*Math.random()); return new Color(r, g, b); } public void paint(Graphics g) { Dimension d = getSize(); int h = d.height; int w = d.width; g.setColor(color); if(rectangular) {
Web Technologies Lab 14 CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

g.fillRect(0, 0, w-1, h-1); } else { g.fillOval(0, 0, w-1, h-1); } } } Step 2) Create a separate directory colors Save this file in C:\beans\bdk\demo\sunw\demo\color Compile the java program using the following command. At Command Prompt C:\beans\demo\sunw\demo\colors>javac Colors.java Due to this command class file gets generated. Step3) Now the manifest file is written as given below. This file will be stored in the folder of c:\beans\demo Colors.mft Name: sunw/demo/colors/Colors.class Java-Bean: True Step 4) Now we will create a jar file using the jar cfm command. The command can be given as below

Step 5) Now give the following command to start the BDK C:\beans\beanbox>run

Web Technologies Lab

15

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

Step 6): Now in the ToolBox window the bean component Colors will appear.Click it and place it on the BeanBox window. Step 7) Now to user bound properties TickTock bean,place the bean TIckTock on the BeanBox just below the Colors bean.Change the interval from 5 to 1. Now from the menu bar select Edit->Events->PropertyChange. A line appears, which must be extended to the bean Colros which is placed on the BeanBox .now the target method named Change must be selected. The adapter class gets generated. Step 8) Thus now Color bean gets configured with the TickTock bean.The colors gets changed on every interval of TickTock bean. Step 9) If we eant to change the shape of Colors bean ,Click on the Colors bean and then got o properties window. Set the property rect_shape to True.we will get that colors in the rectangle gets changed. Output:

Web Technologies Lab

16

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

WEEK 7: Without using parametes Login.html <html> <head> <title>LOGIN FORM</title> </head> <body> <center> <form name="Form1" method=get action="http://localhost:8080/servlets-examples/servlet/CookieDemo"> <table> <tr> <td><B>UserName</td> <td><input type="text" name="UserName" size="25" value=""></td> </tr> <tr> <td><B>PassWord</td> <td><input type="password" name="PassWord" size="25" value=""></td></tr> </table> <input type=submit value="submit"> </body> </html> CookieDemo.java import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class CookieDemo extends HttpServlet { public void init(ServletConfig config) throws ServletException { super.init(config); } public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { res.setContentType("text/html"); Cookie My_cookie=new Cookie("null","null"); Enumeration keys; String key,value; PrintWriter out=res.getWriter(); keys=req.getParameterNames(); while(keys.hasMoreElements()) { key=(String)keys.nextElement(); value=req.getParameter(key);
Web Technologies Lab 17 CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

My_cookie=new Cookie(value,key); res.addCookie(My_cookie); } out.println("\n The Cookie is added"); out.close(); } } Save the above program on the following path and compile the program

getCookieServlet.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class getCookieServlet extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { Cookie[] my_cookies =req.getCookies(); String name[]=new String[10]; String value[]=new String[10]; int i; res.setContentType("text/html"); PrintWriter out=res.getWriter(); out.println("<b>"); int n=my_cookies.length; for(i=0;i<n;i++) { name[i]=my_cookies[i].getName(); value[i]=my_cookies[i].getName(); } for(i=0;i<n;i=i+2) { try
Web Technologies Lab 18 CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

{ out.println("<br/>"); if(name[i].equals("pwd1")) { if(name[i+1].equals("user1")) { out.println("WELCOME"+name[i+1]); } else { out.println(" You are not authenticate user"); } } else if(name[i].equals("pwd2")) { if(name[i+1].equals("user2")) { out.println("WELCOME"+name[i+1]); } else { out.println(" You are not authenticate user"); } } else if(name[i].equals("pwd3")) { if(name[i+1].equals("user3")) { out.println("WELCOME"+name[i+1]); } else { out.println(" You are not authenticate user"); } } else if(name[i].equals("pwd4")) { if(name[i+1].equals("user4")) { out.println("WELCOME"+name[i+1]); } else { out.println(" You are not authenticate user"); } } } catch(Exception e) { out.println("\n Invalid username/password"); } }//end of try
Web Technologies Lab 19 CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

}//end of function }

Start the apache tomcat server Go to C:\Program Files\Apache Software Foundation\Tomcat 4.1\bin Click on the startup batch file Minimize it Now check web server is http://localhost:8080

Web Technologies Lab

20

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

And open the click on login.html file

In the WEB-INF directory C:\Program Files\Apache Software Foundation\Tomcat 4.1\webapps\servlets-examples\WEB-INF There is web.xml file.open it using notepad and edit is as follows---<servlet> <servlet-name> getCookieServlet </servlet-name>
Web Technologies Lab 21 CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

<servlet-class> getCookieServlet </servlet-class> </servlet> --<servlet-mapping> <servlet-name>CookieDemo</servlet-name> <url-pattern>/servlet/CookieDemo*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name> getCookieServlet</servlet-name> <url-pattern>/servlet /getCookieServlet </url-pattern> </servlet-mapping> With using parameters Create a directory servlets-examples In that copy WEB-INF folder. login.html <html> <head> <title>LOGIN FORM</title> </head> <body> <center> <form name="Form1" method=get action="http://localhost:8080/servletsexamples/servlet/getuserServlet"> <table> <tr> <td><B>UserName</td> <td><input type="text" name="UserName" size="25" value=""></td> </tr> <tr> <td><B>PassWord</td> <td><input type="password" name="PassWord" size="25" value=""></td> </tr> </table> <input type=submit value="submit"> </body> </html> Save the above htm file in the C:\Program Files\Apache Software Foundation\Tomcat 4.1\webapps\servlets-examples\WEB-INF

Web Technologies Lab

22

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

getuserServlet.java import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class getuserServlet extends HttpServlet { private String UserName,PassWord; public void init() { ServletConfig config=getServletConfig(); UserName=config.getInitParameter("UserName"); PassWord=config.getInitParameter("PassWord"); } public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { Enumeration keys; String key,value[]; value=new String[4];int i=0; res.setContentType("text/html"); PrintWriter out=res.getWriter(); keys=req.getParameterNames(); while(keys.hasMoreElements()) { key=(String)keys.nextElement(); value[i]=req.getParameter(key); i++; } i=0; if((UserName.equals(value[i+1]))&&(PassWord.equals(value[i]))) { out.println("<h2>"); out.println("Welcome"+UserName+"!!!"); } else { out.println("<h2>"); out.println("YOU are not Authenticated"); } out.close(); } } Save the above file in the below path

Web Technologies Lab

23

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

Web.xml <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-appPUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"><web-app><servlet><servletname>getuserServlet</servlet-name><servlet-class>getuserServlet</servlet-class><initparam><param-name>UserName</param-name><param-value>user1</param-value></initparam>

<init-param> <param-name>PassWord</param-name> <param-value>pwd1</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>getuserServlet</servlet-name> <url-pattern>/servlet/getuserServlet</url-pattern> </servlet-mapping> </web-app> Save the above file in the path of C:\Program Files\Apache Software Foundation\Tomcat 4.1\webapps\servlets-examples\WEB-INF Output: start the tomcat Open the login.html file Give username as user1 and password as pwd1

Web Technologies Lab

24

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

And try for user2 type any kind password we will get the following

Web Technologies Lab

25

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

WEEK 8: Develop a web application to display session tracking information for Login form application using URL rewriting import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class SessionExample extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(true); // print session info Date created = new Date(session.getCreationTime()); Date accessed = new Date(session.getLastAccessedTime()); out.println("ID " + session.getId()); out.println("Created: " + created); out.println("Last Accessed: " + accessed); // set session info if needed String dataName = request.getParameter("dataName"); if (dataName != null && dataName.length() > 0) { String dataValue = request.getParameter("dataValue"); session.setAttribute(dataName, dataValue); } // print session contents Enumeration e = session.getAttributeNames(); while (e.hasMoreElements()) { String name = (String)e.nextElement(); String value = session.getAttribute(name).toString(); out.println(name + " = " + value); } }} Output: Sessions Example Session ID: E55B7AE87EEC8556A084BBFF47E96155 Created: Thu May 29 12:00:08 IST 2008 Last Accessed: Thu May 29 12:00:43 IST 2008 The following data is in your session: ANANTH = 1234 RAM = 4321 Name of Session Attribute: Value of Session Attribute: GET based form: Name of Session Attribute: Value of Session Attribute: URL encoded

Web Technologies Lab

26

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

Week 9: Develop a web application to implement Login form (WEEK 7) using JSP URLrewriting.html <html> <head> <title>How to use URL rewriting in jsp</title> </head> <body> <center><B><font size="4" color="red">LOGIN PAGE</font></B> <form method = "post" action = "URLrewritingMain.jsp"> <font size = 4 color="blue">Enter your name :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type = "text" name = "name"></font><br><br> <font size = 4 color="blue">Enter your password : <input type="password" name = "pwd" ></font><br><br> <input type = "submit" name = "submit" value = "submit" > </form> </center> </body> </html> URLrewriting.jsp <html> <head> <title>Welcome in In the program of URL rewriting</title> </head> <body><center> <font size = 6 color = "red" > Hello <% out.println("Successfull Login");%> </font> </center> </body> </html> URLrewritingMain.jsp <%@ page session ="true" %> <% String name = request.getParameter("name"); String password = request.getParameter("pwd"); if(name.equals("user1") && password.equals("pwd1")) { session.setAttribute("username",name); String string = response.encodeURL("URLrewriting.jsp?name=+name+&password=+password"); %><p><font size = 4>Login successful</font> <font size = 4><a href ='<%= string %>'>Please click here to go forward</a></font> </p> <%} else {
Web Technologies Lab 27 CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

String string = response.encodeURL("URLrewriting.html?name=+name+&password=+password"); %> <p><font size = 4>UserName /Password is Incorrect : </font><font size = 4><a href ='<%= string %>'>Click here to go back</a></font></p> <% }%> Start the tomcat Type as http://localhost:8080/jsp-examples/full/URLrewriting.html in brower

Web Technologies Lab

28

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

Web Technologies Lab

29

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

Week10: Develop a web application using servlet to diaply employee details for the given empid.note:emp(empid,ename,designation,sal)table stored in oracle/ms-access database displayEmp.java import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; // Extend HttpServlet class public class displayEmp extends HttpServlet {public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException { // Create cookies for first and last names. res.setContentType("text/html"); PrintWriter pw = res.getWriter(); String emp = req.getParameter("empid"); try { // System.out.println(fn); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:dsn_oracle","scott","tiger"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM EMP where EMPID="+emp); pw.println("<html><body><table border='1'><tr>"); // pw.println(""); while(rs.next()) { pw.println("<td>"+rs.getInt(1)+"</td>"); pw.println("<td>"+rs.getString(2)+"</td>"); pw.println("<td>"+rs.getString(3)+"</td>"); pw.println("<td>"+rs.getInt(4)+"</td>"); } pw.println("</tr></table></body></html>"); } catch (Exception e) { e.printStackTrace(); } } } Web.xml <web-app> <servlet> <servlet-name>display</servlet-name> <servlet-class>displayEmp</servlet-class> </servlet> <servlet-mapping>
Web Technologies Lab 30 CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

<servlet-name>display</servlet-name> <url-pattern>/readf</url-pattern> </servlet-mapping></web-app> emp.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> </HEAD> <BODY> <html> <body> <form action="readf" method="GET"> Enter Emp ID<input type="text" name="empid"> <input type="submit" value="Submit" /> </form> </BODY> </HTML>

Web Technologies Lab

31

CSE Dept

CHALAPATHI INSTITUTE OF TECHNOLOGY

WEEK 11: Develop a JSP application to insert a new employee record in the above(week 10) emp table saveEmp.jsp <%@ page import="java.sql.*" %> <HTML> <HEAD> <TITLE>Filling a Table</TITLE> </HEAD> <BODY> <H1>Filling a Table</H1> <% Connection con = null; int empid = Integer.parseInt(request.getParameter("empid")); String name = request.getParameter("name"); String desg = request.getParameter("desg"); int sal = Integer.parseInt(request.getParameter("sal")); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con = DriverManager.getConnection("jdbc:odbc:dsn_oracle","scott","tiger"); PreparedStatement ps; ps = con.prepareStatement("insert into emp values(?,?,?,?);"); ps.setInt(1,empid); ps.setString(2,name); ps.setString(3,desg); ps.setInt(4,sal); ps.executeUpdate(); out.println("record inserted"); %> </BODY> </HTML>

Web Technologies Lab

32

CSE Dept

P.N.C & VIJAI INSTITUTE OF ENGINEERING & TECHNOLOGY

ROOL NO: 122B1D58__

Empdet.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> </HEAD> <BODY> <form method="get" action="saveEmp.jsp"> EmpId<input type="text" name="empid"><br> EmpName<input type="text" name="name"><br> EmpDesignation<input type="text" name="desg"><br> EmpSalary<input type="text" name="sal"><br> <input type="submit" value="submit"> <input type="reset" value="reset"> </form> </BODY> </HTML>

Web Technologies Lab

CSE Dept

33

P.N.C & VIJAI INSTITUTE OF ENGINEERING & TECHNOLOGY

ROOL NO: 122B1D58__

Jsp use bean //MyBean.java public class MyBean { // Initialize with random values int prop1 = (int)(Integer.MAX_VALUE*Math.random()); String prop2 = ""+Math.random(); public int getProp1() { return prop1; } public void setProp1(int prop1) { this.prop1 = prop1; } public String getProp2() { return prop2; } public void setProp2(String prop2) { this.prop2 = prop2; } }

Web Technologies Lab

CSE Dept

34

P.N.C & VIJAI INSTITUTE OF ENGINEERING & TECHNOLOGY

ROOL NO: 122B1D58__

//UseBean.jsp <jsp:useBean id="myBean" class="Mybean.MyBean" scope="session" > <jsp:setProperty name="myBean" property="name" value=" James" /> <jsp:setProperty name="myBean" property="address" value=" 007,Gali No.2" /> </jsp:useBean> <%-- <jsp:getProperty name="myBean" property="name" /> <jsp:getProperty name="myBean" property="address" /> --%> The name is<%= myBean.getName()%><br> The address is<%= myBean.getAddress() %>

Web Technologies Lab

CSE Dept

35

P.N.C & VIJAI INSTITUTE OF ENGINEERING & TECHNOLOGY

ROOL NO: 122B1D58__

Week 12: a) Develop a JSP application to load a java bean in JSP page Employees.java package bean; public class Employees { protected String firstName; protected String lastName; protected String address; public void setAddress(String address) { this.address = address; } public void setFirstName(String firstName) { this.firstName = firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getAddress() { return address; } public String getLastName() { return lastName; } public String getFirstName() {return firstName; } } Getproperty.jsp <html> <body> <h1>Get Value from bean</h1> <jsp:useBean id="emp" class="bean.Employees" scope="page" /> <jsp:setProperty name="emp" property="firstName" value="kalyani"/> <jsp:setProperty name="emp" property="lastName" value="rama"/> <jsp:setProperty name="emp" property="address" value="Guntur"/> <table> <tr> <td>First Name</td> <td> : </td> <td><jsp:getProperty name="emp" property="firstName"/></td></tr> <tr> <td>Last Name</td> <td> : </td> <td><jsp:getProperty name="emp" property="lastName"/></td>
Web Technologies Lab CSE Dept 36

P.N.C & VIJAI INSTITUTE OF ENGINEERING & TECHNOLOGY

ROOL NO: 122B1D58__

</tr> <tr> <td>Address</td> <td> : </td> <td><jsp:getProperty name="emp" property="address"/></td> </tr> </table> </body> </html>

Web Technologies Lab

CSE Dept

37

S-ar putea să vă placă și