Practical File Advance Java
Practical File Advance Java
DEPARTMENT:MCA
INDEX
S.no Content Page no Signature
1. Write a program to create Servlet to display
3-6
Username & password using doGet() method.
2. Write a program to create Servlet to display
6-10
Username & password using doPost() method.
3. Demonstrate the use of SendRedirect() method 10-16
4. illustrate the function of cookies in servlet 16-20
5. Demonstrate the use of REQUEST DISPATCHER 20-22
6. Introduction of JSP. 22-24
7. Demonstrate the use of script let tag in JSP 24-26
8. Demonstrate the use of isError and error tag in
26-28
JSP
9. Demonstrate the use of HTML tag in JSP 29-33
10. Demonstrate the sum of two number using JSP 3-36
11. illustrate the use of Hibernate 36-42
12. Demonstrate the use of HQL 42-49
13. Write a program to implement the concept of
49-52
RMI.
Advanced Java Laboratory-PGCA1922
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</form>
</body>
</html>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
Advanced Java Laboratory-PGCA1922
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>pack.servlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Servlet1
package pack;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
public class servlet1 extends HttpServlet {
@Override
Advanced Java Laboratory-PGCA1922
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</form>
</body>
</html>
Advanced Java Laboratory-PGCA1922
Servlet1.java
package pack;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
@Override
Advanced Java Laboratory-PGCA1922
@Override
resp.setContentType("Text/html");
out.println("hello world");
out.println("<html>");
out.println("<head>");
out.println("<title>welcome to servlet</title>");
out.println("<body>");
out.println("<h1>welcome to servlet1</h1>");
out.println("</body>");
out.println("</head>");
out.println("</html>");
}}
OUTPUT:
Advanced Java Laboratory-PGCA1922
WEB.XML
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-class>pack.Servlet1</servlet-class>
</servlet>
<servlet>
<servlet-name>Servlet2</servlet-name>
<servlet-class>pack.Servlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/Servlet1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Servlet2</servlet-name>
<url-pattern>/Servlet2</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
Advanced Java Laboratory-PGCA1922
</session-config>
</web-app>
INDEX.HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</form>
</body>
</html>
Advanced Java Laboratory-PGCA1922
Advanced Java Laboratory-PGCA1922
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</form>
</body>
</html>
SERVLET1
package pack;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
Advanced Java Laboratory-PGCA1922
importjavax.servlet.http.HttpServletResponse;
throwsServletException, IOException
response.setContentType("text/html;charset=UTF-8");
/* TODO output your page here. You may use following sample code. */
out.println("<THIS IS SERVLET1>");
if(name.equals("admin") &&password.equals("admin"))
response.sendRedirect("Servlet2?nm="+name+"&psw="+password+"");
else
response.sendRedirect("index.html");
}
Advanced Java Laboratory-PGCA1922
SERVLET2
package pack;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
throwsServletException, IOException
response.setContentType("text/html;charset=UTF-8");
out.println("name is "+name);
out.println("password is "+password);
out.println("this is servlet2");
Output:
Advanced Java Laboratory-PGCA1922
Advanced Java Laboratory-PGCA1922
<url-pattern>/Servlet2</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Servlet3</servlet-name>
<url-pattern>/Servlet3</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Index.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Advanced Java Laboratory-PGCA1922
</head>
<body>
<form method="get" action="Servlet1">
<input type="text" placeholder="enter your name" name="name"/>
<input type="password" placeholder="enter your password" name="pass"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>
Servlet1
package pack;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.RequestDispatcher;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
public class Servlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequestreq, HttpServletResponseresp) throws
ServletException, IOException{
resp.setContentType("text/html");
Advanced Java Laboratory-PGCA1922
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("this is servlet2");
String name= req.getParameter("name");
String password =req.getParameter("pass");
String database = (String)req.getAttribute("database");
out.println("Name is "+name);
out.println("Password is "+password);
out.println("Database is "+database);
out.println("<a href='Servlet3'>click here to move Servlet3</a>");
}
}
Servlet3
package pack;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
@author admin
public class Servlet3 extends HttpServlet {
@Override
Advanced Java Laboratory-PGCA1922
6.Introduction of JSP.
index.html
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</body>
</html>
welcome.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%= x%>
<%
%>
</body>
</html>
Output:
Advanced Java Laboratory-PGCA1922
<head>
<meta charset="UTF-8">
</head>
<body>
</body>
</html>
welcome.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%= x%>
<%
%>
</body>
</html>
Output:
Advanced Java Laboratory-PGCA1922
<head>
<meta charset="UTF-8">
</head>
<body>
</body>
</html>
newpage
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%= x%>
Advanced Java Laboratory-PGCA1922
<%
y = x/0;
%>
</body>
</html>
ERROR PAGE
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
</body>
</html>
Output:
Advanced Java Laboratory-PGCA1922
Advanced Java Laboratory-PGCA1922
<head>
<meta charset="UTF-8">
</head>
<body>
</body>
</html>
newpage
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%= x%>
Advanced Java Laboratory-PGCA1922
<%
y = x/0;
%>
</body>
</html>
ERROR PAGE
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
</body>
</html>
header
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
</body>
</html>
Output:
Advanced Java Laboratory-PGCA1922
<head>
<meta charset="UTF-8">
</head>
<body>
</form>
</body>
</html>
welcome
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
Advanced Java Laboratory-PGCA1922
<%
String n1=request.getParameter("num1");
String n2=request.getParameter("num2");
%>
</body>
</html>
math
<?xml version="1.0" encoding="UTF-8"?>
<tlib-version>1.0</tlib-version>
<short-name>math</short-name>
<uri>/WEB-INF/tlds/math</uri>
<tag>
<name>num</name>
<tag-class>pack.taghandler</tag-class>
<attribute>
<name>num1</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>num2</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
Advanced Java Laboratory-PGCA1922
</tag>
</taglib>
taghandler
package pack;
importjavax.servlet.jsp.JspException;
importjavax.servlet.jsp.JspWriter;
importjavax.servlet.jsp.tagext.TagSupport;
this.num1 = num1;
this.num2 = num2;
@Override
try{
int n1=Integer.parseInt(num1);
int n2=Integer.parseInt(num2);
JspWriter out=pageContext.getOut();
out.println("sum is"+sum);
catch(Exception e){
System.out.println("exception"+e);
}
Advanced Java Laboratory-PGCA1922
return SKIP_BODY;
}}
Output:
Advanced Java Laboratory-PGCA1922
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<servlet>
<servlet-name>CheckServlet</servlet-name>
<servlet-class>pack.CheckServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CheckServlet</servlet-name>
<url-pattern>/CheckServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
index.html
<!DOCTYPE html>
<!--
-->
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</form>
</body>
</html>
Advanced Java Laboratory-PGCA1922
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/demo8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">12345</property>
<mapping resource="hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Advanced Java Laboratory-PGCA1922
hibernate.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping>
<id name="roll">
<generator class="assigned"></generator>
</id>
</class>
</hibernate-mapping>
checkservlet.java
package pack;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.util.Iterator;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.hibernate.Query;
importorg.hibernate.Session;
importorg.hibernate.SessionFactory;
importorg.hibernate.Transaction;
Advanced Java Laboratory-PGCA1922
importorg.hibernate.cfg.Configuration;
importpojo.Userpojo;
throwsServletException, IOException
response.setContentType("text/html;charset=UTF-8");
// HQL
String roll0=request.getParameter("roll_number");
String hql = "from Userpojo U where U.roll=:R";//only for 1 record use SELECT Userpojo for all
record
query.setParameter("R",Integer.parseInt(roll0));
while(iterator.hasNext(){
Advanced Java Laboratory-PGCA1922
out.println("Name is "+obj.getName());
out.println("Email is "+obj.getEmail());
} tx.commit();
} }
controller.java
package pack;
importantlr.collections.List;
importjava.util.Iterator;
importorg.hibernate.Query;
importorg.hibernate.Session;
importorg.hibernate.SessionFactory;
importorg.hibernate.Transaction;
importorg.hibernate.cfg.Configuration;
importpojo.Userpojo;
String hql = "SELECT E1.roll from Userpojo E1";//only for 1 record use SELECT Userpojo for all record
while(iterator.hasNext())
tx.commit();
}}
Userpojo.java
privateint roll;
publicintgetRoll() {
return roll;
this.roll = roll;
return name;
this.name = name;
return contact;
this.contact = contact;
return email;
this.email = email;
}
Advanced Java Laboratory-PGCA1922
Advanced Java Laboratory-PGCA1922
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
index.html
<!DOCTYPE html>
<!--
-->
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
</body>
</html>
Hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/demo7</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">12345</property>
<mapping resource="hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Hibernate.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping>
<id name="roll">
<generator class="assigned"></generator>
</id>
</class>
</hibernate-mapping>
Controller.java
package pack;
importorg.hibernate.Session;
importorg.hibernate.SessionFactory;
importorg.hibernate.Transaction;
importorg.hibernate.cfg.Configuration;
importpojo.Userpojo;
Transaction tx =session.beginTransaction();
Advanced Java Laboratory-PGCA1922
obj.setRoll(2);
obj.setName("anchal");
obj.setEmail("anch.gmail.com");
obj.setContact("97467838");
session.save(obj);
tx.commit();
}}
Userpojo.java
packagepojo;
privateint roll;
this.roll = roll;
return name;
this.name = name;
return contact;
this.contact = contact;
return email;
this.email = email;}}
Output
Advanced Java Laboratory-PGCA1922
Advanced Java Laboratory-PGCA1922
packagermi;
importjava.rmi.Remote;
importjava.rmi.RemoteException;
public interface Adder extends Remote
{
publicint add(int a, int b) throws RemoteException;
packagermi;
importjava.rmi.NotBoundException;
importjava.rmi.RemoteException;
importjava.rmi.registry.LocateRegistry;
importjava.rmi.registry.Registry;
importjava.util.Scanner;
public class Client
{
public static void main(String args[]) throws RemoteException, NotBoundException
{
Client c = new Client();
c.connectRemote();
Advanced Java Laboratory-PGCA1922
}
private void connectRemote() throws RemoteException, NotBoundException
{
try
{ Scanner sc = new Scanner(System.in);
Registry reg = LocateRegistry.getRegistry("localhost",1099);
Adder ad = (Adder)reg.lookup("hello");
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println("Addition is "+ad.add(a, b));
}
catch(RemoteException e)
{
System.out.println("Exception is ctrated in Client "+e);
}
}
Advanced Java Laboratory-PGCA1922
packagermi;
importjava.rmi.RemoteException;
importjava.rmi.registry.LocateRegistry;
importjava.rmi.registry.Registry;
importjava.rmi.server.UnicastRemoteObject;
public class Server extends UnicastRemoteObject implements Adder
{
public Server() throws RemoteException
{
}
@Override
publicint add(int a, int b) throws RemoteException
{
int c= a+b;
return c;
}
Advanced Java Laboratory-PGCA1922
}
catch (RemoteException e)
{
System.out.println("Exception "+e);
}
}
}
Output:-