Hi,
Jay SenSharma

Jay SenSharma

Many times we dont want to install a separate WebServer like (Apache,IIS or IPlanate) rather we want to configure and run a WebLogic Server itself as a WebLogic WebServer. Yes it is possible. In the following manner we can configure a WebLogic Server as a Frontend WebServer which is going to point a WebLogic Cluster. This can be achievend by deploying a simple WebApplication in the front end WebLogic ManagedServer to make it working it as a WebServer.

Here in this sample we are going to use WebLogic HttpClusterServlet application as a WebLogic WebServer. As described in the following Diagram e are going to use HttpClusterServlet…
Architecture Diagram:
HttpClusterServlet   Architecture Diagram

HttpClusterServlet Architecture Diagram

Step1). Create a WebLogic Domain with 1-AdminServer and 3 ManagedServers…And a Cluster like following:
Servers and Cluster Configuration

Servers and Cluster Configuration

Step2). We are going to develope a Simple HttpClusterServletWebApplication to make the “FrontEnd_MS” Server as a WebServer. So Create a Directory somewhere in your file system like following:
C:DemoProxyWebApp

Step3). Create a “WEB-INF” directory inside “C:DemoProxyWebApp” and then place the following “web.xml” file inside “C:DemoProxyWebAppWEB-INF”
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee">
<servlet>
<servlet-name>HttpClusterServlet</servlet-name>
<servlet-class>weblogic.servlet.proxy.HttpClusterServlet</servlet-class>
<init-param>
<param-name>WebLogicCluster</param-name>
<param-value>localhost:7003|localhost:7005</param-value>
</init-param>
<init-param>
<param-name>verbose</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>DebugConfigInfo</param-name>
<param-value>ON</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>HttpClusterServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>HttpClusterServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>HttpClusterServlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>HttpClusterServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
————————————————————————–
Step4). Now create a Simple “weblogic.xml” file inside “C:DemoProxyWebAppWEB-INF” like following:
<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://www.bea.com/ns/weblogic/90">
<context-root>/</context-root>
</weblogic-web-app>
————————————————————————–
Step5). Now deploy the Application “ProxyWebApp” on “FrontEnd_MS” WebLogic Server to make it as a WebServer…to redirect Client request to Cluster Members.

NOTE: S t e p – 6 ) is optional If already u have some application deployed on your Cluster then no need to develop a new Test Application…your Set up is ready till “s t e p 5)” you can skip other below Steps.

————————————————————————–
Step6). Now we will develope a Simple WebApplication to check if the Failover and Loadbalancing is happening or not. So Please create another WebApplication in the following Location:
C:DemoTestWebApp
————————————————————————–
Step7). provide the following “index.jsp” page inside “C:DemoTestWebApp”
<HTML>
<center>
<form action="ValidateServlet" method="POST">
<h3>Enter Your ID : <input type=text name=userid value=admin><BR>
<h3>Enter Password : <input type=password name=pass value="weblogic"><BR>
<input type=submit value="Login">
<input type=reset value="Clear">
</form>
</center>
<HR> NOTE:   Default password is "weblogic"
<%
System.out.println("nnt index.jsp called");
%>
</HTML>
————————————————————————–
Step8). Provide the following “logout.jsp” page inside “C:DemoTestWebApp”
<HTML>
<%
if(session!=null)
{
session.invalidate();
%>
<jsp:forward page="index.jsp" />
<%
} else{
%>
Logged Out Successfully....
<% }%>
</HTML>
————————————————————————–
Step9). Create a “WEB-INF” directory inside “C:DemoTestWebApp” location
.
Step10). Create “classes” directory inside “C:DemoTestWebAppWEB-INF” location
.
————————————————————————–
Step11). Now start writing the “ValidateServlet.java” program inside “C:DemoTestWebAppWEB-INFclasses” like following:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ValidateServlet extends HttpServlet
{
public void service(HttpServletRequest req , HttpServletResponse res)throws ServletException,IOException
{
PrintWriter out=res.getWriter();
out.println("<html>");
String id=req.getParameter("userid");
String pass=req.getParameter("pass");
if(id.equals("admin") && pass.equals("weblogic"))
{
HttpSession session=req.getSession(true);
System.out.println("nt ValidateServlet: SessionID: "+session.getId());
session.setAttribute("name","Jack");
RequestDispatcher rd=req.getRequestDispatcher("/AddServlet");
System.out.println("nnt ValidateServlet calling the requstDispatcher.forward(req,res)");;
out.println("<center><a href=AddServlet>Click Here to Add Two Numbers</a></center>");
}
else
{
RequestDispatcher rd=req.getRequestDispatcher("/SorryServlet");
req.setAttribute("msg","InValid User Please Try Again..");
rd.forward(req,res);
}
out.println("</html>");
}
}
————————————————————————–
Step12). Now start writing the “AddServlet.java” program inside “C:DemoTestWebAppWEB-INFclasses” like following:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class AddServlet extends HttpServlet
{
public void service(HttpServletRequest req , HttpServletResponse res)throws ServletException,IOException
{
PrintWriter out=res.getWriter();
out.println("<html>");
HttpSession session=req.getSession(false);
System.out.println("nt AddServlet : Session Exists? : "+session);
System.out.println("nt AddServlet : SessionID: "+session.getId());
// false=> If the Session Object for this client is already existing then Use the Same
// Otherwise   DON’T Create a new Session Object
if(session!=null)
{
String name=(String)session.getAttribute("name");
out.println("<body bgcolor=maroon text=white>");
out.println("<center><h1>Welcome Mr. "+name+"</h1>");
out.println("<form action=ResultServlet>");
out.println("<h3>Enter First Number : <input type=text name=fno><BR>");
out.println("<h3>Enter Second Number : <input type=text name=sno><BR>");
out.println("<input type=submit value=AddNow></form>");
out. println("<hr><a href=LogoutServlet>Want to Logout</a>");
}
else
{
RequestDispatcher rd=req.getRequestDispatcher("/SorryServlet");
req.setAttribute("msg","Sorry You cannot Access AddServlet Please login First...");
rd.forward(req,res);
}
out.println("</body></html>");
}
}
————————————————————————–
Step13). Now start writing the “ResultServlet.java” program inside “C:DemoTestWebAppWEB-INFclasses” like following:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ResultServlet extends HttpServlet
{
public void service(HttpServletRequest req , HttpServletResponse res)throws ServletException,IOException
{
PrintWriter out=res.getWriter();
out.println("<html>");
HttpSession session=req.getSession(false);
System.out.println("nt ResultServlet : Session Exists? : "+session);
System.out.println("nt ResultServlet  : SessionID: "+session.getId());
if(session!=null)
{
String name=(String)session.getAttribute("name");
out.println("<center><h1>Welcome Mr. "+name+"</h1>");
int a=Integer.parseInt(req.getParameter("fno"));
int b=Integer.parseInt(req.getParameter("sno"));
int sum=(a+b);
out.println("<h1>Sum of "+a+" And "+b+" = "+sum);
out. println("<hr><a href=LogoutServlet>Want to Logout</a>");
}
else
{
RequestDispatcher rd=req.getRequestDispatcher("/SorryServlet");
req.setAttribute("msg","Sorry You cannot Access ResultServlet Please login First...");
rd.forward(req,res);
}
out.println("</html>");
}
}
————————————————————————–
Step14). Now start writing the “LogoutServlet.java” program inside “C:DemoTestWebAppWEB-INFclasses” like following:
import javax.servlet.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class LogoutServlet extends HttpServlet
{
public void service(HttpServletRequest req , HttpServletResponse res)throws ServletException,IOException
{
PrintWriter out=res.getWriter();
out.println("<html>");
HttpSession session=req.getSession(false);
System.out.println("nt 1----LogoutServlet : Session Exists? : "+session);
System.out.println("nt LogoutServlet  : SessionID: "+session.getId());
if(session!=null)
{
String name=(String)session.getAttribute("name");
out.println("<center><h1>Welcome Mr. "+name+"</h1>");
session.invalidate();        // to destroy session object
out. println("<hr><a href=logout.jsp>Want to Login Again</a>");
}
else
{
RequestDispatcher rd=req.getRequestDispatcher("/SorryServlet");
req.setAttribute("msg","Sorry You cannot Logout Before Loggin In, Please login First…");
rd.forward(req,res);
}
System.out.println("nt 2----LogoutServlet : Session Exists? : "+session);
out.println("</html>");
}
}
————————————————————————–
Step15). Now start writing the “LogoutServlet.java” program inside “C:DemoTestWebAppWEB-INFclasses” like following:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class SorryServlet extends HttpServlet
{
public void service(HttpServletRequest req , HttpServletResponse res)throws ServletException,IOException
{
PrintWriter out=res.getWriter();
out.println("<html>");
String message=(String)req.getAttribute("msg");
out.println("<h1>"+message);
out.println("</html>");
}
}
————————————————————————–
Step 16). Now provide the following “web.xml” file inside “C:DemoTestWebAppWEB-INF” location
<!DOCTYPE web-app PUBLIC  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>one</servlet-name>
<servlet-class>ValidateServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>one</servlet-name>
<url-pattern>/ValidateServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>onea</servlet-name>
<servlet-class>AddServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>onea</servlet-name>
<url-pattern>/AddServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>oneb</servlet-name>
<servlet-class>ResultServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>oneb</servlet-name>
<url-pattern>/ResultServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>onec</servlet-name>
<servlet-class>LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>onec</servlet-name>
<url-pattern>/LogoutServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>oned</servlet-name>
<servlet-class>SorryServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>oned</servlet-name>
<url-pattern>/SorryServlet</url-pattern>
</servlet-mapping>
</web-app>
————————————————————————–
Step17). The Most Important Step to make the HttpSession as replicatable for our application….Please provide the following “weblogic.xml” file inside “C:DemoTestWebAppWEB-INF” :
<?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://www.bea.com/ns/weblogic/90">
<session-descriptor>
<persistent-store-type>replicated</persistent-store-type>
</session-descriptor>
</weblogic-web-app>
————————————————————————–
Step18). Open a command window and then run the “setWLSEnv.cmd” to set the environment in the current prompt and then move inside the “C:DemoTestWebAppWEB-INFclasses” directory in the same command prompt to compile the Servlet programs….like following:
Compiling_Servlets

Compiling_Servlets

Step19). Now deploy the Application “TestWebApp” from the following location “C:DemoTestWebApp” in the WebLogic Cluster (This Cluster contains 2 ManagedServers…MS1 and MS2 as we have seen in the architectur Diagram)
Deployed the Applications on The Secvers/Clusters

Deployed the Applications on The Secvers/Clusters

Step20). Now hit the Proxy Server to access the Clustered Application:
Hitting_The Application_Through_WebLogic_Proxy

Hitting_The Application_Through_WebLogic_Proxy

.
.
Thanks
Jay SenSharma