Labmanual J2 Ee
Labmanual J2 Ee
Labmanual J2 Ee
1. Write a JAVA Servlet Program to implement a dynamic HTML using Servlet (user name and
Password should be accepted using HTML and displayed using a Servlet).
Project Structure
Index.html
<html>
<head><title>login</title> </head>
<body>
<form action="loginServlet" method="get">
<fieldset>
<legend>Login Form</legend>
Username:<input type="text" name="username"/><br/></br>
Password: <input type="password" name="pass"/><br/></br>
<input type="submit" value="send"/>
<input type="reset" value="clear"/>
</fieldset>
</form>
</body>
</html>
loginServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
String name=request.getParameter("username");
String secretword=request.getParameter("pass");
out.println("<html>");
out.println("<head><title>Servlet</title></head>");
out.println("<body>");
out.println("<fieldset>");
out.println("<h2>You have Entered</h2>");
out.println("<p>Name: " + name + "</p>");
out.println("<p> Password: " +secretword+"</p>");
out.println("</fieldset>");
out.println("</body>");
out.println("</html>");
}
}
output:
Project Structure
Refresh.java
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
response.addHeader("Refresh", "3");
output
3. Write a java servlet program to implement and demonstrate get() and post() methods
Project structure
Index.html
<html>
<head>
<title>TODO supply a title</title> </head>
<body>
<form action="loginServ" method="get">
<h2> Using Get method</h2>
<h3> UserName: <input type="text" name="uname"/></h3>
<h3> Password:<input type="password" name="pwd"/></h3>
<input type="submit" value="get()"/>
<input type="reset">
<hr>
</form>
loginServ.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head><title>Servlet loginServ</title></head>");
out.println("<body>");
out.println("<h1>Details of Get()</h1>");
out.println("<h1>username: " + uname+ "</h1>");
out.println("<h1>password: " + pwd+ "</h1>");
out.println("</body></html>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
out.println("<h1>Details of Post()</h1>");
out.println("<h1>username: " + uname1+ "</h1>");
out.println("<h1>password: " + pwd1+ "</h1>");
out.println("</body>");
out.println("</html>");
}
Output
Get request
Post request
Index.html
<html>
<head>
<title>cookie example</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<form method="post" action="Validate">
<fieldset>
<h2> Setting cookie </h2>
<h3>Name: <input type="text" name="user" /></h3>
<h3>Password:<input type="password" name="pass" /></h3>
<input type="submit" value="submit">
</fieldset>
</form>
</body>
</html>
Validate.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
if(pass.equals("1234"))
{
Cookie ck = new Cookie("username",name);
response.addCookie(ck);
response.sendRedirect("First");
}
}
First.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
Output:
5. Write a JAVA Servlet program to track HttpSession by accepting user name and password using
HTML and display the profile page on successful login.
Index.html
<html>
<head>
<title>setting session</title>
</head>
<body>
<form method="post" action="Validate">
<fieldset>
<h2> Setting session </h2>
<h3>Name: <input type="text" name="user" /></h3>
<h3>Password:<input type="password" name="pass" /></h3>
<input type="submit" value="submit">
</fieldset>
</form>
</body>
</html>
Validate.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
if(pass.equals("1234"))
{
//creating a session
HttpSession session = request.getSession();
session.setAttribute("user", name);
response.sendRedirect("Welcome");
}
}
}
Welcome.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
// getting session
HttpSession session = request.getSession();
String user = (String)session.getAttribute("user");
out.println("Hello "+user);
}
}
Output
6. Write a JAVA JSP Program which uses jsp:include and jsp:forward action to display a Webpage.
Project Structure:
Index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title></head>
<body>
<form action="Homepage.jsp" method="post">
<h2> Enter your birth date </h2>
Date: <input type="text" name="day"/>
<input type="submit" value="submit"/>
<input type="reset" value="clear"/>
</form>
</body>
</html>
Homepage.jsp
<%@page import="java.util.Date"%>
<%@page import="java.util.GregorianCalendar"%>
<%@page import="java.util.Calendar"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Homepage</title>
</head>
<body>
<h1>Happy Birthday</h1>
<%
Date date = new Date();
int udate = Integer.parseInt(request.getParameter("day"));
%>
<%if(udate==(date.getDate())){ %>
<jsp:include page="date.jsp"/>
<%}else{ %>
<jsp:forward page="date.jsp"/>
<%}%>
</body> </html>
date.jsp
output
7. Write a JAVA JSP program which uses <jsp:pulgin> tag to run an applet.
Project Structure
Index.jsp
MyApp.java
package mypack;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
Output:
8. Write a JAVA JSP Program to get student information through a HTML and create a JAVA Bean
class, populate Bean and display the same information through another JSP
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Student Info</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<form action="userdetails.jsp" method="post">
<center><h2> Enter the student details </h2>
<p> Name: <input type="text" name="name"><br> </p>
<p>USN: <input type="text" name="usn"><br> </p>
<p>Branch: <input type="text" name="branch"><br></p>
<input type="submit" value="register"> </center>
</form>
</body>
</html>
userdetails.jsp
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<jsp:useBean id="userinfo" class="mypack.Details">
</jsp:useBean>
Details.java
package mypack;
import java.io.Serializable;
public class Details implements Serializable{
}
Output
9. Write a JSP program to implement all the attributes of page directive tag.
Index.html
<html>
<head>
<title>page attribute</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<h2>Read two value to divide</h2>
<form action="lab09.jsp">
Enter First Value:<input type="text" name="val1"/><br>
Enter Second Value:<input type="text" name="val2"/><br>
<input type="submit" value="Calculate"/>
</form>
</body>
</html>
Lab09.jsp
<%@page import="java.util.Date"
contentType="text/html"
pageEncoding="UTF-8"
session="true"
buffer="16kb"
autoFlush="true"
isThreadSafe="true"
isELIgnored="false"
extends="org.apache.jasper.runtime.HttpJspBase"
info="Lab 10: demo of all page directive" language="java"
errorPage="showerror.jsp"
%>
<!DOCTYPE html>
<html>
<head>
<%
String str1=request.getParameter("val1");
String str2=request.getParameter("val2");
a=Integer.parseInt(str1);
b=Integer.parseInt(str2);%>
showerror.jsp
<%@page isErrorPage="true"%>
Output:
10. Write a JAVA Program to insert data into Student DATA BASE and retrieve info based on
particular queries(For example update, delete, search etc…)
import java.sql.*;
import java.util.Scanner;
public class Student
{
Connection con;
public void establishConnection()throws ClassNotFoundException,SQLException
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/studentlogin","root","rnsit");
}
public void sInsert(String usn, String name, String dept)throws ClassNotFoundException, SQLException
{
PreparedStatement pst=null;
establishConnection();
try
{
if(con!=null)
{
pst=con.prepareStatement("insert into student values(?,?,?)");
pst.setString(1, usn);
pst.setString(2,name);
pst.setString(3,dept);
int i=pst.executeUpdate();
if(i==1)
{
System.out.println("Record inserted successfully");
}}}
catch(SQLException e)
{
System.err.println(e.getMessage());
}
finally
{
pst.close();
con.close();
}}
public void sSelect(String usn)throws ClassNotFoundException,SQLException
{
PreparedStatement pst=null;
ResultSet res;
establishConnection();
try
{
if(con!=null)
{
pst=con.prepareStatement("select * from student where usn=?");
pst.setString(1, usn);
res=pst.executeQuery();
if(res.next())
{
System.out.println("USN= "+res.getString(1)+"\tName="+ res.getString(2)+ "\tDepartment=
"+res.getString(3));
} }}
finally
{
pst.close();
con.close();
} }
public void sUpdate(String usn, String name, String dept)throws ClassNotFoundException, SQLException
{
PreparedStatement pst=null;
establishConnection();
try
{
if(con!=null)
{
pst=con.prepareStatement("update student set name=?,dept=? where usn=?");
pst.setString(1, name);
pst.setString(2,dept);
pst.setString(3,usn);
int i=pst.executeUpdate();
System.out.println(i);
if(i==1)
{
System.out.println("Record updated successfully");
} } }
finally
{
pst.close();
con.close();
}}
public void sDelete(String usn)throws ClassNotFoundException, SQLException
{
PreparedStatement pst=null;
establishConnection();
try
{
if(con!=null)
{
{
System.out.println("Operations on Student table");
System.out.println("1.Insert\n2.Select\n3.Update\n4.Delete\n5.View All\n6.Exit");
System.out.println("select the operation");
switch(sc.nextInt())
{
case 1: System.out.println("Enter USN to insert");
usn=sc.next();
System.out.println("Enter Name to insert");
name=sc.next();
System.out.println("Enter Deaprtment to insert");
dept=sc.next();
std.sInsert(usn, name, dept);
break;
case 5: std.viewAll();
break;
case 6: System.exit(0);
default: System.out.println("Invalid operation");
break;
}
}
}
}
SessionB.java
package com;
import javax.ejb.Stateless;
@Stateless
public class SessionB implements SessionBLocal {
@Override
public int Square(int side) {
return side*side;
}
SessionBLocal
package com;
import javax.ejb.Local;
@Local
public interface SessionBLocal {
int Square(int side);
}
index.html
<!DOCTYPE html>
<html>
<head>
<title>lab11</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<form action="SessionServlet">
Enter the number:
<input type="text" name="num"/><br>
<input type="submit"/>
</form>
</body>
</html>
SessionServlet.java
package com;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
int i, result;
i=Integer.parseInt(request.getParameter("num"));
result=sessionB.Square(i);
out.println("the result "+result);
}
}
Output:
MessageB.java
package com;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
public MessageB() {
}
@Override
public void onMessage(Message message) {
TextMessage t=null;
t=(TextMessage) message;
try {
System.out.println(t.getText());
} catch (JMSException ex) {
Logger.getLogger(MessageB.class.getName()).log(Level.SEVERE, null, ex);
}
}
index.jsp
</body>
</html>
MDB.java
package com;
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;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
String m=request.getParameter("msg");
try {
sendJMSMessageToDestlab12(m);
} catch (JMSException ex) {
Logger.getLogger(MDB.class.getName()).log(Level.SEVERE, null, ex);
}
out.println("check output in server log");
if (session != null) {
try {
session.close();
} catch (JMSException e) {
Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot
close session", e);
}
}
if (connection != null) {
connection.close();
}
}
}
Output
1. Right Click on JMS
Select clean and build
13. An EJB application that demonstrates persistence (with appropriate business logic).
Project Structure
EMP1Facade.java
package com;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class Emp1Facade extends AbstractFacade<Emp1> implements Emp1FacadeLocal {
@PersistenceContext(unitName = "lab13-ejbPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public Emp1Facade() {
super(Emp1.class);
}
Emp1FacadeLocal.java
package com;
import java.util.List;
import javax.ejb.Local;
@Local
public interface Emp1FacadeLocal {
List<Emp1> findAll();
int count();
void addEmp(String eid, String ename);
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="EmpServlet" method="GET">
<b>Employee Form</b><br/><br/>
Emp Id: <input type="text" name="id"/><br/>
Name: <input type="text" name="name"/><br/>
<input type="submit" value="Add"/>
</form>
</body>
</html>
EmpServlet.java
package com;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
}
}
Output:
1.Clean and Build
2. Deploy
3. Run