title database handling in jsp using jdbc - kavediasirkavediasir.yolasite.com/resources/experiment...

34
Experiment - 10 Title Database Handling in JSP using JDBC Aim : Write a JSP program for Demonstration of connecting to database using JDBC Appartus Theory : GlassFish Server GlassFish is an open-source application server project started by Sun Microsystems for the Java EE platform and now sponsored by Oracle Corporation. The supported version is called Oracle GlassFish Server. GlassFish is free software, dual-licensed under two free software licences: the Common Development and Distribution License (CDDL) and the GNU General Public License (GPL) with the classpath exception. GlassFish is the reference implementation of Java EE and as such supports Enterprise JavaBeans, JPA, JavaServer Faces, JMS, RMI, JavaServer Pages, servlets, etc. This allows developers to create enterprise applications that are portable and scalable, and that integrate with legacy technologies. Optional components can also be installed for additional services. Built on a modular kernel powered by OSGi, GlassFish runs straight on top of the Apache Felix implementation. It also runs with Equinox OSGi or Knopflerfish OSGi runtimes. HK2 abstracts the OSGi module system to provide components, which can also be viewed as services. Such services can be discovered and injected at runtime. GlassFish is based on source code released by Sun and Oracle Corporation's TopLink persistence system. It uses a derivative of Apache Tomcat as the servlet container for serving Web content,

Upload: nguyenanh

Post on 29-May-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Experiment - 10

Title Database Handling in JSP using JDBC

Aim : Write a JSP program for Demonstration of connecting to database usingJDBC

Appartus

Theory :GlassFish Server

GlassFish is an open-source application server project started by Sun Microsystems for the Java EE platform and now sponsored by Oracle Corporation. The supported version is called Oracle GlassFish Server. GlassFish is free software, dual-licensed under two free software licences: the Common Development and Distribution License (CDDL) and the GNU General Public License (GPL) with the classpath exception.GlassFish is the reference implementation of Java EE and as such supports Enterprise JavaBeans, JPA, JavaServer Faces, JMS, RMI, JavaServer Pages, servlets, etc. This allows developers to create enterprise applications that are portable and scalable, and that integrate with legacy technologies. Optional components can also be installed for additional services.Built on a modular kernel powered by OSGi, GlassFish runs straight on top of the Apache Felix implementation. It also runs with Equinox OSGi or Knopflerfish OSGi runtimes. HK2 abstracts the OSGi module system to provide components, which can also be viewed as services. Such services can be discovered and injected at runtime.GlassFish is based on source code released by Sun and Oracle Corporation's TopLink persistence system. It uses a derivative of Apache Tomcat as the servlet container for serving Web content, with an added component called Grizzly which uses Java New I/O (NIO) for scalability and speed.

Tomcat Server

Apache Tomcat is an open-source web server and servlet container developed by the Apache Software Foundation (ASF). Tomcat implements several Java EE specifications including Java Servlet, JavaServer Pages (JSP),

Java EL, and WebSocket, and provides a "pure Java" HTTP web server environment for Java code to run in.Apache is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation, released under the Apache License 2.0 license, and is open-source software.

Catalina

Catalina is Tomcat's servlet container. Catalina implements Sun Microsystems' specifications for servlet and JavaServer Pages (JSP). In Tomcat, a Realm element represents a "database" of usernames, passwords, and roles (similar to Unix groups) assigned to those users. Different implementations of Realm allow Catalina to be integrated into environments where such authentication information is already being created and maintained, and then use that information to implement Container Managed Security as described in the Servlet Specification.Coyote

Coyote is a Connector component for Tomcat that supports the HTTP 1.1 protocol as a web server. This allows Catalina, nominally a Java Servlet or JSP container, to also act as a plain web server that serves local files as HTTP documents.Coyote listens for incoming connections to the server on a specific TCP port and forwards the request to the Tomcat Engine to process the request and send back a response to the requesting client. Another Coyote Connector, Coyote JK, listens similarly but instead forwards its requests to another web server, such as Apache, using the JK protocol.[4] This usually offers better performance.Jasper

Jasper is Tomcat's JSP Engine. Jasper parses JSP files to compile them into Java code as servlets (that can be handled by Catalina). At runtime, Jasper detects changes to JSP files and recompiles them.As of version 5, Tomcat uses Jasper 2, which is an implementation of the Sun Microsystems's JSP 2.0 specification. From Jasper to Jasper 2, important features were added: JSP Tag library pooling - Each tag markup in JSP file is handled by a tag handler class. Tag handler class objects can be pooled and reused in the whole JSP servlet.

Background JSP compilation - While recompiling modified JSP Java code, the older version is still available for server requests. The older JSP servlet is deleted once the new JSP servlet has finished being recompiled. Recompile JSP when included page changes - Pages can be inserted and included into a JSP at runtime. The JSP will not only be recompiled with JSP file changes but also with included page changes. JDT Java compiler - Jasper 2 can use the Eclipse JDT (Java Development Tools) Java compiler instead of Ant and javac.

Three new components were added with the release of Tomcat 7:Cluster

This component has been added to manage large applications. It is used for load balancing that can be achieved through many techniques. Clustering support currently requires the JDK version 1.5 or later.High availability

A high-availability feature has been added to facilitate the scheduling of system upgrades (e.g. new releases, change requests) without affecting the live environment. This is done by dispatching live traffic requests to a temporary server on a different port while the main server is upgraded on the main port. It is very useful in handling user requests on high-traffic web applications.Web Application

It has also added user- as well as system-based web applications enhancement to add support for deployment across the variety of environments. It also tries to manage sessions as well as applications across the network.Tomcat is building additional components. A number of additional components may be used with Apache Tomcat. These components may be built by users should they need them or they can be downloaded from one of the mirrors.

JNDI (Java Naming and Directory Interface)

JNDI (Java Naming and Directory Interface) enables Javaplatform-based applications to access multiple naming and directory services. Part of the Java Enterprise application programming interface (API) set, JNDI makes it possible for developers to create portable applications that are enabled for a number of different naming and directory services, including: file systems; directory services such as

Lightweight Directory Access Protocol (LDAP),  Novell Directory Services, and Network Information System (NIS);

Also distributed object systems such as the Common Object Request Broker Architecture (CORBA), Java Remote Method Invocation (RMI), and Enterprise JavaBeans (EJB).

Step to Create JSP DataBase Application

Step 1: Open Netbeans 8.0.2

Step 2: Create a New Project

Step 3: Select a Web Appication

Step 4: Choose your project name.

Step 5:-Set “GlassFish ”as Server & Click on “Next” Button.

Step 6:-Click on “Finish” Button.

Step 7: Right Click on the WEB-INF Folder and select New JSP File

Step 8: After Selecting JSP File –A “new JSP “ wizard appears.Click on Finish to continue

Step 9: Click on Services. Right Click on Databases.Select New Connection

Step 10: You will see New Connection Wizard. Click on Driver Drop Menu. Select MySQL(Connector/J driver). Then,click Next button.

Step 11: You will have to enter password.

Step 12:Right click on it and Select Start if it not started. Then Select Create Database.

Step 13: you will see Create MySQL Database. Give Database Name same like here I have given(new1)

Then,Click OK button.

Step 14: You will see database inside MySQL Server. Right Click on it and Click Connect.

If you find this line under Drivers folder then you have connected.

Step 15: You also can see your database(new1)inside here.Right Click on Tables folder.

Click Creatae Table.

Step 16: You will see Create Table wizard. Give table name same as here it is given(new1).

Click on AddColumn button. Now you will see Add Column wizard.

Click the name of column you want to add.

Step 17: You can see created table “name” here. Also can see the columns table.

Now Rigt Click on “name” and Select “View Data.”

Step 18: Then the following window will appear.

Step 19: Right Click on where records are seen.

Then Click on “Insert Record(s)”1

Step 20: You can see the empty database.Now enter the name you want to add in that column.

Step 21: Go to Souce CodeFolder and Write the code.

Output

Code for DataBase in JSPDynamic.JSP

<html>

<head>

</head>

<body>

<h1 align="center">Database</h1><hr>

<form name="f1" method="post" action="tp.jsp">

<table width=50%>

<tr><td> Enter name to Insert</td><td><input type="text" name="t1" value="fullname" onfocus="value=''"></td></tr>

<br> <tr><td>Update </td><td><input type="text" name="t2" value="what" onfocus="value=''"> <input type="text" name="t3" value="with what" onfocus="value=''"></td></tr>

<br><tr><td>Delete</td><td><input type="text" name="t4" value="what" onfocus="value=''"></td></tr>

<br><tr><td> Search</td><td><input type="text" name="t5" value="name initial" onfocus="value=''"></td></tr>

</table>

<br><td><input type="submit" name="b1" value="Insert" >

<input type="submit" name="b1" value="Update" >

<input type="submit" name="b1" value="Delete" >

<input type="submit" name="b1" value="Search" >

<input type="submit" name="b1" value="Show Table" >

</form>

</body>

</html>

Insert.jsp

<html>

<head>

</head>

<body>

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

<%

try{

Class.forName("com.mysql.jdbc.Driver");

}

catch (Exception c){c.printStackTrace();}

Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/insert","root","root");

Statement st=conn.createStatement();

Statement st2=conn.createStatement();

ResultSet rs=null;ResultSet rs2=null;ResultSet rs3=null;

PreparedStatement insert=null;PreparedStatement update=null;

try{

insert=conn.prepareStatement("insert into name values('yash nikam')");

update=conn.prepareStatement("update name set fullname='noob' where fullname like 'yash nikam'");

rs=st.executeQuery("select * from name");

insert.executeUpdate();

update.executeUpdate();

}catch(SQLException e){e.printStackTrace();}

out.println("Before Insertion");

while(rs.next())

{

%><br><%=rs.getString("fullname")%><br><%

}

out.println("<br>After Insertion & updation");

rs2=st.executeQuery("select * from name");

while(rs2.next())

{

%><br><%=rs2.getString("fullname")%><br><%

}

out.println("<br>Searching name a letter as second character");

rs3=st2.executeQuery("select * from name where fullname like '_a%'");

while(rs3.next())

{

%><br><%=rs3.getString("fullname")%><br><%

}

rs.close();rs2.close();rs3.close();

insert.close();update.close();

conn.close();%>

</body>

</html>

tp.jsp

<html>

<head>

<meta http-equiv="refresh" content="3;URL='dynamic.jsp'">

</head>

<body>

<%String insert1=request.getParameter("t1");

String what=request.getParameter("t2");

String withwhat=request.getParameter("t3");

String delete1=request.getParameter("t4");

String search=request.getParameter("t5");

String button=request.getParameter("b1");

%>

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

<%

try{

Class.forName("com.mysql.jdbc.Driver");

}

catch (Exception c){c.printStackTrace();}

Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/new","root","root");

Statement st=conn.createStatement();

Statement st2=conn.createStatement();

ResultSet rs=null;ResultSet rs2=null;

PreparedStatement insert=null;PreparedStatement update=null;

PreparedStatement delete=null;

try{

if(button.equals("Insert"))

{

insert=conn.prepareStatement("insert into name values('"+insert1+"')");

insert.executeUpdate();

out.println("Value inserted please wait for 3 Seconds.....");

}

else if(button.equals("Update"))

{

update=conn.prepareStatement("update name set fullname='"+withwhat+"' where fullname like '"+what+"'");

update.executeUpdate();

out.println("Value Updated please wait for 3 Seconds.....");

}

else if(button.equals("Delete"))

{

delete=conn.prepareStatement("delete from name where fullname like '"+delete1+"'");

delete.executeUpdate();

out.println("Value Deleted please wait for 3 seconds.....");}

else if(button.equals("Search"))

{

rs2=st2.executeQuery("select * from name where fullname like '"+search+"%'");

while(rs2.next())

{

%><br><%=rs2.getString("fullname")%><br><%

}

}

else if(button.equals("Show Table"))

{

rs=st.executeQuery("select * from name");

while(rs.next())

{

%><br><%=rs.getString("fullname")%><br><%

}

}

}catch(SQLException e){e.printStackTrace();}

%>

</body>

</html>

Starting JSP Page

Note : Write new Code on New Page

Also Student tomorrow you have give one topic individual for JSP miniProject for exeriement no 9Which u have submit by next Wednesday or Thursday latestProject should include

1.Homepage with introduction and Image2.Front side validated (Name , mobile no , email address and other validation using javascript)JSP registration Page register data should go in database3.Contact us page with information of Yours to contact4. Email page , were email will be stored in database5. One page include information of Browser and Server6. one page to view all email in the data base in table form7.Every page should display Menu as on other website 8.Ur Name and Todays date on every page9. Login page were correct login will help you to see email if ur Admin or else appropriate message10.Some space for AdvertisementHappy Project

U can give topic by tomorrow ,work irrespective of Topic u can start Immedaitely