PROGRAM TO SHOW VALIDATION OF USER USING SERVLET
RequestServlet.java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.sql.*;
public class RequestServlet extends HttpServlet
{
public void service(HttpServletRequest req, HttpServletResponse res)throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<html><body>");
String name=req.getParameter("name");
String pass=req.getParameter("pass");
if(name.equals("ABCDE")&& pass.equals("123456"))
out.println("user is valid through service method");
else
out.println("user is invalid through service method");
out.println("</body></html>");
}
PROGRAM TO DISPLAY MESSAGE ON BROWSER USING JSP
Init.jsp
<html>
<body>
<%!
String s;
public void jspInit()
{
ServletConfig con=getServletConfig();
s=con.getInitParameter("name");
}
%>
<%
String s1=config.getInitParameter("name");
out.println(s+s1);
%>
</body>
</html>
Web.xml
<web-app>
<servlet>
<servlet-name>my</servlet-name>
<jsp-file>Init.jsp</jsp-file>
<init-param>
<param-name>name</param-name>
<param-value> i miss you</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>my</servlet-name>
<url-pattern>/run</url-pattern>
</servlet-mapping>
</web-app>
</body>
</html>
public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<html><body>");
String name=req.getParameter("name");
String pass=req.getParameter("pass");
if(name.equals("ABCDE")&& pass.equals("123456"))
out.println("user is valid through get method");
else
out.println("user is invalid through get mathod");
out.println("</body></html>");
}
public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<html><body>");
String name=req.getParameter("name");
String pass=req.getParameter("pass");
if(name.equals("ABCDE")&& pass.equals("123456"))
out.println("user is valid through get method");
else
out.println("user is invalid through get mathod");
out.println("</body></html>");
}
}
Login.html
<html>
<body>
<form action="run3" method="get">
enter your name:
<input type="text" name="name"/>
<br>
enter your password:
<input type="password" name="pass"/>
<br>
<input type="submit"/>
</body>
</html>
Web.xml
<web-app>
<servlet>
<servlet-name>RequestServlet</servlet-name>
<servlet-class>RequestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RequestServlet</servlet-name>
<url-pattern>/run3</url-pattern>
</servlet-mapping>
</web-app>
PROGRAM TO INSERT DATA INTO TABLE USING JSP
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
class TestDB1
{
public static void main(String ss[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:mydsn","system","mca6");
String t1="China";
int t2=422;
Statement s=c.createStatement();
s.executeUpdate("INSERT INTO emp " + "VALUES ('chinaaaaaaa',200000001)");
ResultSet result1=s.executeQuery("SELECT * FROM emp");
while(result1.next())
{
System.out.println("name : "+result1.getString(1));
System.out.println("salary : "+result1.getString(2));
}
System.out.println("after insertion");
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(Exception i)
{
System.out.println(i);
}
}
}
PROGRAM OF CALLING ONE SERVLET BY ANOTHER SERVLET
RequestDispatcher2.java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class RequestDispatcher2 extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<html><body>");
String s="before dispatcher";
out.println(s);
try
{
RequestDispatcher rd=req.getRequestDispatcher("/run2");
rd.include(req,res);
}
catch(Exception e)
{
out.println("exception");
}
out.println("after dispatcher");
out.println("</body></html>");
}
}
DemoServlet2.java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class DemoServlet2 extends GenericServlet
{
public void service(ServletRequest req, ServletResponse res)throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println("<html><body>");
out.println("hello servlet with GenericServlet");
out.println("</body></html>");
}
}
Web.xml
<web-app>
<servlet>
<servlet-name>RequestDispatcher2</servlet-name>
<servlet-class>RequestDispatcher2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RequestDispatcher2</servlet-name>
<url-pattern>/run7</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DemoServlet2</servlet-name>
<servlet-class>DemoServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DemoServlet2</servlet-name>
<url-pattern>/run2</url-pattern>
</servlet-mapping>
</web-app>
PROGRAM TO SHOW VALIDATION OF USER USING JSP
Parameter.jsp
<%
String name=request.getParameter("name");
String pass=request.getParameter("pass");
RequestDispatcher rd=request.getRequestDispatcher("Demo.jsp");
rd.include(request,response);
if(name.equals("DUCAT")&& pass.equals("DUCAT"))
out.println("user is valid through jsp parameter");
else
out.println("user is invalid through jsp parameter");
%>
Index.jsp
<html>
<body>
<form action="Parameter.jsp">
enter your name:
<input type="text" name="name">
<br>
enter your password:
<input type="password" name="pass"/>
<br>
<input type="submit"/>
</body>
</html>
Demo.jsp
<%out.println("hello jsp");%>
Web.xml
<web-app>
</web-app>
PROGRAM TO DEMONSTRATE USE OF BEANS
MyBean.java
package my;
public class MyBean
{
private String name;
private int pass;
public void setName(String name)
{
this.name=name;
}
public void setPass(int pass)
{
this.pass=pass;
}
public String getName()
{
return name;
}
public int getPass()
{
return pass;
}
public String validate()
{
try
{
if(name.equals("kapil"))
return "valid";
}
catch(Exception e){}
return "invalid";
}
public int add(int x,int y)
{
return x+y;
}
}
Bean.jsp
<%
String name=request.getParameter("name");
String pass=request.getParameter("pass");
%>
<jsp:useBean id="t1" class="my.MyBean"/>
<jsp:setProperty name="t1" property="name" param="name"/>
<jsp:setProperty name="t1" property="pass" param="pass"/>
<%
String s=t1.validate();
int r=t1.add(10,20);
out.println(s+r);
%>
<jsp:getProperty name="t1" property="name"/>
<jsp:getProperty name="t1" property="pass"/>
Index.jsp
<html>
<body>
<form action="Bean.jsp">
enter your name:
<input type="text" name="name">
<br>
enter your password:
<input type="password" name="pass"/>
<br>
<input type="submit"/>
</body>
</html>
Web.xml
<web-app>
</web-app>
PROGRAM TO SET SCOPE OF BEANS
MyBean.java
package my;
public class MyBean
{
private String name;
private int pass;
public void setName(String name)
{
this.name=name;
}
public void setPass(int pass)
{
this.pass=pass;
}
public String getName()
{
return name;
}
public int getPass()
{
return pass;
}
public String validate()
{
try
{
if(name.equals("ducat"))
return "valid";
}
catch(Exception e){}
return "invalid";
}
public int add(int x,int y)
{
return x+y;
}
}
Bean.jsp
<jsp:useBean id="t1" class="my.MyBean" scope="session"/>
<jsp:setProperty name="t1" property="*" param="name"/>
<%
String s=t1.validate();
int r=t1.add(10,20);
out.println(s+r);
%>
<jsp:getProperty name="t1" property="name"/>
<jsp:getProperty name="t1" property="pass"/>
Index.jsp
<html>
<body>
<form action="Bean.jsp">
enter your name:
<input type="text" name="name">
<br>
enter your password:
<input type="password" name="pass"/>
<br>
<input type="submit"/>
</body>
</html>
PROGRAM TO DEMONSTRATE WORKING OF RMI
MyServer.java
package my;
import java.rmi.*;
import java.rmi.server.*;
public class MyServer extends UnicastRemoteObject implements MyRemote
{
public MyServer() throws RemoteException
{}
public int add(int x,int y)throws RemoteException
{
return(x+y);
}
}
Register.java
package my;
import java.rmi.*;
import java.rmi.server.*;
public class Register
{
public static void main(String s[])
{
try
{
my.MyServer m=new my.MyServer();
Naming.rebind("ducat",m);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
MyRemote.java
package my;
import java.rmi.*;
public interface MyRemote extends Remote
{
public int add(int x,int y)throws RemoteException;
}
Remote.java
import my.*;
import java.rmi.*;
import java.rmi.server.*;
public class Client
{
public static void main(String s[])
{
try
{
my.MyRemote m=(my.MyRemote)Naming.lookup("rmi://localhost:1099/ducat");
System.out.println(m.add(10,20));
}
catch(Exception e)
{
System.out.println(e);
}
}
No comments:
Post a Comment