Tuesday, May 4, 2010

wap to validate the user id and password


/*

* ser_reqs.java

*

* Created on March 18, 2009, 9:47 AM

*/

package com;

import java.io.*;

import java.net.*;

import java.sql.*;

import javax.servlet.*;

import javax.servlet.http.*;

/**

*

* @author Administrator

* @version

*/

public class ser_reqs extends HttpServlet {

/** Initializes the servlet.

*/

String userid;

String password;

Connection dbconnection;

ResultSet resultset;

public void init() throws ServletException {

ServletConfig config = getServletConfig();

String driverClassName = config.getInitParameter("driverclassname");

String dbURL = config.getInitParameter("dburl");

String username = config.getInitParameter("username");

String dbpassword = config.getInitParameter("dbpassword");

try

{

Class.forName(driverClassName);

}

catch(ClassNotFoundException cnfe)

{

System.err.println("Error loading driver: " + cnfe);

}

try

{

dbconnection = DriverManager.getConnection(dbURL, username, dbpassword);

}

catch(SQLException sqle)

{

System.err.println("Connection error: " + sqle);

}

}

/** Destroys the servlet.

*/

public void destroy() {

}

/** Processes requests for both HTTP GET and POST methods.

* @param request servlet request

* @param response servlet response

*/

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

// String s1=request.getParameter("t1");

// String ps=request.getParameter("p1");

try

{

Statement statement = dbconnection.createStatement();

String sqlString = "SELECT * FROM st_record WHERE name='"+request.getParameter("t1")+"'AND password='"+request.getParameter("p1")+"'";

resultset=statement.executeQuery(sqlString);

while(resultset.next())

{

userid = resultset.getString("name");

password = resultset.getString("password");

}

}

catch(SQLException sqle)

{

System.err.println("Connection error: " + sqle);

}

if(password.equals(request.getParameter("p1")) && userid.equals(request.getParameter("t1")))

{

response.sendRedirect("/ser_req/corret.html");

}

else

{

response.sendRedirect("/ser_req/incorret.html");

}

//TODO output your page here

out.println("");

out.println("");

out.println("Servlet");

out.println("");

out.println("");

// out.println(s1);

// out.println(ps);

out.println("");

out.println("");

out.close();

}

/** Handles the HTTP GET method.

* @param request servlet request

* @param response servlet response

*/

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

/** Handles the HTTP POST method.

* @param request servlet request

* @param response servlet response

*/

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

}

/** Returns a short description of the servlet.

*/

public String getServletInfo() {

return "Short description";

}


Rest of program do urself....

Wap to implement Servlet

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
*
* @author student
*/
public class NewServlet extends HttpServlet {

/**
* Processes requests for both HTTP GET and POST methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Welcome "+ request.getParameter("t1"));
}



protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { processRequest(request, response);

}

/**
* Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
*/
//
}

You Have to do the jsp code by urself...

Program to implement Prepared Statement in JDBC

import java.sql.*;
import java.lang.*;
public class pst
{public static void main(String args[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String query="select *from employee where salary>20000";
Connection con=DriverManager.getConnection("jdbc:odbc:name","","");
PreparedStatement pst = con.prepareStatement(query);
ResultSet rs1 = pst.executeQuery();

while(rs1.next())
{
System.out.println(rs1.getString(1)+ "\t" +rs1.getString(2)+ "\t" +rs1.getString(3)+ "\t" +rs1.getString(4)+
"\t" +rs1.getString(5));
}
}
catch(Exception e)
{System.out.println("error " +e);
}
}
}

To implement Remote Method Invocation(RMI) (Client Server Application)

Product.java

import java.rmi.*;

public interface product extends Remote{

public String getname() throws RemoteException;

}

Productimpl.java

import java.rmi.*;

import java.rmi.server.*;

public class productimpl extends UnicastRemoteObject implements product

{

public String name1;

productimpl(String name) throws RemoteException

{

name1=name;

}

public String getname()

{

return name1;

}

}

Productclient.java

import java.rmi.*;

import java.rmi.registry.*;

public class productclient

{

public static void main(String s[])

{

try

{

product p=(product)Naming.lookup("rmi://127.0.0.1:1099/java");

System.out.println("productname="+p.getname());

}

catch(Exception e)

{

System.out.println(e);

}

}

}

Productserver.java

import java.rmi.*;

import java.rmi.registry.*;

public class productserver

{

public static void main(String s[])

{

try{

LocateRegistry.getRegistry(1099);

productimpl p1 =new productimpl("java");

Naming.bind("java",p1);

}

catch(java.lang.Exception e)

{System.out.println(e);

}

}

}

JDBC programing....

import java.sql.*;
/**
*
* @author Administrator
*/
public class jdbc_test {

/** Creates a new instance of jdbc_test */
public jdbc_test() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

Connection con1=null;
Statement stmt1=null;
ResultSet rs1=null;
//PreparedStatement pstmt=null;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException cnfe){ }
try{
con1 = DriverManager.getConnection("jdbc:mysql://localhost/test","root","asecse");
stmt1 = con1.createStatement();
rs1 = stmt1.executeQuery("select *FROM student");

while(rs1.next()){

System.out.println(rs1.getString("name"));

}

}catch(SQLException sqle){ }

}}

Monday, May 3, 2010

Q.WAP to create a client server application in which the client sends some string to the server and server responds by converting the string to upper

//Client:

import java.net.*;
import java.io.*;
class LocalClient1
{
public static void main(String s[]) throws IOException,UnknownHostException
{
String sin, sout;
Socket s=new Socket("localhost",1010);
BufferedReader bclient=new BufferedReader(new InputStreamReader(System.in));
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
BufferedReader bserver=new BufferedReader(new InputStreamReader(s.getInputStream()));
sin=bclient.readLine();
dos.writeBytes(sin+"\n");
sout=bserver.readLine();
System.out.println("Output String:"+sout);
s.close();
}
}


//Server:

import java.net.*;
import java.io.*;
class LocalServer
{
public static void main(String s[]) throws IOException,UnknownHostException
{
String s1, s2;
SreverSocket ss=new ServerSocket(1010);
System.out.println("Listen...");
while(true)
{
Socket s=ss.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
DataOutputStream d=new DataOutputStream(s.getOutputStream());
s1=br.readLine();
s2=s1.toUpperCase();
d.WriteBytes(s2+"\n");
s.close();
}

Q. WAP to obtain IP address of localhost and any well known website

import java.net.*;
import java.io.*;

class prg1{
static void p(String s)
{
System.out.println(s);
}

public static void main(String s[]) throws UnknownHostException, IOException
{
InetAddresss i,o[];
i=InetAddress.getLocalHost();
p("local host"+i);
i=InetAddress.getByName("10.0.56.80");
p("10.0.56.80"+i);
o=InetAddress.getAllByName("www.ndtv.com");
for(int j=0;j{
p("ndtv ip address"+j+" "+o[j]);
}
}
}