JdbcResultSet.java. import java.sql.*;

Similar documents
LAB 1 PREPARED BY : DR. AJUNE WANIS ISMAIL FACULTY OF COMPUTING UNIVERSITI TEKNOLOGI MALAYSIA

Remote Method Invocation in Java

SERVLETS INTERVIEW QUESTIONS

Unit-4: Servlet Sessions:

Web based Applications, Tomcat and Servlets - Lab 3 -

Module 4: SERVLET and JSP

AJP. CHAPTER 5: SERVLET -20 marks

This tutorial will teach you how to use Java Servlets to develop your web based applications in simple and easy steps.

INTRODUCTION TO SERVLETS AND WEB CONTAINERS. Actions in Accord with All the Laws of Nature

INTERNET PROGRAMMING TEST-3 SCHEME OF EVALUATION 1.A 3 LIFE CYCLE METHODS - 3M 1.B HTML FORM CREATION - 2 M

Session 8. Introduction to Servlets. Semester Project

Java servlets CSCI 470: Web Science Keith Vertanen Copyright 2013

Principles and Techniques of DBMS 6 JSP & Servlet

Chettinad College of Engineering and Technology CHETTINAD COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF COMPUTER SCIENCE AND TECHNOLOGY

The Basic Web Server CGI. CGI: Illustration. Web based Applications, Tomcat and Servlets - Lab 3 - CMPUT 391 Database Management Systems 4

HttpServlet ( Class ) -- we will extend this class to handle GET / PUT HTTP requests

CIS 3952 [Part 2] Java Servlets and JSP tutorial

Servlet Basics. Agenda

sessionx Desarrollo de Aplicaciones en Red A few more words about CGI CGI Servlet & JSP José Rafael Rojano Cáceres

Handout 31 Web Design & Development

JAVA SERVLET. Server-side Programming PROGRAMMING

Session 9. Introduction to Servlets. Lecture Objectives

Database Systems Lab. 11. JSP I 충남대학교컴퓨터공학과 데이타베이스시스템연구실

Java Enterprise Edition. Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 1

Advanced Internet Technology Lab # 4 Servlets

Servlets and JSP (Java Server Pages)

Unit 4 - Servlet. Servlet. Advantage of Servlet

Servlets1. What are Servlets? Where are they? Their job. Servlet container. Only Http?

Backend. (Very) Simple server examples

a. Jdbc:ids://localhost:12/conn?dsn=dbsysdsn 21. What is the Type IV Driver URL? a. 22.

UNIT-V. Web Servers: Tomcat Server Installation:

Advanced Internet Technology Lab # 6

Advanced Web Technology

Servlet. Web Server. Servlets are modules of Java code that run in web server. Internet Explorer. Servlet. Fire Fox. Servlet.

Enterprise Java Unit 1- Chapter 4 Prof. Sujata Rizal Servlet API and Lifecycle

PARTIAL Final Exam Reference Packet

PSD1B Advance Java Programming Unit : I-V. PSD1B- Advance Java Programming

********************************************************************

Servlet Fudamentals. Celsina Bignoli

CE212 Web Application Programming Part 3

PES INSTITUTE OF TECHNOLOGY, SOUTH CAMPUS DEPARTMENT OF MCA INTERNAL TEST (SCHEME AND SOLUTION) II

UNIT -5. Java Server Page

To follow the Deitel publishing program, sign-up now for the DEITEL BUZZ ON-

Chapter 10 Servlets and Java Server Pages

Unit III- Server Side Technologies

JAVA SERVLET. Server-side Programming ADVANCED FEATURES

Getting started with Winstone. Minimal servlet container

Java4570: Session Tracking using Cookies *

JavaServer Pages (JSP)

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

Servlets by Example. Joe Howse 7 June 2011

&' () - #-& -#-!& 2 - % (3" 3 !!! + #%!%,)& ! "# * +,

Advanced Java Programming

Kamnoetvidya Science Academy. Object Oriented Programming using Java. Ferdin Joe John Joseph. Java Session

JAVA SERVLET. Server-side Programming INTRODUCTION

EXPERIMENT- 9. Login.html

Using Java servlets to generate dynamic WAP content

Unit 4 Java Server Pages

To create a view for students, staffs and courses in your departments using servlet/jsp.

Web Technology for IE 20 November ISE 582: Information Technology for Industrial Engineering

Introduction. This course Software Architecture with Java will discuss the following topics:

Java Database Connectivity (JDBC) 25.1 What is JDBC?

2. Follow the installation directions and install the server on ccc. 3. We will call the root of your installation as $TOMCAT_DIR

Introduction to Java Servlets. SWE 432 Design and Implementation of Software for the Web

Java Servlets. Preparing your System

Enterprise Java Technologies (Part 1 of 3) Component Architecture. Overview of Java EE. Java Servlets

Session 10. Form Dataset. Lecture Objectives

Servlet and JSP Review

CS506 Web Design & Development Final Term Solved MCQs with Reference

Introduction. Literature: Steelman & Murach, Murach s Java Servlets and JSP. Mike Murach & Associates Inc, 2003

SSC - Web applications and development Introduction and Java Servlet (I)

Get the cookies from the service request: Cookie[] HttpServletRequest.getCookies() Add a cookie to the service response:

A Servlet-Based Search Engine. Introduction

Servlets. An extension of a web server runs inside a servlet container

Session 8. JavaBeans. Reading & Reference. Reading. Reference. Session 8 Java Beans. 2/27/2013 Robert Kelly, Head First Chapter 3 (MVC)

Stateless -Session Bean

Lab session Google Application Engine - GAE. Navid Nikaein

Servlet 5.1 JDBC 5.2 JDBC

CREATE A SERVLET PROGRAM TO DISPLAY THE STUDENTS MARKS. To create a servlet program to display the students marks

ICOM 5016 Database Systems. Database Users. User Interfaces and Tools. Chapter 8: Application Design and Development.

Java TM. JavaServer Faces. Jaroslav Porubän 2008

Advanced Topics in Operating Systems. Manual for Lab Practices. Enterprise JavaBeans

Databases and JDBC. by Vlad Costel Ungureanu for Learn Stuff

Watch Core Java and Advanced Java Demo Video Here:

ProgramvaruUtveckling för Stora System F2

SNS COLLEGE OF ENGINEERING, Coimbatore

CreateServlet.java

Develop an Enterprise Java Bean for Banking Operations

UNIT-VI. HttpServletResponse It extends the ServletResponse interface to provide HTTP-specific functionality in sending a response.

Unit III- Server Side Technologies

Handling Cookies. Agenda

Handling Cookies. For live Java EE training, please see training courses at

Welcome To PhillyJUG. 6:30-7:00 pm - Network, eat, find a seat 7:00-7:15 pm - Brief announcements 7:15-8:30 pm - Tom Janofsky's presentation

Questions and Answers

Introduction to Servlets. After which you will doget it

Allenhouse Institute of Technology (UPTU Code : 505) OOT Notes By Hammad Lari for B.Tech CSE V th Sem

Unit 5 JSP (Java Server Pages)

COMP9321 Web Application Engineering

Complimentary material for the book Software Engineering in the Agile World

Université Antonine - Baabda

Transcription:

1)Write a program to display the current contents of the tables in the database where table name is Registration and attributes are id,firstname,lastname,age. JdbcResultSet.java import java.sql.*; public class jdbcresultset { public static void main(string[] args) { try { Class.forName("com.mysql.jdbc.Driver"); catch(classnotfoundexception e) { System.out.println("Class not found "+ e); try { Connection con = DriverManager.getConnection( "jdbc:mysql://localhost/students","root", "srinath"); CallableStatement proc_stmt=con.preparecall("{call p1( )"); ResultSet rs = proc_stmt.executequery(); System.out.println("id first last age"); while (rs.next()) { int id = rs.getint("id"); String first = rs.getstring("first"); String last = rs.getstring("last"); int age= rs.getint("age"); System.out.println(id+""+first+""+last+""+age); catch(sqlexception e) { System.out.println("SQL exception occured" + e); 2)Describe the simple Html file to pass parameter to servlet and display the paramter values accepted by servlet. <html> <head> <title>login Form</title> </head> <body> <h2>login Page</h2> <p>please enter your username and password</p> <formmethod="get"action="loginservlet">

<p> Username <inputtype="text"name="username"size="50"> </p> <p> Password <inputtype="text"name="password"size="20"> </p> <p> <inputtype="submit"value="submit"name="b1"> </p> </form> <p> </p> </body> </html> login.java: package com.jwt.servlet; importjava.io.ioexception; importjava.io.printwriter; importjavax.servlet.servletexception; importjavax.servlet.http.httpservlet; importjavax.servlet.http.httpservletrequest; importjavax.servlet.http.httpservletresponse; publicclassloginservletextends HttpServlet { publicvoiddoget(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { response.setcontenttype("text/html"); PrintWriter out = response.getwriter(); String username = request.getparameter("username"); String password = request.getparameter("password"); out.println("<html>"); out.println("<body>"); out.println("hello "+""+ username +"welcome to my blog");

out.println("your password is : "+""+ password +"<br>"); out.println("</body></html>"); 3)What is a cookie?list out the methods defined by cookie.write a program to add a cookie called MVS and has a value of 5366 and display using servlets. Cookies are small files which are stored on a user's computer. They are designed to hold a modest amount of data specific to a particular client and website, and can be accessed either by the web server or the client computer. This allows the server to deliver a page tailored to a particular user, or the page itself can contain some script which is aware of the data in the cookie and so is able to carry information from one visit to the website to the next. Methods: getdomain() Returns the name of the server making the request. Public string getdomain() getmaxage() Returns a number representing how long the cookie is valid for, in seconds. If set to < 0, a session cookie is issued. If set to 0, the cookie is deleted. Ppublic integer getmaxage() getname() Returns the name of the cookie. Can't be null. getpath() Returns the path from which you can retrieve the cookie. If null or blank, the location is set to root, or /. getvalue() Returns the data captured in the cookie, such as Session ID. issecure() Returns true if the cookie can only be accessed through HTTPS, otherwise returns false. Program: <html> <head> <title>jsp program</title> </head> <body> <%! string mycookie = MVS ; string cnam,cval; int find=0;

Cookie[ ] Cookie=request.getcookie(); for(int i<0;i<cookie.length;i++) { cnam=cookie.s[i].getname(); cval=cookie[i].getvalue(); if(my Cookie.equals(Cookie Names[i]) find=i; MyCookie val=5366; if(find==1) { %> <p>cookie name=<%=mycookie%></p> <p> Cookie value=<%=mycookie val%></p> <%+%></body></html> 4)Define JSP.Explain Different types of JSP tags by taking suitable example. JSP: JavaServer Pages (JSP) is a technology that helps software developers create dynamically generated web pages based on HTML, XML, or other document types. Released in 1999 by Sun Microsystems, JSP is similar to PHP and ASP, but it uses the Java programming language. JSP Tags: Comment tag: opens with <%-- and closes with --%> and is followed by a comment that usually describes the functionality of statements that follow the comment tag. Declaration statement tag: opens with <%! and is followed by a Java declaration statement(s) that define variables, objects and methods that are available to other components of the JSP program. Directive tag: opens with a <%@ and commands the JSP virtual engine to perform a specific task, such as importing a Java package required by objects and methods used in a declaration statement. The directive tag closes with %>. There are three commonly used directives: import, include and taglib. The import tag is used to import Java packages into the JSP program. The include tag inserts a specified file into the JSP program replacing the include tag. The taglib tag specifies a file that contains a tag library. Example: <%@ page import= import java.sql.* ; %> <%@ include file= keogh\books.html %>

<%@ taglib url= mytags.tld %> The first tag imports the java.sql package. The next tag includes the books.html file located in the keogh directory. The last tag loads the mytags.tld library. Expression tag: opens with <%= and is used for an expression statement whose result replaces the expression tag when the JSP virtual engine resolves JSP tags. An expression tag closes with %>. Scriptlet tag: A scriptlet tag opens with <% and contains commonly used Java control statements and loops. A scriptlet tag closes with %>. <HTML> <HEAD> <TITLE> JSP Programming </TITLE> </HEAD> <BODY> <%! boolean curve (int grade) { return 10 + grade; boolean curve (int grade, int curvevalue) { return curvevalue + grade; %> <P> Your curved grade is: <%=curve(80, 10)%></P> % 5)What is RMI?Describe the code snippet TMI at server and client for adding two numbers. RMI:Remote Method Invocation (RMI) facilitates object function calls between Java Virtual Machines (JVMs). JVMs can be located on separate computers - yet one JVM can invoke methods belonging to an object stored in another JVM. Methods can even pass objects that a foreign virtual machine has never encountered before, allowing dynamic loading of new classes as required. RMI is used for remote communication between Java applications and components, both of which must be written in Java. RMI is used to connect together a client and a server. MyClient.java import java.rmi.*; public class MyClient{ public static void main(string args[]){ try{ Adder stub=(adder)naming.lookup("//localhost:5000/rmi"); System.out.println(stub.add(34,4));

catch(exception e){ MyServer.java import java.rmi.*; import java.rmi.registry.*; public class MyServer{ public static void main(string args[]){ try{ Adder stub=new AdderRemote(); Naming.rebind("//localhost:5000/rmi",stub); catch(exception e){system.out.println(e); AdderRemote.java import java.rmi.*; import java.rmi.server.*; public class AdderRemote extends UnicastRemoteObject implements Adder{ AdderRemote()throws RemoteException{ super(); public int add(int x,int y){return x+y; 6)What is a servlet? What are the phases of servlet life cycle? Give an Example. Servlets are modules of Java code that run in a server application (hence the name "Servlets", similar to "Applets" on the client side) to answer client requests. Servlets are not tied to a specific client-server protocol but they are most commonly used with HTTP and the word "Servlet" is often used in the meaning of "HTTP Servlet". Phases of Servlet Life Cycle: A servlet life cycle can be defined as the entire process from its creation till the destruction. The following are the paths followed by a servlet: The servlet is initialized by calling the init () method. The servlet calls service() method to process a client's request.

The servlet is terminated by calling the destroy() method. Finally, servlet is garbage collected by the garbage collector of the JVM. The init() method : The init method is designed to be called only once. It is called when the servlet is first created, and not called again for each user request. So, it is used for one-time initializations, just as with the init method of applets. The servlet is normally created when a user first invokes a URL corresponding to the servlet, but you can also specify that the servlet be loaded when the server is first started. When a user invokes a servlet, a single instance of each servlet gets created, with each user request resulting in a new thread that is handed off to doget or dopost as appropriate. The init() method simply creates or loads some data that will be used throughout the life of the servlet. The init method definition looks like this: public void init() throws ServletException { // Initialization code... The service() method : The service() method is the main method to perform the actual task. The servlet container (i.e. web server) calls the service() method to handle requests coming from the client( browsers) and to write the formatted response back to the client. Each time the server receives a request for a servlet, the server spawns a new thread and calls service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doget, dopost, doput, dodelete, etc. methods as appropriate. Here is the signature of this method: public void service(servletrequest request, ServletResponse response) throws ServletException, IOException{ The service () method is called by the container and service method invokes doge, dopost, doput, dodelete, etc. methods as appropriate. So you have nothing to do with service() method but you override either doget() or dopost() depending on what type of request you receive from the client. The doget() and dopost() are most frequently used methods with in each service request. public void doget(httpservletrequest request, HttpServletResponse

response) throws ServletException, IOException { // Servlet code public void dopost(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { // Servlet code destroy() method The destroy() method is the last method to be called right before the Java servlet terminates, such as when an instance of a Java sevlet is removed from memory. You can override the destroy() method with statements that release resources, such as closing a database connection. Example: Html: <% int z=1; %> <form method="post" action="servlet"> <input type="text" name="username" /> <input type="password" name="password" /> <input type="hidden" name="product_no" value="<%=z%>" /> <input type='submit' /> </form> Java: protected void dopost(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { String username= request.getparameter("username"); String password= request.getparameter("password"); 7a)Write a java program to display the meta data of a Database such as database product name,database product version,driver name,total number of tables,total number of views. 7b)Write a Java program to get the meta data of a table such as total number of columns, column name, column type. Rsmd.java

import java.sql.*; class Rsmd{ public static void main(string args[]){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection con=drivermanager.getconnection( "jdbc:mysql://localhost/students","root","srinath"); PreparedStatement ps=con.preparestatement("select * from REGISTRATION"); ResultSet rs=ps.executequery(); //DatabaseMetaData dbmd= ResultSetMetaData rsmd=rs.getmetadata(); System.out.println("Total columns: "+rsmd.getcolumncount()); System.out.println("Column Name of 1st column: "+rsmd.getcolumnname(1)); System.out.println("Column Type Name of 1st column: "+rsmd.getcolumntypename(1)); con.close(); catch(exception e){ System.out.println(e); 8)What is session?write a Java Program to create and display session using JSP. Session:JSP Sessions. On a typical web site, a visitor might visit several pages and perform several interactions. If you are programming the site, it is very helpful to be able to associate some data with each visitor. For this purpose, "session"s can be used in JSP. A session is an object associated with a visitor. Session.html <html> <body> <form action="session.jsp"> <input type="text" name="uname"> <input type="submit" value="go"><br/> </form> </body> </html>

session.jsp <html> <body> <% String name=request.getparameter("uname"); session.setattribute("user",name); String name=(string)session.getattribute("user"); out.print("hello "+name); %> </body> </html>