Académique Documents
Professionnel Documents
Culture Documents
Dynamic.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Newhtml.html
<!DOCTYPE html>
<html>
<head>
<title> Dynamic </title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form name="form" method="post" action="Dynamic">
<pre>
User Name:<input type=text name="uname" />
Password:<input type=password name="pord" />
</pre>
<input type=submit value="Submit" />
</form>
</body>
</html>
StocksRefresh.java
import java.io.*;
import java.util.Random;
import javax.servlet.*;
import javax.servlet.http.*;
ServletException,IOException {
String title;
response.setContentType("text/html");
response.addHeader("Refresh", "5");
out.println("<html><head><title>"+title+
"</title></head>\n<body>\n");
out.println("<tr>\n");
out.println("<td>"+sharenames[i]+"</td>");
out.println("<td>Rs. "+getPrice()+"</td>");
out.println("</tr>");
out.println("</center></body></html>");
return ranNum;
Output:
GetHandler.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
PostHandler.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
pw.println("Welcome "+name);
pw.close();
}
}
Senddata.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>GET METHOD</h1>
<form method="GET" action="GetHandler">
<label>Name:</label><input type="text" name="name"/><br/>
<input type="submit" value="GET"/>
</form>
<h1>POST METHOD</h1>
<form method="POST" action="PostHandler">
<label>Name:</label><input type="text" name="name"/><br/>
<input type="submit" value="POST"/>
</form>
</body>
</html>
lab04.html
<html>
<head>
<title>lab04</title>
</head>
<body>
<center>
<form name="form1" method="POST" action="Lab04AddCookie">
<pre>
<b>Enter the following details</b><br/>
Name: <input type="text" name="name"/><br/>
College: <input type="text" name="college"/><br/>
Location: <input type="text" name="location"/><br/>
</pre>
Lab04AddCookie.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
Lab04GetCookie.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
out.println("<h2>Welcome</h2><br/>");
for(int i = 0;i<cookies.length;i++) {
String name = cookies[i].getName();
lab04home.html
<html>
<head>
<title>Lab 04 user preferences</title>
</head>
<body>
<p>New Visitor: <a href="lab04.html">Click here</a></p>
<p>Existing visitor: <a href="Lab04GetCookie">click here</a></p>
</body>
</html>
Welcome.html
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Hello Users</h1>
<form method="post" action="index.jsp">
<p>
Username :<input type="text" name="username"><br />
Password:<input type="password" name="password">
</p>
<p><input type="submit" value="login"></p>
</form>
</body>
</html>
Select the index.jsp in the Lab8b->Web Pages and Type the following
Index.jsp
<!DOCTYPE html>
<html>
<head>
<title>Lab05b</title>
</head>
<body>
<h1>Lab 05b</h1>
<center>
<%@page import="java.util.Random" %>
<%
Random ran = new Random();
int ranNum = ran.nextInt(100);
%>
<b>Random number generated is: </b>
<% ranNum %>
</center>
</body>
</html>
lab06.jsp
<html>
<head>
<title>Lab 06</title>
</head>
<body>
<% String s;
s = request.getParameter("rad");
if("include".equals(s))
{
%>
<jsp:include page="includepage.jsp"/>
}
else
{
%>
<jsp:forward page="forwardpage.jsp"/>
<% }
%>
</body>
</html>
includepage.jsp
<html>
<head>
<title>Include page</title>
</head>
<body>
<h1>Include page</h1>
</body>
</html>
forwardpage.jsp
<html>
<head>
<title>Forward page</title>
</head>
<body>
<h1>Forward page</h1>
</body>
</html>
7. Write a JAVA JSP Program which uses <jsp:plugin> tag to run a applet.
JavaApplet.java
import java.applet.Applet;
import java.awt.Graphics;
@Override
public void init()
{
setBackground (Color.yellow);
@Override
public void paint(Graphics g)
{
if(x1 > 300)
x1=0;
g.fillOval(x1, y1, 30, 30);
x1++;
try
{
Thread.Sleep(100);
}
catch(Exception e){}
repaint();
}
}
Select the index.jsp in the Lab10->Web Pages and Type the following
Index.jsp
Student.java
package std;
{
return sname;
}
View.jsp
Select the index.jsp in the Lab9->Web Pages and Type the following
Index.jsp
Stud.html
<!DOCTYPE html>
<html>
<head>
<title>Student Entry</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
Department of MCA, Bangalore Institute Of Technology Page 32
Advanced JAVA Programming Laboratory 13MCA47
<body>
<form action="index.jsp" method="post">
Enter Name:<input type="text" name="sname"/><br/>
Enter USN:<input type="text" name="usnno"/><br/>
Enter Branch:<input type="text" name="branch"/><br/>
<input type="submit" value="OK"/>
</form>
</body>
</html>
Right Click->Connect
->employee->Tables->Right Click->Create Table->Table Name:std
->Add Column
->Name:usn->Type:VARCHAR->Select CheckBoxes->Primary Key->Unique->OK
->Name:name->Type:VARCHAR->Size:20->OK
->Name:sem->Type:INT->OK
->OK
Student.java
package std;
import java.sql.*;
import java.util.Scanner;
try
{
PreparedStatement pst;
pst=con.prepareStatement("insert into std(usn,name,sem) values(?,?,?)");
pst.setString(1,u);
pst.setString(2,n);
pst.setInt(3,s);
int i=pst.executeUpdate();
if(i==1)
{
System.out.println("Record inserted successfully");
}
else
{
System.out.println("Invalid Insertion");
}
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
finally
{
con.close();
}
}
}
public void sSelect(String u)throws ClassNotFoundException, SQLException
{
estConnection();
if(con!=null)
{
try
{
PreparedStatement pst;
pst=con.prepareStatement("select * from std where usn=?");
pst.setString(1,u);
ResultSet res;
Department of MCA, Bangalore Institute Of Technology Page 36
Advanced JAVA Programming Laboratory 13MCA47
res=pst.executeQuery();
if(res.next())
{
System.out.println("USN="+res.getString(1)+"\tName="+res.getString(2)+"\tSem="+res.getInt(
3));
}
else
{
System.out.println("USN not found");
}
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
finally
{
con.close();
}
}
}
}
else
{
System.out.println("USN not found");
}
}
catch(SQLException e)
{
System.out.println(e.getMessage());
}
finally
{
con.close();
}
}
}
{
System.out.println(e.getMessage());
}
finally
{
con.close();
}
}
}
case 6: System.exit(0);
default: System.out.println("Invalid option");
}
}
}
}
directive.jsp
<html>
<body>
<h2>This Demonstrates Content type and language</h2>
<%@ page contentType="text/html" %>
<%@ page language="java" %>
<%=response.getContentType()%>
myerrorpage.jsp
<html>
<body>
<%@ page isErrorPage="true" %>
</body>
</html>
session.jsp
<html>
<body>
<body>
</html>
It Creates 2 Files
CircleEJBBean.java
CircleEJBBeanLocal.java
CircleEJBBean.java
package stateless;
import javax.ejb.Stateless;
@Stateless
public class CircleEJBBean implements CircleEJBBeanLocal
{
@Override
public Double area(double r)
{
return (3.14*r*r);
}
@Override
public Double circum(double r)
{
return (2*3.14*r);
}
}
CircleEJBBeanLocal.java
package stateless;
import javax.ejb.Local;
@Local
public interface CircleEJBBeanLocal
{
CircleServlet.java
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import stateless.CircleEJBBeanLocal;
out.close();
}
}
}
Select the index.jsp in the Lab12->Web Pages and Type the following
Index.jsp
JNDI Name:jms/dest
Physical Destination Name:Destination
Resource Type:javax.jms.Queue
->OK
Close Browser
Go to NetBeans
Select Lab12-ejb->Right Click->New->Other->Categories:Enterprise JavaBeans->File
Types:Message-Driven Bean->Next
EJB Name:Mymdb
Package:mdb
Server Destination:jms/dest
->Finish
Mymdb.java
package mdb;
import java.util.logging.Level;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.util.logging.Logger;
@Override
public void onMessage(Message message)
Department of MCA, Bangalore Institute Of Technology Page 55
Advanced JAVA Programming Laboratory 13MCA47
{
TextMessage tmsg=null;
tmsg=(TextMessage)message;
try
{
System.out.println(tmsg.getText());
}
catch(JMSException ex)
{
Logger.getLogger(Mymdb.class.getName()).log(Level.SEVERE, null,
ex);
}
}
}
mdbservlet.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
Department of MCA, Bangalore Institute Of Technology Page 56
Advanced JAVA Programming Laboratory 13MCA47
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
catch(JMSException ex)
{
Logger.getLogger(mdbservlet.class.getName()).log(Level.SEVERE,null,ex);
}
finally
{
out.close();
}
}
}
catch (JMSException e)
{
Logger.getLogger(this.getClass().getName()).log(Level.WARNING,
"Cannot close session", e);
}
}
if (connection != null)
{
connection.close();
}
}
}
}
Index.jsp
->Deploy
Select Lab12->Right Click->Clean and Build
->Deploy
->Run
Output:
Lab13
Lab13-ejb
Lab13-war
jdbc:mysql://localhost:3306/employee?zeroDateTimeBehavior=convertToNull [root on
Default schema]->Right Click->Connect
->employee->Tables->Right Click->Create Table->Table Name:Emp
Department of MCA, Bangalore Institute Of Technology Page 62
Advanced JAVA Programming Laboratory 13MCA47
Emp.java
package server;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
@Table(name = "emp")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Emp.findAll", query = "SELECT e FROM Emp e"),
@NamedQuery(name = "Emp.findById", query = "SELECT e FROM Emp e WHERE e.id =
:id"),
Department of MCA, Bangalore Institute Of Technology Page 63
Advanced JAVA Programming Laboratory 13MCA47
public Emp()
{
}
@Override
public int hashCode()
{
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object)
{
if (!(object instanceof Emp))
{
return false;
}
Emp other = (Emp) object;
if ((this.id == null && other.id != null) || (this.id != null &&
!this.id.equals(other.id)))
{
return false;
}
return true;
}
@Override
public String toString()
{
return "server.Emp[ id=" + id + " ]";
}
Select the AbstractFacade.java and Erase all methods in below create() method….
AbstractFacade.java
package server;
import java.util.List;
import javax.persistence.EntityManager;
Select the EmpFacadeLocal.java and Erase all methods in below create() method…
Name:addEmp
Return Type:void
Parameters->Add->Name:id->Type:int
->Add->Name:name->Type:String
->OK
EmpFacade.java
package server;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class EmpFacade extends AbstractFacade<Emp> implements EmpFacadeLocal
{
@PersistenceContext(unitName = "Lab13-ejbPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager()
{
return em;
}
public EmpFacade()
{
super(Emp.class);
}
@Override
public void addEmp(int id, String name)
{
Emp e=new Emp();
e.setId(id);
Department of MCA, Bangalore Institute Of Technology Page 67
Advanced JAVA Programming Laboratory 13MCA47
e.setName(name);
create(e);
}
}
EmpFacadeLocal.java
package server;
import javax.ejb.Local;
@Local
public interface EmpFacadeLocal
{
void create(Emp emp);
Select NewServlet.java->In the Source Code->In try block->In the Comment Line-
>Right Click->Insert Code->Call Enterprise Bean->select Lab13-ejb->in that Select
EmpFacade->Press OK
package servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
Department of MCA, Bangalore Institute Of Technology Page 68
Advanced JAVA Programming Laboratory 13MCA47
import javax.servlet.http.HttpServletResponse;
import server.EmpFacadeLocal;
}
}
Select the index.jsp in the Lab13-war->Web Pages and Type the following
Index.jsp