scvagadiya.files.wordpress.com€¦  · web viewpractical:-12. aim: - create application with java...

38
ADVANCE JAVA TECHNOLOGY (170703) PRACTICAL:-12 AIM: - Create Application With Java Servlet which Contain 1.HTML Form 2. Database Connectivity(Insert,Update Delete,Select) 3. Cookie and Session 4. Also check with Forward,Include,Redirect,URL Rewriting. Database Connectiviy Insert into the Database import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import java.sql.*; public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { out.write("\n"); out.write("\n"); out.write("\n"); out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"); out.write(" \"http://www.w3.org/TR/html4/loose.dtd\">\ n"); out.write("\n"); out.write("\n"); out.write("\n"); out.write("<html>\n"); out.write(" <head>\n"); out.write(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"); out.write(" <title>JSP Page</title>\n"); VIMAT/BE/CE/SEM-7/120940107018 Page

Upload: others

Post on 25-Sep-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

PRACTICAL:-12

AIM: - Create Application With Java Servlet which Contain

1. HTML Form2. Database Connectivity(Insert,Update Delete,Select)3. Cookie and Session4. Also check with Forward,Include,Redirect,URL Rewriting.

Database Connectiviy

Insert into the Database

import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;import java.sql.*;public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { out.write("\n"); out.write("\n"); out.write("\n"); out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"); out.write(" \"http://www.w3.org/TR/html4/loose.dtd\">\n"); out.write("\n"); out.write("\n"); out.write("\n"); out.write("<html>\n"); out.write(" <head>\n"); out.write(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"); out.write(" <title>JSP Page</title>\n"); out.write(" </head>\n"); out.write(" <body>\n"); out.write(" <h1>Hello World!</h1>\n"); out.write(" ");

Statement st=null; String name = request.getParameter("name"); String add1 = request.getParameter("add1"); response.encodeURL("http://localhost:8080/jsp_servlet_kirti/jsp3.jsp?name=+name+&password=+password");Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:Driver={Microsoft Access Driver " + "(*.mdb)};DBQ=D:\\db1.mdb"; Connection con = DriverManager.getConnection(url);

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 2: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

System.out.println("Connected!"); st=con.createStatement(); String qry="insert into t1 values(' " + name + " ',' " + add1 + " ')"; st.execute(qry);

System.out.println("Inserted!"); st.close(); con.close();

out.write("\n"); out.write(" </body>\n"); out.write("</html>\n"); }

Select From Database

import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;import java.sql.*;public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException {out.write("\n"); out.write("\n"); out.write("\n"); out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"); out.write(" \"http://www.w3.org/TR/html4/loose.dtd\">\n"); out.write("\n"); out.write("\n"); out.write("\n"); out.write("<html>\n"); out.write(" <head>\n"); out.write(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"); out.write(" <title>JSP Page</title>\n"); out.write(" </head>\n"); out.write(" <body>\n"); out.write(" <h1>Hello World!</h1>\n"); out.write(" ");

Statement st=null; String name = request.getParameter("name"); String add1 = request.getParameter("add1"); response.encodeURL("http://localhost:8080/jsp_servlet_kirti/jsp3.jsp?name=+name+&password=+password");Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:Driver={Microsoft Access Driver " + "(*.mdb)};DBQ=D:\\db1.mdb";

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 3: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

Connection con = DriverManager.getConnection(url); System.out.println("Connected!"); st=con.createStatement(); String qry="Select * from Table1"; rs=st.executeQuery(qry); while(rs.next()) { s2=rs.getString("Name"); System.out.println(s2); } System.out.println("All Selected!");

st.close(); con.close();

out.write("\n"); out.write(" </body>\n"); out.write("</html>\n"); }

Delete from Database

import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;import java.sql.*;public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException {out.write("\n"); out.write("\n"); out.write("\n"); out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"); out.write(" \"http://www.w3.org/TR/html4/loose.dtd\">\n"); out.write("\n"); out.write("\n"); out.write("\n"); out.write("<html>\n"); out.write(" <head>\n"); out.write(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"); out.write(" <title>JSP Page</title>\n"); out.write(" </head>\n"); out.write(" <body>\n"); out.write(" <h1>Hello World!</h1>\n"); out.write(" ");

Statement st=null; String name = request.getParameter("name"); String add1 = request.getParameter("add1");

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 4: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

response.encodeURL("http://localhost:8080/jsp_servlet_kirti/jsp3.jsp?name=+name+&password=+password");Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:Driver={Microsoft Access Driver " + "(*.mdb)};DBQ=D:\\db1.mdb"; Connection con = DriverManager.getConnection(url); System.out.println("Connected!"); st=con.createStatement();String qry="delete from Table1 where name=' " + name + " '"; st.execute(qry);

System.out.println("Deleted!"); st.close(); con.close();

out.write("\n"); out.write(" </body>\n"); out.write("</html>\n"); }

Update in to database

import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;import java.sql.*;public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException {out.write("\n"); out.write("\n"); out.write("\n"); out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n"); out.write(" \"http://www.w3.org/TR/html4/loose.dtd\">\n"); out.write("\n"); out.write("\n"); out.write("\n"); out.write("<html>\n"); out.write(" <head>\n"); out.write(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"); out.write(" <title>JSP Page</title>\n"); out.write(" </head>\n"); out.write(" <body>\n"); out.write(" <h1>Hello World!</h1>\n"); out.write(" ");

Statement st=null; String name = request.getParameter("name"); String add1 = request.getParameter("add1");

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 5: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

response.encodeURL("http://localhost:8080/jsp_servlet_kirti/jsp3.jsp?name=+name+&password=+password");Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:Driver={Microsoft Access Driver " + "(*.mdb)};DBQ=D:\\db1.mdb"; Connection con = DriverManager.getConnection(url); System.out.println("Connected!"); st=con.createStatement(); String qry="Update Table1 set name= ' " + newname + " ' where name=' " + name + " '"; st.execute(qry);

System.out.println("Inserted!"); st.close(); con.close();

out.write("\n"); out.write(" </body>\n"); out.write("</html>\n"); }

Cookie

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;

public class UseCookies extends HttpServlet {   public void doGet ( HttpServletRequest request,    HttpServletResponse response )throws ServletException, IOException {  PrintWriter out;  response.setContentType("text/html");  out = response.getWriter();  Cookie cookie = new Cookie("CName","Cookie Value");  cookie.setMaxAge(100);  response.addCookie(cookie);    out.println("<HTML><HEAD><TITLE>");  out.println(" Use of cookie in servlet");  out.println("</TITLE></HEAD><BODY BGCOLOR='cyan'>");  out.println(" <b>This is a Cookie example</b>");  out.println("</BODY></HTML>");  out.close();  }}

Session

HttpSession session = request.getSession(true);

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 6: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import java.net.*;import java.util.*;

public class ShowSession extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(true); response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Searching the Web"; String heading; Integer accessCount = new Integer(0);; if (session.isNew()) { heading = "Welcome, Newcomer"; } else { heading = "Welcome Back"; Integer oldAccessCount = // Use getAttribute, not getValue, in version // 2.2 of servlet API. (Integer)session.getValue("accessCount"); if (oldAccessCount != null) { accessCount = new Integer(oldAccessCount.intValue() + 1); } } // Use putAttribute in version 2.2 of servlet API. session.putValue("accessCount", accessCount); out.println(ServletUtilities.headWithTitle(title) + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=\"CENTER\">" + heading + "</H1>\n" + "<H2>Information on Your Session:</H2>\n" + "<TABLE BORDER=1 ALIGN=CENTER>\n" + "<TR BGCOLOR=\"#FFAD00\">\n" + " <TH>Info Type<TH>Value\n" + "<TR>\n" + " <TD>ID\n" + " <TD>" + session.getId() + "\n" + "<TR>\n" + " <TD>Creation Time\n" + " <TD>" + new Date(session.getCreationTime()) + "\n" + "<TR>\n" + " <TD>Time of Last Access\n" + " <TD>" + new Date(session.getLastAccessedTime()) + "\n" + "<TR>\n" +

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 7: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

" <TD>Number of Previous Accesses\n" + " <TD>" + accessCount + "\n" + "</TABLE>\n" + "</BODY></HTML>");

}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}

Forward

String destination = "/resultservlet"; 

RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);rd.forward(request, response);

Redirect

response.sendRedirect("/ServletProject/ValidUser");

URL Rewriting

<HTML><TITLE>ONLINE SHOPPING PORTAL</TITLE><BODY><FORM ACTION ="http://localhost:8080/ankit/servlet/RewriteServletURL" METHOD = POST align=CENTER>Username: <INPUT TYPE = TEXT NAME = "user" align=CENTER><BR><INPUT TYPE = SUBMIT VALUE = "Login" align=CENTER></FORM></BODY></HTML>

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class RewriteServletURL extends HttpServlet{public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{doPost(req, res);}

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 8: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

public void doPost(HttpServletRequest req, HttpServletResponseres) throws ServletException, IOException{/* Retrieve the parameters bound to user, password andlogin from the request object. */String username = req.getParameter("user");PrintWriter pw = res.getWriter();/* Verify the login status */res.setContentType("text/html");pw.println("Hello! <a href=\"http://localhost:8080/ankit/servlet/SecondServlet?uname="+ username + "\"> click here </a>to proceed");}}

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 9: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

PRACTICAL:-13

AIM: - Create Application With Jsp.1. HTML Form2. Database Connectivity(Insert,Update Delete,Select)3. Cookie and Session4. Also check with Forward,Include,Redirect,URL Rewriting.

HTML Form :

<html>

<head>

<title></title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>

<body>

<b>&nbsp;&nbsp;&nbsp; Welcome!!!!!</b><br>

<jsp:include page="jsp_servlet_kirti/jspForm.html"/>

<form action="jsp3.jsp" method="post">

Enter Name &nbsp;&nbsp;&nbsp; :&nbsp;&nbsp; <input type="text" name="name"><br><br>

Enter Address &nbsp;: &nbsp; <input type="text" name="add1"><br><br>

Enter City &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :&nbsp;&nbsp;&nbsp;<input type="text" name="city"><br><br>

<input type="submit" value="OK">

<input type="submit" value="Cancel">

</form>

</body>

</html>

Database Connectivity

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 10: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

Insert Data in to the Database

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

<%

Statement st=null;

String name = request.getParameter("name");

String add1 = request.getParameter("add1");

response.encodeURL("http://localhost:8080/jsp_servlet_kirti/jsp3.jsp?name=+name+&password=+password");

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String url = "jdbc:odbc:Driver={Microsoft Access Driver " +

"(*.mdb)};DBQ=D:\\db1.mdb";

Connection con = DriverManager.getConnection(url);

System.out.println("Connected!");

st=con.createStatement();

String qry="insert into Table1 values(' " + name + " ',' " + add1 + " ')";

st.execute(qry);

System.out.println("Inserted!");

st.close();

con.close();

%>

Select Data From the Database

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

<%

Statement st=null;

String name = request.getParameter("name");

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 11: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

String add1 = request.getParameter("add1");

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String url = "jdbc:odbc:Driver={Microsoft Access Driver " +

"(*.mdb)};DBQ=D:\\db1.mdb";

Connection con = DriverManager.getConnection(url);

System.out.println("Connected!");

st=con.createStatement();

String qry="Select * from Table1";

rs=st.executeQuery(qry);

while(rs.next())

{

s2=rs.getString("Name");

System.out.println(s2);

}

System.out.println("All Selected!");

st.close();

con.close();

st.close();

con.close();

%>

Delete From the Databse

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

<%

Statement st=null;

String name = request.getParameter("name");

String add1 = request.getParameter("add1");

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 12: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

response.encodeURL("http://localhost:8080/jsp_servlet_kirti/jsp3.jsp?name=+name+&password=+password");

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String url = "jdbc:odbc:Driver={Microsoft Access Driver " +

"(*.mdb)};DBQ=D:\\db1.mdb";

Connection con = DriverManager.getConnection(url);

System.out.println("Connected!");

st=con.createStatement();

String qry="delete from Table1 where name=' " + name + " '";

st.execute(qry);

System.out.println("Deleted!");

st.close();

con.close();

%>

Update the Databse

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

<%

Statement st=null;

String newname = request.getParameter("newname");

String name = request.getParameter("name");

String add1 = request.getParameter("add1");

response.encodeURL("http://localhost:8080/jsp_servlet_kirti/jsp3.jsp?name=+name+&password=+password");

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

String url = "jdbc:odbc:Driver={Microsoft Access Driver " +

"(*.mdb)};DBQ=D:\\db1.mdb";

Connection con = DriverManager.getConnection(url);

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 13: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

System.out.println("Connected!");

st=con.createStatement();

String qry="Update Table1 set name= ' " + newname + " ' where name=' " + name + " '";

st.execute(qry);

System.out.println("Updated!");

st.close();

con.close();

%>

Cookie

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

<%String name=request.getParameter("name");

Cookie cookie = new Cookie ("name",name);cookie.setMaxAge(365 * 24 );response.addCookie(cookie);%>

More than one Cookie

<%@ page language="java" import="java.util.*"%><%String cookieName = "name";Cookie cookies [] = request.getCookies ();Cookie myCookie = null;if (cookies != null){for (int i = 0; i < cookies.length; i++) {if (cookies [i].getName().equals (cookieName)){myCookie = cookies[i];break;}}}

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 14: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

%><html><head><title>Show Saved Cookie</title></head><body><%if (myCookie == null) {%>No Cookie found with the name <%=cookieName%><%} else {%> <p>Welcome: <%=myCookie.getValue()%>.<%}%></body>

Session

<%@ page language="java" session="true"%>

<%   String name = request.getParameter( "name" );   session.setAttribute( "Name", name );%><HTML><BODY>Hello, <%= session.getAttribute( "Name" ) %></BODY></HTML>

Forward

<jsp:forward page="jsp2.jsp"/>

Include

<jsp:include page="news/Item1.html" flush="true"/>

Redirect

<%

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 15: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

String redirectURL = "http://www.roseindia.net/";

response.sendRedirect(redirectURL);

%>

URL Rewriting

String name = request.getParameter("name");

String add1 = request.getParameter("add1");

response.encodeURL("http://localhost:8080/jsp_servlet_kirti/jsp3.jsp?name=+name+&password=+password");

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 16: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

PRACTICAL:-3

Aim:-Create Application with Maximum Two Java Forms.It must

Represent the Connection with Database.(Insert,Update,Delete,Select

all,Select Perticular).with simple Query or with Prepared Statement.

Input:-import java.sql.*;public class myapp extends javax.swing.JFrame {public myapp() { initComponents(); } private void okActionPerformed(java.awt.event.ActionEvent evt) { Statement st=null; String name=Name.getText(); String add1=Add1.getText(); try {Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:Driver={Microsoft Access Driver " + "(*.mdb)};DBQ=C:\\Documents and Settings\\Administrator\\My Documents\\

db1.mdb"; Connection con = DriverManager.getConnection(url); System.out.println("Connected!"); st=con.createStatement(); String qry="insert into Table1 values(' " + name + " ',' " + add1 + " ')"; st.execute(qry); System.out.println("Inserted!"); st.close(); con.close(); } catch (SQLException e) { System.out.println("SQL Exception: "+ e.toString()); } catch (ClassNotFoundException cE) { System.out.println("Class Not Found Exception: "+ cE.toString()); } } private void SELECTActionPerformed(java.awt.event.ActionEvent evt) { Statement st=null; ResultSet rs=null; String s2=null; try {

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 17: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:Driver={Microsoft Access Driver " + "(*.mdb)};DBQ=C:\\Documents and Settings\\Administrator\\My Documents\\

db1.mdb"; Connection con = DriverManager.getConnection(url); System.out.println("Connected!"); st=con.createStatement(); String qry="Select * from Table1"; rs=st.executeQuery(qry); while(rs.next()) { s2=rs.getString("Name"); System.out.println(s2); } System.out.println("All Selected!"); st.close(); con.close(); } catch (SQLException e) { System.out.println("SQL Exception: "+ e.toString()); } catch (ClassNotFoundException cE) { System.out.println("Class Not Found Exception: "+ cE.toString()); } } private void SELECT_ONActionPerformed(java.awt.event.ActionEvent evt) { Statement st=null; ResultSet rs=null; String s2=null; String name=sec_name.getText(); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:Driver={Microsoft Access Driver " + "(*.mdb)};DBQ=C:\\Documents and Settings\\Administrator\\My Documents\\

db1.mdb"; Connection con = DriverManager.getConnection(url); System.out.println("Connected!"); st=con.createStatement(); String qry="Select Add1 from Table1 where Name=' " + name + " '"; rs=st.executeQuery(qry); while(rs.next()) { s2=rs.getString("Add1"); System.out.println(s2);

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 18: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

} sec_add.setText(s2); System.out.println("one Selected!"); st.close(); con.close(); } catch (SQLException e) { System.out.println("SQL Exception: "+ e.toString()); } catch (ClassNotFoundException cE) { System.out.println("Class Not Found Exception: "+ cE.toString()); } } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new myapp().setVisible(true); } } }}

Output:-

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 19: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

PRACTICAL:-14

AIM:-Write Down JSTL Tag Library Examples.

JSTL Tags:-

The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which

encapsulates core functionality common to many JSP applications.

JSTL has support for common, structural tasks such as iteration and conditionals, tags for

manipulating XML documents, internationalization tags, and SQL tags. It also provides a

framework for integrating existing custom tags with JSTL tags.

The JSTL tags can be classified, according to their functions, into following JSTL tag

library groups that can be used when creating a JSP page:

Core Tags

Formatting tags

SQL tags

XML tags

JSTL Functions

1.Core Tags:

The core group of tags are the most frequently used JSTL tags. Following is the syntax to include JSTL Core library in your JSP:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

There are following Core JSTL Tags:

Tag Description

<c:out > Like <%= ... >, but for expressions.

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 20: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

<c:set > Sets the result of an expression evaluation in a 'scope'

<c:remove > Removes a scoped variable (from a particular scope, if specified).

<c:catch> Catches any Throwable that occurs in its body and optionally exposes it.

<c:if> Simple conditional tag which evalutes its body if the supplied condition is true.

<c:choose> Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>

<c:when> Subtag of <choose> that includes its body if its condition evalutes to 'true'.

<c:otherwise > Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to 'false'.

<c:import> Retrieves an absolute or relative URL and exposes its contents to either the page, a String in 'var', or a Reader in 'varReader'.

<c:forEach > The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality .

<c:forTokens> Iterates over tokens, separated by the supplied delimeters.

<c:param> Adds a parameter to a containing 'import' tag's URL.

<c:redirect > Redirects to a new URL.

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 21: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

<c:url> Creates a URL with optional query parameters

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<html>

<head>

<title><c:forEach> Tag Example</title>

</head>

<body>

<c:forEach var="i" begin="1" end="5">

Item <c:out value="${i}"/><p>

</c:forEach>

</body>

</html>

This would produce following result:

Item 1Item 2Item 3Item 4Item 5

2. Formatting tags:The JSTL formatting tags are used to format and display text, the date, the time, and numbers for internationalized Web sites. Following is the syntax to include Formatting library in your JSP:

<%@ taglib prefix="fmt"

uri="http://java.sun.com/jsp/jstl/fmt" %>

Following is the list of Formatting JSTL Tags:

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 22: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

Tag Description

<fmt:formatNumber> To render numerical value with specific precision or format.

<fmt:parseNumber> Parses the string representation of a number, currency, or percentage.

<fmt:formatDate> Formats a date and/or time using the supplied styles and pattern

<fmt:parseDate> Parses the string representation of a date and/or time

<fmt:bundle> Loads a resource bundle to be used by its tag body.

<fmt:setLocale> Stores the given locale in the locale configuration variable.

<fmt:setBundle> Loads a resource bundle and stores it in the named scoped variable or the bundle configuration variable.

<fmt:timeZone> Specifies the time zone for any time formatting or parsing actions nested in its body.

<fmt:setTimeZone> Stores the given time zone in the time zone configuration variable

<fmt:message> To display an internationalized message.

<fmt:requestEncoding> Sets the request character encoding

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<html>

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 23: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

<head>

<title>JSTL fmt:setBundle Tag</title>

</head>

<body>

<fmt:setLocale value="en"/>

<fmt:setBundle basename="com.tutorialspoint.Example" var="lang"/>

<fmt:message key="count.one" bundle="${lang}"/><br/>

<fmt:message key="count.two" bundle="${lang}"/><br/>

<fmt:message key="count.three" bundle="${lang}"/><br/>

</body>

</html>

This would produce following result:

One Two Three

3.SQL tags:The JSTL SQL tag library provides tags for interacting with relational databases (RDBMSs) such as Oracle, mySQL, or Microsoft SQL Server.

Following is the syntax to include JSTL SQL library in your JSP:

<%@ taglib prefix="sql"

uri="http://java.sun.com/jsp/jstl/sql" %>

Following is the list of SQL JSTL Tags:

Tag Description

<sql:setDataSource> Creates a simple DataSource suitable only for prototyping

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 24: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

<sql:query> Executes the SQL query defined in its body or through the sql attribute.

<sql:update> Executes the SQL update defined in its body or through the sql attribute.

<sql:param> Sets a parameter in an SQL statement to the specified value.

<sql:dateParam> Sets a parameter in an SQL statement to the specified java.util.Date value.

<sql:transaction > Provides nested database action elements with a shared Connection, set up to execute all statements as one transaction.

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

<%@ page import="javax.servlet.http.*,javax.servlet.*" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>

<html>

<head>

<title>JSTL sql:param Tag</title>

</head>

<body>

<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost/TEST"

user="root" password="pass123"/>

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 25: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

<c:set var="empId" value="103"/>

<sql:update dataSource="${snapshot}" var="count">

DELETE FROM Employees WHERE Id = ?

<sql:param value="${empId}" />

</sql:update>

<sql:query dataSource="${snapshot}" var="result">

SELECT * from Employees;

</sql:query>

<table border="1" width="100%">

<tr>

<th>Emp ID</th>

<th>First Name</th>

<th>Last Name</th>

<th>Age</th>

</tr>

<c:forEach var="row" items="${result.rows}">

<tr>

<td><c:out value="${row.id}"/></td>

<td><c:out value="${row.first}"/></td>

<td><c:out value="${row.last}"/></td>

<td><c:out value="${row.age}"/></td>

</tr>

</c:forEach>

</table>

</body>

</html>

Now try to access above JSP, which should display the following result:

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 26: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

Emp ID First Name Last Name Age100 Zara Ali 18101 Mahnaz Fatma 25102 Zaid Khan 30

4.XML tags:The JSTL XML tags provide a JSP-centric way of creating and manipulating XML documents. Following is the syntax to include JSTL XML library in your JSP.

The JSTL XML tag library has custom tags for interacting with XML data. This includes parsing XML, transforming XML data, and flow control based on XPath expressions.

<%@ taglib prefix="x"

uri="http://java.sun.com/jsp/jstl/xml" %>

Before you proceed with the examples, you would need to copy following two XML and XPath related libraries into your <Tomcat Installation Directory>\lib:

XercesImpl.jar: Download it fromhttp://www.apache.org/dist/xerces/j/

xalan.jar: Download it from http://xml.apache.org/xalan-j/index.html

Following is the list of XML JSTL Tags:

Tag Description

<x:out> Like <%= ... >, but for XPath expressions.

<x:parse> Use to parse XML data specified either via an attribute or in the tag body.

<x:set > Sets a variable to the value of an XPath expression.

<x:if > Evaluates a test XPath expression and if it is true, it processes its body. If the test condition is false, the body is ignored.

<x:forEach> To loop over nodes in an XML document.

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 27: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

<x:choose> Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>

<x:when > Subtag of <choose> that includes its body if its expression evalutes to 'true'

<x:otherwise > Subtag of <choose> that follows <when> tags and runs only if all of the prior conditions evaluated to 'false'

<x:transform > Applies an XSL transformation on a XML document

<x:param > Use along with the transform tag to set a parameter in the XSLT stylesheet

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

<html>

<head>

<title>JSTL x:choose Tags</title>

</head>

<body>

<h3>Books Info:</h3>

<c:set var="xmltext">

<books>

<book>

<name>Padam History</name>

<author>ZARA</author>

<price>100</price>

</book>

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 28: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

<book>

<name>Great Mistry</name>

<author>NUHA</author>

<price>2000</price>

</book>

</books>

</c:set>

<x:parse xml="${xmltext}" var="output"/>

<x:choose>

<x:when select="$output//book/author = 'ZARA'">

Book is written by ZARA

</x:when>

<x:when select="$output//book/author = 'NUHA'">

Book is written by NUHA

</x:when>

<x:otherwise>

Unknown author.

</x:otherwise>

</x:choose>

</body>

</html>

This would produce following result:

Books Info:

Book is written by ZARA

5.JSTL Functions:

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 29: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

JSTL includes a number of standard functions, most of which are common string manipulation functions. Following is the syntax to include JSTL Functions library in your JSP:

<%@ taglib prefix="fn"

uri="http://java.sun.com/jsp/jstl/functions" %>

Following is the list of JSTL Functions:

Function Description

fn:contains() Tests if an input string contains the specified substring.

fn:containsIgnoreCase() Tests if an input string contains the specified substring in a case insensitive way.

fn:endsWith() Tests if an input string ends with the specified suffix.

fn:escapeXml() Escapes characters that could be interpreted as XML markup.

fn:indexOf() Returns the index withing a string of the first occurrence of a specified substring.

fn:join() Joins all elements of an array into a string.

fn:length() Returns the number of items in a collection, or the number of characters in a string.

fn:replace() Returns a string resulting from replacing in an input string all occurrences with a given string.

fn:split() Splits a string into an array of substrings.

fn:startsWith() Tests if an input string starts with the specified prefix.

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 30: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

fn:substring() Returns a subset of a string.

fn:substringAfter() Returns a subset of a string following a specific substring.

fn:substringBefore() Returns a subset of a string before a specific substring.

fn:toLowerCase() Converts all of the characters of a string to lower case.

fn:toUpperCase() Converts all of the characters of a string to upper case.

fn:trim() Removes white spaces from both ends of a string.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<html>

<head>

<title>Using JSTL Functions</title>

</head>

<body>

<c:set var="string1" value="This is first String."/>

<c:set var="string2" value="This is second String." />

<p>Length of String (1) : ${fn:length(string1)}</p>

<p>Length of String (2) : ${fn:length(string2)}</p>

</body>

</html>

VIMAT/BE/CE/SEM-7/120940107018 Page

Page 31: scvagadiya.files.wordpress.com€¦  · Web viewPRACTICAL:-12. AIM: - Create Application With Java Servlet which Contain. HTML Form. Database Connectivity(Insert,Update Delete,Select)

ADVANCE JAVA TECHNOLOGY (170703)

This would produce following result:

Length of String (1) : 21Length of String (2) : 22

VIMAT/BE/CE/SEM-7/120940107018 Page