Thursday, 12 February 2015

14. An EJB application that demonstrates MDB.


 package mybeans;  
 import javax.ejb.ActivationConfigProperty;  
 import javax.ejb.MessageDriven;  
 import javax.jms.Message;  
 import javax.jms.MessageListener;  
 @MessageDriven(mappedName = "jms/helloserver", activationConfig = {  
   @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),  
   @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")  
 })  
 public class msgbean1 implements MessageListener {  
   public msgbean1() {  
   }  
   @Override  
   public void onMessage(Message message)   
   {  
    try{  
      System.out.println("HELLO "+ message.getStringProperty("name"));  
    }catch(Exception e){}  
   }  
 }  
 --------------------  
 //msgclient.java  
 import javax.annotation.Resource;  
 import javax.jms.*;  
 public class msgclient   
 {  
   @Resource(mappedName = "jms/helloserver")  
   private static Queue helloserver;  
   @Resource(mappedName = "jms/helloserverFactory")  
   private static ConnectionFactory helloserverFactory;  
   public static void main(String[] args) throws JMSException  
   {  
      Connection connection = helloserverFactory.createConnection();  
      Session  session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
      MessageProducer messageProducer = session.createProducer(helloserver);  
      Message message=session.createTextMessage();  
      message.setStringProperty("name","Students");  
      messageProducer.send(message);  
      System.exit(0);  
    }  
   }  
Read more...

13. An EJB application that demonstrates Entity Bean.


 index.jsp  
<html>
<body>
<form action="serv13" method="post">
Enter Discount Code <input type="text" name="dcode">
<input type="submit" value="Find Rate">
</form>
</body>
</html>
----------------------------------------------------
// serv13.java
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;
import serv.DiscountCodeFacade;
public class serv13 extends HttpServlet {
@EJB
private DiscountCodeFacade discountCodeFacade;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<body>");
out.println("<h1>Disount Rate :" + discountCodeFacade.find(request.getParameter("dcode").charAt(0)).getRate() + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
}
Read more...

12. An EJB application that demonstrates Session Bean


 //hellobean.java  
package abcd;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
@Stateless
@LocalBean
public class hellobean
{
public String sayhello(String nm)
{
return "Hello " + nm+ " From session bean";
}
}
----------------------------------------------
//serv12.java
package abcd;
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;
public class serv12 extends HttpServlet {
@EJB
private hellobean hellobean;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<body>");
out.println("<h1>"+hellobean.sayhello(request.getParameter("uname")) + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
}
---------------------------------------------
//index.jsp
<html>
<body>
<form action="serv12" method="post">
Enter name <input type="text" name="uname">
<input type="submit" value="Execute">
</form>
</body>
</html>
Read more...

11. Write a JAVA JSP Program which implements nested tags and also use TagSupport Class


 index.jsp  
<%@taglib prefix="d" uri="/WEB-INF/tlds/abcd.tld"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<d:tag1>
<c:out value="this is inner tag"></c:out>
</d:tag1>
</body>
</html>
--------------------------------
tag1.java
package mytag;
import java.io.*;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
public class tag1 extends TagSupport
{
public int dostartTag() throws JspException
{
try
{
pageContext.getOut().write("hello dis is inside customer tag class");
}
catch(IOException e)
{
throw new JspException("Error");
}
return EVAL_BODY_INCLUDE;
}
}
Read more...

10. Write a JAVA JSP Program which uses tag to run a applet.


 applet1.java  
import java.applet.*;
import java.awt.*;
public class applet1 extends Applet
{
public void init()
{
setBackground(Color.blue);
}
public void paint(Graphics g)
{
g.drawString("Welcome to Applet !!!! ", 50,50);
}
}
-------------------------------------------------------------
index.jsp
<html>
<head>
<title>Prgram 10 </title>
</head>
<body>
<h1> Welcome this is an applet demo </h1>
<jsp:plugin type="applet" code="applet1.class" height="100" width="400" codebase="http://localhost:41274/Prg10/">
<jsp:fallback> Unable to load applet </jsp:fallback>
</jsp:plugin>
</body>
</html>
Read more...

9. 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.jsp  
<html>
<head>Student Info Entry Form</head>
<body>
<form action ="jsp9.jsp" method = "post">
Registration Number : <input type = "text" name = "regno" />
<br>
Name : <input type = "text" name = "uname" />
<br>
Address :<input type = "text" name = "address" />
<br>
<input type ="submit" value ="Register" />
</form>
</body>
</html>
--------------------------------------------------------
//jsp9.jsp
<html>
<body>
<jsp:useBean id="std" scope="request" class="ourbeans.Student" />
<jsp:setProperty name="std" property="*"/>
<jsp:forward page="welcomeinfo.jsp"/>
</body>
</html>
--------------------------------------------------------
// welcomeinfo.jsp
<html>
<body>
<h1>Welcome Your Info </h1>
<jsp:useBean id="std" scope="request" class="ourbeans.Student"/>
User Name : <jsp:getProperty name="std" property="uname"/><br>
Registration Number : <jsp:getProperty name="std" property="regno"/><br>
Address : <%out.print(std.getaddress());%>
</body>
</html>
//Student.java
package ourbeans;
public class Student
{
public String uname;
public int regno;
public String address;
public Student(){}
public void setuname(String s)
{uname = s;}
public String getuname()
{return uname;}
public void setregno(int rg)
{regno=rg;}
public int getregno()
{return regno;}
public void setaddress(String ad)
{address = ad;}
public String getaddress()
{return address;}
}
Read more...

8b. Write a JAVA JSP Program to implement verification of a particular user login and display a welcome page.


 8b. Write a JAVA JSP Program to implement verification of a particular user login and display a welcome page.  
//index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form method="post" action="jsp8b.jsp" >
User name <input type="text" name="uname" />
Password <input type="password" name="pwd" />
<input type="submit" value="Login" />
</form>
</body>
</html>
//jsp8b.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String u=request.getParameter("uname");
String p=request.getParameter("pwd");
if ((u.equals("admin"))&& (p.equals("rose")))
out.println("Welcome "+u+" you are authenticated");
else
out.println("Failed Login Attempt");
%>
</body>
</html>
Read more...