Experiment-1<br />Aim:- Program to access a table “table1” from MS-Access using java code.<br />Program:-<br />import  jav...
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
My java file
Upcoming SlideShare
Loading in...5
×

My java file

1,872

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,872
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
125
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

My java file

  1. 1. Experiment-1<br />Aim:- Program to access a table “table1” from MS-Access using java code.<br />Program:-<br />import java.sql.*;<br /> public class Main {<br />public static void main(String[] args)<br /> {<br /> try {<br /> Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");<br /> }<br /> catch(Exception e)<br /> {<br /> System.out.println(e);<br /> }<br /> try<br /> {<br /> Connection conn;<br /> Statement stmt;<br /> ResultSet rs;<br /> conn=DriverManager.getConnection("Jdbc:Odbc:anamika","anamika","anamika");<br /> stmt=conn.createStatement();<br /> rs=stmt.executeQuery("Select * from table1");<br /> while(rs.next())<br /> {<br /> System.out.println(rs.getString("name"));<br /> System.out.println(rs.getString("rollno"));<br /> }<br /> }<br /> catch(Exception e)<br /> {<br /> System.out.println(e);<br /> }<br />}<br />}<br />Output<br />Experiment:-2<br />Aim:- Program to access a table “table1” from derby database using java code.<br />Program:-<br />import java.sql.*;<br /> public class Main {<br />public static void main(String[] args)<br /> {<br /> try {<br /> Class.forName("org.apache.derby.jdbc.ClientDriver ");<br /> }<br /> catch(Exception e)<br /> {<br /> System.out.println(e);<br /> }<br /> try<br /> {<br /> Connection conn;<br /> Statement stmt;<br /> ResultSet rs;<br /> conn=DriverManager.getConnection("jdbc:derby://localhost:1527/anamika","anamika","anamika");<br /> stmt=conn.createStatement();<br /> rs=stmt.executeQuery("Select * from table1");<br /> while(rs.next())<br /> {<br /> System.out.println(rs.getString("name"));<br /> System.out.println(rs.getString("rollno"));<br /> }<br /> }<br /> catch(Exception e)<br /> {<br /> System.out.println(e);<br /> }<br />}<br />}<br />Output:-<br />Experiment-3<br />Aim:-Program for invoking a remote method using interface.<br />Program:-<br />Product.java<br />import java.rmi.*;<br />public interface product extends Remote<br />{<br />public String getname() throws RemoteException;<br />}<br />Product_impl.java<br />import java.rmi.*;<br />import java.rmi.server.*;<br />public class product_impl extends UnicastRemoteObject implements product<br />{<br />public String name;<br />public product_impl(String name) throws RemoteException<br />{this.name=name;<br />}<br />public String getname()<br />{<br />return name;<br />}<br /> }<br />Productserver.java<br />import java.rmi.*;<br />import java.rmi.registry.*;<br />public class productserver<br />{<br />public static void main(String s[])<br />{<br />try<br />{<br />LocateRegistry.getRegistry(1099);<br />product_impl p1=new product_impl("java");<br />Naming.rebind("xyz",p1);<br />}<br />catch(Exception e)<br />{<br />System.out.println(e);<br />}<br />}<br />}<br />Productclient.java<br />import java.rmi.*;<br />import java.rmi.registry.*;<br /> class productclient<br />{<br />public static void main(String s[])<br />{<br />try<br />{<br />product p=(product)Naming.lookup("rmi://localhost:1099/xyz");<br />System.out.println(p.getname());<br />}<br />catch(Exception e)<br />{<br />System.out.println(e);<br />}<br />}<br />}<br />Generating stub and skeleton<br />Start server<br />Start client<br />Experiment:-4<br />Aim:- Program that implement a simple servlet program<br />Program:-<br />//Prog for simple servelet<br />import java.io.IOException;<br />import java.io.PrintWriter;<br />import javax.servlet.ServletException;<br />import javax.servlet.annotation.WebServlet;<br />import javax.servlet.http.HttpServlet;<br />import javax.servlet.http.HttpServletRequest;<br />import javax.servlet.http.HttpServletResponse;<br />/**<br /> *<br /> * @author anamika<br /> */<br />@WebServlet(name="NewServlet", urlPatterns={"/NewServlet"})<br />public class NewServlet extends HttpServlet {<br /> protected void processRequest(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> response.setContentType("text/html;charset=UTF-8");<br /> PrintWriter out = response.getWriter();<br /> try {<br /> // TODO output your page here<br /> out.println("<html>");<br /> out.println("<head>");<br /> out.println("<title>Servlet NewServlet</title>"); <br /> out.println("</head>");<br /> out.println("<body>");<br /> out.println("<h1>Servlet NewServlet at " + request.getContextPath () + "</h1>");<br /> out.println("</body>");<br /> out.println("This is my first servlet program");<br /> out.println("</html>");<br /> <br /> } finally { <br /> out.close();<br /> }<br /> } <br />Output<br /> <br />Experiment 5<br />Program:-servlet connectivity wid derby<br />import java.io.*;<br />import java.sql.*;<br />import javax.servlet.ServletConfig;<br />import javax.servlet.ServletException;<br />import javax.servlet.http.*;<br />/**<br /> *<br /> * @author student<br /> * @version<br /> */<br />public class NewServlet extends HttpServlet {<br /> Connection dbconnection;<br /> ResultSet resultset;<br /> /** Initializes the servlet.<br /> */<br /> public void init() throws ServletException {<br /> ServletConfig config = getServletConfig();<br /> String driverClassName = config.getInitParameter("driverclassname");<br /> String dbURL = config.getInitParameter("dburl");<br /> String username = config.getInitParameter("username");<br /> String dbpassword = config.getInitParameter("dbpassword");<br /> try<br /> {<br /> Class.forName(driverClassName);<br /> }<br /> catch(ClassNotFoundException cnfe)<br /> {<br /> System.err.println("Error loading driver: " + cnfe);<br /> }<br /> try<br /> {<br /> dbconnection = DriverManager.getConnection(dbURL, username, dbpassword);<br /> }<br /> catch(SQLException sqle)<br /> {<br /> System.err.println("Connection error: " + sqle);<br /> }<br /> }<br /> /** Destroys the servlet.<br /> */<br /> public void destroy() {<br /> }<br /> /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.<br /> * @param request servlet request<br /> * @param response servlet response<br /> */<br /> protected void processRequest(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> response.setContentType("text/html");<br /> PrintWriter out = response.getWriter();<br /> //TODO output your page here<br /> out.println("<html>");<br /> out.println("<head>");<br /> out.println("<title>Servlet</title>");<br /> out.println("</head>");<br /> out.println("<body>");<br /> try<br /> {<br /> Statement statement = dbconnection.createStatement();<br /> String sqlString = "SELECT * FROM app.untitled";<br /> resultset=statement.executeQuery(sqlString);<br /> while(resultset.next())<br /> {<br /> out.println(resultset.getString("name"));<br /> out.println(resultset.getString("rollno"));<br /> // password = resultset.getString("UserPassword");<br /> }<br /> }<br /> catch(SQLException sqle)<br /> {<br /> System.err.println("Connection error: " + sqle);<br /> }<br /> // out.println("subahsh");<br /> out.println("</body>");<br /> out.println("</html>");<br /> out.close();<br /> }<br /> /** Handles the HTTP <code>GET</code> method.<br /> * @param request servlet request<br /> * @param response servlet response<br /> */<br /> protected void doGet(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> processRequest(request, response);<br /> }<br /> /** Handles the HTTP <code>POST</code> method.<br /> * @param request servlet request<br /> * @param response servlet response<br /> */<br /> protected void doPost(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> processRequest(request, response);<br /> }<br /> /** Returns a short description of the servlet.<br /> */<br /> public String getServletInfo() {<br /> return "Short description";<br /> }<br />}<br />Web.xml<br /><?xml version="1.0" encoding="UTF-8"?><br /><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><br /> <servlet><br /> <servlet-name>NewServlet</servlet-name><br /> <servlet-class>NewServlet</servlet-class><br /> <init-param><br /> <param-name>driverclassname</param-name><br /> <param-value>org.apache.derby.jdbc.ClientDriver</param-value><br /> </init-param><br /> <init-param><br /> <param-name>dburl</param-name><br /> <param-value>jdbc:derby://localhost:1527/anamika</param-value><br /> </init-param><br /> <init-param><br /> <param-name>username</param-name><br /> <param-value>anamika</param-value><br /> </init-param><br /> <init-param><br /> <param-name>dbpassword</param-name><br /> <param-value>anamika</param-value><br /> </init-param><br /> </servlet><br /> <servlet-mapping><br /> <servlet-name>NewServlet</servlet-name><br /> <url-pattern>/NewServlet</url-pattern><br /> </servlet-mapping><br /> <session-config><br /> <session-timeout><br /> 30<br /> </session-timeout><br /> </session-config><br /> <welcome-file-list><br /> <welcome-file>index.jsp</welcome-file><br /> </welcome-file-list><br /> </web-app><br />Output<br />Experiment:-login using servelts<br />import java.io.*;<br />import java.sql.*;<br />import javax.servlet.ServletConfig;<br />import javax.servlet.ServletException;<br />import javax.servlet.http.*;<br />/**<br /> *<br /> * @author student<br /> * @version<br /> */<br />public class NewServlet extends HttpServlet {<br /> Connection dbconnection;<br /> ResultSet resultset;<br /> /** Initializes the servlet.<br /> */<br /> String userid;<br /> String password;<br /> public void init() throws ServletException {<br /> ServletConfig config = getServletConfig();<br /> String driverClassName = config.getInitParameter("driverclassname");<br /> String dbURL = config.getInitParameter("dburl");<br /> String username = config.getInitParameter("username");<br /> String dbpassword = config.getInitParameter("dbpassword");<br /> try<br /> {<br /> Class.forName(driverClassName);<br /> }<br /> catch(ClassNotFoundException cnfe)<br /> {<br /> System.err.println("Error loading driver: " + cnfe);<br /> }<br /> try<br /> {<br /> dbconnection = DriverManager.getConnection(dbURL, username, dbpassword);<br /> }<br /> catch(SQLException sqle)<br /> {<br /> System.err.println("Connection error: " + sqle);<br /> }<br /> }<br /> /** Destroys the servlet.<br /> */<br /> public void destroy() {<br /> }<br /> /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.<br /> * @param request servlet request<br /> * @param response servlet response<br /> */<br /> protected void processRequest(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> response.setContentType("text/html");<br /> PrintWriter out = response.getWriter();<br /> // String s1=request.getParameter("t1");<br /> // String ps=request.getParameter("p1");<br /> try<br /> {<br /> Statement statement = dbconnection.createStatement();<br /> String sqlString = "SELECT * FROM app.student1 WHERE username='"+request.getParameter("userid")+"'AND password='"+request.getParameter("pass")+"'";<br /> resultset=statement.executeQuery(sqlString);<br /> while(resultset.next())<br /> {<br /> userid = resultset.getString("username");<br /> password = resultset.getString("password");<br /> }<br /> }<br /> catch(SQLException sqle)<br /> {<br /> System.err.println("Connection error: " + sqle);<br /> }<br /> if(password.equals(request.getParameter("pass")) && userid.equals(request.getParameter("userid")))<br /> {<br /> response.sendRedirect("/WebApplication2/correct.html");<br /> }<br /> else<br /> {<br /> response.sendRedirect("/WebApplication2/incorrect.html");<br /> }<br /> //TODO output your page here<br /> out.println("<html>");<br /> out.println("<head>");<br /> out.println("<title>Servlet</title>");<br /> out.println("</head>");<br /> out.println("<body>");<br /> // out.println(s1);<br /> // out.println(ps);<br /> out.println("</body>");<br /> out.println("</html>");<br /> out.close();<br /> }<br /> /** Handles the HTTP <code>GET</code> method.<br /> * @param request servlet request<br /> * @param response servlet response<br /> */<br /> protected void doGet(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> processRequest(request, response);<br /> }<br /> /** Handles the HTTP <code>POST</code> method.<br /> * @param request servlet request<br /> * @param response servlet response<br /> */<br /> protected void doPost(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> processRequest(request, response);<br /> }<br /> /** Returns a short description of the servlet.<br /> */<br /> public String getServletInfo() {<br /> return "Short description";<br /> }<br />}<br />Mytable is student1 wid username,password<br />loginframe<br />Top of Form<br />Top of Form<br /><form action="/Program6/NewServlet" method="POST"><br /> <h1>UserId<input type="text" name="userid" value="" size="20" /> <br /> Password<input type="password" name="pass" value="" size="20" /><br /> <input type="submit" value="login" /></h1><br />Correct.html<br />Incorrect.html<br />Web.xml:-same as above<br />Experiment:-login using jsp<br />create jsp and insert code of loginframe into its body.rest all is same as above and delete correct.html<br />Experiment:-session management<br />Changes are only underlined <br />/*<br /> * To change this template, choose Tools | Templates<br /> * and open the template in the editor.<br /> */<br />import java.io.*;<br />import java.sql.*;<br />import javax.servlet.ServletConfig;<br />import javax.servlet.ServletException;<br />import javax.servlet.http.*;<br />/**<br /> *<br /> * @author student<br /> * @version<br /> */<br />public class NewServlet extends HttpServlet {<br /> Connection dbconnection;<br /> ResultSet resultset;<br /> /** Initializes the servlet.<br /> */<br /> String userid;<br /> String password;<br /> public void init() throws ServletException {<br /> ServletConfig config = getServletConfig();<br /> String driverClassName = config.getInitParameter("driverclassname");<br /> String dbURL = config.getInitParameter("dburl");<br /> String username = config.getInitParameter("username");<br /> String dbpassword = config.getInitParameter("dbpassword");<br /> try<br /> {<br /> Class.forName(driverClassName);<br /> }<br /> catch(ClassNotFoundException cnfe)<br /> {<br /> System.err.println("Error loading driver: " + cnfe);<br /> }<br /> try<br /> {<br /> dbconnection = DriverManager.getConnection(dbURL, username, dbpassword);<br /> }<br /> catch(SQLException sqle)<br /> {<br /> System.err.println("Connection error: " + sqle);<br /> }<br /> }<br /> /** Destroys the servlet.<br /> */<br /> public void destroy() {<br /> }<br /> /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.<br /> * @param request servlet request<br /> * @param response servlet response<br /> */<br /> protected void processRequest(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> response.setContentType("text/html");<br /> PrintWriter out = response.getWriter();<br /> // String s1=request.getParameter("t1");<br /> // String ps=request.getParameter("p1");<br /> HttpSession s=request.getSession();<br /> try<br /> {<br /> Statement statement = dbconnection.createStatement();<br /> String sqlString = "SELECT * FROM app.STUDENT WHERE userid='"+request.getParameter("userid")+"'AND pass='"+request.getParameter("pass")+"'";<br /> resultset=statement.executeQuery(sqlString);<br /> while(resultset.next())<br /> {<br /> userid = resultset.getString("userid");<br /> password = resultset.getString("pass");<br /> }<br /> }<br /> catch(SQLException sqle)<br /> {<br /> System.err.println("Connection error: " + sqle);<br /> }<br /> if(password.equals(request.getParameter("pass")) && userid.equals(request.getParameter("userid")))<br /> {<br /> response.sendRedirect("/WebApplication3/correct.jsp?ssid="+s.getId()+"");<br /> }<br /> else<br /> {<br /> response.sendRedirect("/WebApplication3/incorrect.jsp?ssid="+s.getId()+"");<br /> }<br /> //TODO output your page here<br /> out.println("<html>");<br /> out.println("<head>");<br /> out.println("<title>Servlet</title>");<br /> out.println("</head>");<br /> out.println("<body>");<br /> // out.println(s1);<br /> // out.println(ps);<br /> out.println("</body>");<br /> out.println("</html>");<br /> out.close();<br /> }<br /> /** Handles the HTTP <code>GET</code> method.<br /> * @param request servlet request<br /> * @param response servlet response<br /> */<br /> protected void doGet(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> processRequest(request, response);<br /> }<br /> /** Handles the HTTP <code>POST</code> method.<br /> * @param request servlet request<br /> * @param response servlet response<br /> */<br /> protected void doPost(HttpServletRequest request, HttpServletResponse response)<br /> throws ServletException, IOException {<br /> processRequest(request, response);<br /> }<br /> /** Returns a short description of the servlet.<br /> */<br /> public String getServletInfo() {<br /> return "Short description";<br /> }<br />}<br /><%-- <br /> Document : correct<br /> Created on : Apr 10, 2011, 3:18:30 AM<br /> Author : babu<br />--%><br /><%@page contentType="text/html" pageEncoding="UTF-8"%><br /><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"<br /> "http://www.w3.org/TR/html4/loose.dtd"><br /><html><br /> <head><br /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br /> <title>JSP Page</title><br /> </head><br /> <body><br /> <%!<br />String userid;<br />String in_session;<br />%><br /><%<br />userid=request.getParameter("ssid");<br />String Id=session.getId();<br />//out.println(userid);<br />//out.println(Id);<br />//if(Id.equals(userid))<br /> if(request.getParameter("ssid").equals(session.getId()))<br />{%><br /> correct yipee<br /> <% }<br />else{<br /> %><br /> // response.sendRedirect("abc.html");<br /> <jsp:forward page="abc.html" /><br /> <%<br />}<br />session.invalidate();<br />%><br /> </body><br /></html><br />Same of incorrect.jsp only make correct inplace of correct<br />Web.xml same as previous<br />Output<br />This above url pasted on another tab and we get d output<br />Experiment:-jsp connectivity<br /><%-- <br /> Document : jspconnectivity<br /> Created on : Apr 10, 2011, 3:38:37 AM<br /> Author : babu<br />--%><br /><%@page language="java" import="java.sql.*"%><br /><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"<br /> "http://www.w3.org/TR/html4/loose.dtd"><br /><html><br /> <head><br /> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><br /> <title>JSP Page</title><br /> </head><br /> <body><br /> <%<br /> try {<br /> Class.forName("org.apache.derby.jdbc.ClientDriver");<br /> }<br /> catch(Exception e)<br /> {<br /> System.out.println(e);<br /> }<br /> try<br /> {<br /> Connection conn;<br /> Statement stmt;<br /> ResultSet rs;<br /> conn=DriverManager.getConnection("jdbc:derby://localhost:1527/anamika","anamika","anamika");<br /> stmt=conn.createStatement();<br /> rs=stmt.executeQuery("Select * from app.student");<br /> // PrintStream out= new PrintStream(response.getOutputStream());<br /> //PrintStream out= new PrintStream(response.getOutputStream());<br /> //response.setContentType("text/html");<br /> //response.setContentType("text/html");<br /> out.println("<html>");<br /> out.println("<head>");<br /> out.println("<title>My table</title>");<br /> out.println("<title>my personal details</title>");<br /> out.println("</head>");<br /> out.println("<body>");<br /> out.println("<center>");<br />//out.println(“<center>”);<br /> out.print("<table border=1>");<br /> out.print("<td>userid</td>");<br /> out.print("<td>password</td>");<br /> // System.out.println("Rollno Name");<br /> while(rs.next())<br /> {<br /> //out.println(rs.getString("userid"));<br /> // out.println(rs.getString("pass"));<br /> out.print("<tr>");<br /> out.print("<td>"+rs.getString(1));<br /> String s=rs.getString(2);<br /> out.print("<td>"+s);<br /> }<br /> //out.println(“</center>”);<br /> out.println("</center>");<br /> out.println("</table>");<br /> out.println("</body>");<br /> out.println("</html>");<br /> }<br /> catch(Exception e)<br /> {<br /> System.out.println(e);<br /> }<br />%><br /> </body><br /></html><br />Output<br />

×