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>