Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses
Showing posts with label Java EE 6. Show all posts
Showing posts with label Java EE 6. Show all posts

How to generate WAR file of a Web Application project in NetBeans IDE ? (Video Tutorial)



Video tutorial to demonstrate how to generate WAR file of a Web Application project in NetBeans IDE.






@WebFilter annotation for filtering Web Requests in Java EE

A simple Web application to demonstrate @WebFilter annotation for filtering request coming to web server.In this tutorial the filter is printing the remote host IP address on the console.

Click here to download complete source code 

package com.hubberspot.javaee.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

// @WebFilter annotation makes HostFilter class 
// a filter in Java EE application. /* tells
// annotation that filter each and every request
// coming to server. Here the filter prints IP 
// address of the request on the console.
@WebFilter("/*")
public class HostFilter implements Filter {

 private FilterConfig filterConfig;
 
 public void destroy() {
  System.out.println("Destroyed ... ");
 }

 public void doFilter(
   ServletRequest request, 
   ServletResponse response, 
   FilterChain chain) 
     throws IOException, ServletException {

  String ipAddress = request.getRemoteHost();
  System.out.println("Remote IP Address : " + ipAddress);
  
  // pass the request along the filter chain
  chain.doFilter(request, response);
 }

 public void init(FilterConfig fConfig) throws ServletException {
  this.filterConfig = fConfig;
 }

}




Output of the program : 


 


Video tutorial to demonstrate @WebFilter annotation for filtering request coming to web server.In this tutorial the filter is printing the remote host IP address on the console. 




Using annotation







How to use @WebInitParam annotation for setting initialization parameters in Servlet ?.

A simple HelloWorldServlet demonstrating usage of @WebInitParam annotation for setting initialization parameters.

package com.hubberspot.javaee;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// @WebServlet annotation has a initParams field which takes
// in initialization parameters for a servlet.
// @WebInitParam annotation takes in a name and value for the
// initialization parameters for the current Servlet.

@WebServlet(name = "HelloWorldServlet" , urlPatterns = { "/HelloWorldServlet" }
    , initParams = { @WebInitParam(name = "user" , value = "Jonty") })
public class HelloWorldServlet extends HttpServlet {

 protected void doGet(
   HttpServletRequest request, 
   HttpServletResponse response
   ) throws ServletException, IOException {

  response.setContentType("text/html");

  PrintWriter out = response.getWriter();

  try {
   out.println("<html>");
   out.println("<body>");
   out.println("<h2>Hello " 
      + getServletConfig().getInitParameter("user") 
      + "</h2>");
   out.println("</body>");
   out.println("</html>");
  } finally {
   out.close();
  }

 }

}




Output of the program : 


 

How to determine active users / sessions in a Java Web Application ?.

A simple tutorial to demonstrate how to determine active users / sessions in a Java Web Application.

package com.hubberspot.javaee.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class OnlineUsersCounter implements HttpSessionListener {

 private static int numberOfUsersOnline;
 
 public OnlineUsersCounter() {
  numberOfUsersOnline = 0;
 }
 
 public static int getNumberOfUsersOnline() { 
  return numberOfUsersOnline;
 }
 
    public void sessionCreated(HttpSessionEvent event) {

     System.out.println("Session created by Id : " + event.getSession().getId());
     synchronized (this) {
   numberOfUsersOnline++;
  }
     
    }

    public void sessionDestroyed(HttpSessionEvent event) {
     
     System.out.println("Session destroyed by Id : " + event.getSession().getId());
     synchronized (this) {
   numberOfUsersOnline--;
  }

    }
 
}



Running the below servlet on three different browsers will provide output as : (see fig below)

package com.hubberspot.javaee;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hubberspot.javaee.listener.OnlineUsersCounter;

// @WebServlet annotation has a initParams field which takes
// in initialization parameters for a servlet.
// @WebInitParam annotation takes in a name and value for the
// initialization parameters for the current Servlet.

@WebServlet(name = "HelloWorldServlet" , urlPatterns = { "/HelloWorldServlet" }
    , initParams = { @WebInitParam(name = "user" , value = "Jonty") })
public class HelloWorldServlet extends HttpServlet {

 protected void doGet(
   HttpServletRequest request, 
   HttpServletResponse response
   ) throws ServletException, IOException {

  response.setContentType("text/html");

  PrintWriter out = response.getWriter();
  
  // sessionCreated method gets executed
  HttpSession session = request.getSession();
  
  session.setMaxInactiveInterval(60);
  
  try {
   out.println("<html>");
   out.println("<body>");
   out.println("<h2>Number of Users Online : " 
      + OnlineUsersCounter.getNumberOfUsersOnline() 
      + "</h2>");
   out.println("</body>");
   out.println("</html>");
  } finally {
   out.close();
  }
        
 }

}



Output of the program : 

1. Eclipse Browser
























2. Firefox Browser



3. Internet Explorer Browser

 
 4. Console Output


 

How to create a Listener using @WebListener annotation ( ServletContextListener ) in Java EE application ?.

Program to demonstrate creation of a ServletContextListener using @WebListener annotation.

package com.hubberspot.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

// @WebListener annotation informs container that
// this class is a web listener which will listen to
// various events happening during lifecycle of application
// Here this class listens to StartUp and ShutDown of the
// application.

// We make class implements ServletContextListener which has two
// methods contextInitialized() and contextDestroyed() , which
// are called by the container whenever a servlet context is 
// started or shutdown

@WebListener
public class StartStopAppListener implements ServletContextListener {

 public void contextInitialized(ServletContextEvent servletContextEvent) {

  System.out.println("Servlet Context Initialized ... ");

 }

 public void contextDestroyed(ServletContextEvent servletContextEvent) {

  System.out.println("Servlet Context Destroyed ... ");

 }

}




Output of the program : 


 

HttpSessionAttributeListener example using @WebListener annotation.

Program to demonstrate how to apply listener to Session attributes in Java EE

package com.hubberspot.javaee.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

// @WebListener annotation informs container that
// this class is a web listener which will listen to
// various events happening during lifecycle of application
// Here this class listens to changes made to HttpSession 
// attributes.

// We make class implements HttpSessionAttributeListener
// which has three methods : attributeRemoved(), attributeAdded() 
// and attributeReplaced() , which are called by the container
// whenever attributes are added, removed, or replaced 
// within the HTTP session.

@WebListener
public class MySessionAttributeListener implements HttpSessionAttributeListener {


    public void attributeRemoved(HttpSessionBindingEvent event) {

     System.out.println("Method called when HttpSession attribute removed :");
     HttpSession session = event.getSession();     
     System.out.println("Session ID : " + session.getId());
     System.out.println("Session Name : " + event.getName());
     System.out.println("Session Value : " + event.getValue());
     
    }

    public void attributeAdded(HttpSessionBindingEvent event) {

     System.out.println("Method called when HttpSession attribute added :");
     HttpSession session = event.getSession();     
     System.out.println("Session ID : " + session.getId());
     System.out.println("Session Name : " + event.getName());
     System.out.println("Session Value : " + event.getValue());
     
    }

    public void attributeReplaced(HttpSessionBindingEvent event) {

     System.out.println("Method called when HttpSession attribute replaced :");
     HttpSession session = event.getSession();     
     System.out.println("Session ID : " + session.getId());
     System.out.println("Session Name : " + event.getName());
     System.out.println("Session Value : " + event.getValue());
     
    }
 
}


Run HelloWorldServlet.java and methods mentioned in above listener will be called by the container.


package com.hubberspot.javaee;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

// @WebServlet annotation has a initParams field which takes
// in initialization parameters for a servlet.
// @WebInitParam annotation takes in a name and value for the
// initialization parameters for the current Servlet.

@WebServlet(name = "HelloWorldServlet" , urlPatterns = { "/HelloWorldServlet" }
    , initParams = { @WebInitParam(name = "user" , value = "Jonty") })
public class HelloWorldServlet extends HttpServlet {

 protected void doGet(
   HttpServletRequest request, 
   HttpServletResponse response
   ) throws ServletException, IOException {

  response.setContentType("text/html");

  PrintWriter out = response.getWriter();
  
  HttpSession session = request.getSession();
  
  // attributeAdded method gets executed
  session.setAttribute("user", "Jonty");
  
  // attributeReplaced method gets executed
  session.setAttribute("user", "Dinesh");
  
  // attributeRemoved method gets executed
                session.removeAttribute("user");

 }

}


Output of the program : 


 

Using @WebServlet annotation to register Servlets without using web.xml

With the introduction of Annotations in Java, now its easy to remove xml configuration files with annotations. Registering each and every servlet in web.xml is quite hectic task. In order to avoid this, @WebServlet annotation is used. This post creates a simple Servlet named as HelloWorldServlet without making any registry in web.xml file.
@WebServlet annotation is used over the class level.  

package com.hubberspot.javaee;

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;

@WebServlet(name = "HelloWorldServlet" , urlPatterns = { "/HelloWorldServlet" })
public class HelloWorldServlet extends HttpServlet {

 protected void doGet(
   HttpServletRequest request, 
   HttpServletResponse response
   ) throws ServletException, IOException {

  response.setContentType("text/html");

  PrintWriter out = response.getWriter();

  try {
   out.println("<html>");
   out.println("<body>");
   out.println("<h2>Hello World !!!</h2>");
   out.println("</body>");
   out.println("</html>");
  } finally {
   out.close();
  }

 }

}



Here in above HelloWorldServlet @WebServlet(name = "HelloWorldServlet" , urlPatterns = { "/HelloWorldServlet" }) is equivalent to
<servlet>
    <servlet-name>HelloWorldServlet</servlet-name>
    <servlet-class>com.hubberspot.javaee.HelloWorldServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>HelloWorldServlet</servlet-name>
    <url-pattern>/HelloWorldServlet</url-pattern>
</servlet-mapping>



Output of the program : 


 

Java EE

  1. Java Server Pages (JSP) : Advantages over Servlets and other technologies
  2. How to create simple Hello World Servlet application in Java ?
  3. What is a Web Application in Java programming language?
  4. A Simple HTML and JSP Email Subscription List Application
  5. How to get real path for a file in JSP and Servlet ?.
  6. How to use various types of JSP tags in a JSP page ?
  7. How to set and get an attribute to/from request object in JSP and Servlets ?
  8. How to redirect responses to a different JSP or Servlets ?.
  9. How to add and retrieve Sessions in JSP and Servlets using Session Management API ?
  10. How to destroy a session in JSP and Servlets using Session Management API ?
  11. How to expire and destroy Cookies in JSP and Servlets ?
  12. A simple application demonstrating Request, Session, Context differences and usages in JSP and Servlets
  13. How to check whether which Web-Browser have been used to run JSP and Servlets ?.
  14. How to add and retrieve Sessions in JSP and Servlets using Session Management API ?
  15. How to destroy a session in JSP and Servlets using Session Management API ?
  16. How to perform create sql query in JSP and Servlets using JDBC ?
  17. How to perform insert sql query in JSP and Servlets using JDBC ?
  18. How to perform update sql query in JSP and Servlets using JDBC ?
  19. How to create a Filter that adds url and time of request to a database in JSP and Servlets ?.
  20. How to perform delete sql query in JSP and Servlets using JDBC ?
  21. How to create error page in a JSP and Servlet to handle exceptions ?
  22. How to use filters in JSP and Servlets for logging information in ServletContext logs ?
  23. How to include JSP page dynamically into another JSP page?
  24. How to display HTTP Request Headers through a Servlet ?.
  25. How to display Request url Information through a Servlet ?
  26. How to display request parameters in a JSP coming in a request ?.
  27. How to use model / Java / Pojo classes with JSP and Servlets ?.
  28. How to forward a request to a JSP using RequestDispatcher ?.
  29. How to do Session Management in JSP and Servlets using its methods and API ?.
  30. How to forward a request from one Jsp to another Jsp ?.
  31. How to include one JSP into another JSP ?.
  32. How to use Jsp action elements useBean, setProperty and getProperty to add, set and get property of a bean in a JSP page ?.
  33. How to decorate form in a JSP using fieldset and legend tag ?.
  34. How to write a Servlet code to download a Jar or file from the Server at a specified location ?.
  35. How to use Servlets Initialization Parameters through ServletConfig object in Java EE Application ?.
  36. How to use Context Parameters in Servlets through ServletContext object in Java EE Application ?.
  37. "Online Tweeter Enterprise Application" : Creating a JSP page in NetBeans Web module - Part 6
  38. "Online Tweeter Enterprise Application" : Creating a Servlet named DisplayTweets in NetBeans Web module - Part 9
  39. How to use Arithmetic Operations in Expression Language ( el ) in a simple jsp page ?.
  40. "Online Tweeter Enterprise Application" : Creating a Servlet named TweetsSubmit in NetBeans Web module - Part 8
  41. How to use param implicit object in a JSP page ?. 
  42. "Online Tweeter Enterprise Application" : Creating an Enterprise Application with EJB 3.1 in Netbeans - Part 0
  43. "Online Tweeter Enterprise Application" : Creating an Enterprise Application Project in NetBeans - Part 1
  44. "Online Tweeter Enterprise Application" : Creating a Persistence Unit in NetBeans - Part 2
  45. "Online Tweeter Enterprise Application" : Creating an Entity Class in NetBeans EJB module - Part 3
  46. "Online Tweeter Enterprise Application" : Creating a Message-Driven Bean in NetBeans EJB module - Part 4
  47. "Online Tweeter Enterprise Application" : Creating a Stateless Session Bean in NetBeans EJB module - Part 5
  48. "Online Tweeter Enterprise Application" : Creating a JSP page in NetBeans Web module - Part 6
  49. "Online Tweeter Enterprise Application" : Creating a Singleton Session Bean in NetBeans EJB module - Part 7
  50. "Online Tweeter Enterprise Application" : Creating a Servlet named TweetsSubmit in NetBeans Web module - Part 8
  51. "Online Tweeter Enterprise Application" : Creating a Servlet named DisplayTweets in NetBeans Web module - Part 9
  52. "Online Tweeter Enterprise Application" : Building and Running the Enterprise Application - Part 10
  53.  


How to Create Your First Java Server Faces (JSF) Web Application ?.

In this tutorial, we will show you how to develop a simple JavaServer Faces (JSF) 2.0 application called as "WelcomeJSF" in NetBeans 7.3. In order to demonstrate development of this application we begin with:

1. Create a Web Application named as "WelcomeJSF" in NetBeans.
2. Create a simple JSF index form as index.xhtml
3. Create JSF page named as welcome.xhtml
4. Create a Managed Bean named as Welcome.java
5. Overview of Deployment Descriptor file named as web.xml
6. Build and Run the web application.

Create a Web Application named as "WelcomeJSF" in NetBeans.

Step 1 :- Open NetBeans IDE (See fig below)



and Select in the menu bar File ---> New Project or press Ctrl + Shift + N. (See fig below)


New Project dialog box gets open.

Step 2 :- Under Categories: select Java Web.
Step 3 :- Under Projects: select Web Application.
Step 4 :- Click Next > . (See fig below)































New Web Application dialog box gets open.

Step 5 :- Under Name and Location tab, enter Project Name: as "WelcomeJSF".
Step 6 :- Click Next > . (See fig below)






























Under the same New Web Application, Server and Settings dialog box gets open.

Step 7:- Choose Server: as "GlassFish Server 4.0". You can also choose "GlassFish v3 Domain" as server if you are using old version of NetBeans other than 7.3.1, which comes bundled with "GlassFish v3 Domain".

Step 8:- Choose Java EE 6 Web as Java EE Version:. Keep rest as default. 
Step 9:- Click Next >. (See fig below)






























Under the same New Web Application, Frameworks dialog box gets open.

Step 10:- Choose JavaServer Faces in the Frameworks section.
Step 11:- Under the Libraries tab, select the server default libraries (JSF 2.2 or JSF 2.0) which comes up with server.
Step 12:- Click Configuration tab. (See fig below)






























Step 13:- Under Configuration tab, provide JSF Servlet URL Pattern: as "/faces/*" and Preferred Page Language: as "Facelets".
Step 14:- Click Finish. (See fig below)



Following package structure for the web application gets created




















Create a simple JSF index form as index.xhtml

After creation of WelcomeJSF web application, index.xhtml gets created automatically by NetBeans IDE. Modifying the xhtml page with following code.

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Index Page !!!</title>
    </h:head>
    <h:body>
        <h:form>
            <h1>The Welcome Index Form !!!.</h1>
            <h:panelGrid columns="2" cellspacing="1" cellpadding="1">
                <h:panelGroup>
                    <h:outputLabel for="firstName">
                        <h:outputText value="FirstName: " />
                    </h:outputLabel>
                </h:panelGroup>
                <h:panelGroup>
                    <h:inputText id="firstName" value="#{welcome.firstName}"/>
                </h:panelGroup>

                <h:panelGroup>
                    <h:outputLabel for="lastName">
                        <h:outputText value="LastName: " />
                    </h:outputLabel>
                </h:panelGroup>
                <h:panelGroup>
                    <h:inputText id="lastName" value="#{welcome.lastName}"/>
                </h:panelGroup>
                
                <h:panelGroup>
                    <h:commandButton id="submit" value="Submit" 
action="#{welcome.welcomeUser}" />
                </h:panelGroup>
            </h:panelGrid>
        </h:form>
    </h:body>
</html>


In order to use JSF 2.0 features, JSF Namespaces are declared at the top of JSF page.


































Step 1:- h:panelGrid represents a table having rows and columns.
Step 2:- h:panelGroup represents rows in a table. h:outputLabel represents a label displaying a value as a label through h:outputText on JSF page. h:inputText represents a component that takes in a value entered by the user."#{welcome.firstName}" represents JSF expression language, where JSF finds a managed bean by the name "welcome" and assign the value of h:inputText to firstName property of the managed bean.
Step 3:- h:commandButton renders out to a button having value specified by value attribute and action attribute evaluates to a string value. This value tells which page to display when submit button is clicked. Here string value is evaluated by calling "welcomeUser()" method present in the managed bean having name as "welcome".


Create JSF page named as welcome.xhtml

Step 1:- Click Web Pages directory, do a right click and browse from New ---> Other. (See fig below)



































 
  
Step 2:- On clicking Other, New File dialog box gets open. Select JavaServer Faces in the Categories: tab.
Step 3:- Choose JSF Page under the File Types: section.
Step 4:- Click Next >. (See fig below)





 
Step 5:- New JSF Page dialog box gets open. Provide File Name: value as "welcome" in order to create welcome.xhtml page. 
Step 6:- Select "Facelets" in the Options: box.
Step 7:- Click Finish. welcome.xhtml page gets created. (See fig below)































Modify welcome.xhtml with the following code.


<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
    
    <h:head>
        <title>Welcome !!!</title>
    </h:head>
    <h:body>
        <h:form><h1>
            <h:outputText id="welcomeUser"
                value="Welcome #{welcome.firstName} #{welcome.lastName} to JSF !!!."/>
        </h1></h:form>
    </h:body>
</html>


Create a Managed Bean named as Welcome.java

Step 1:- Click Source Packages directory, do a right click and browse from New ---> Other. (See fig below)  

































Step 2:- On clicking Other, New File dialog box gets open. Select JavaServer Faces in the Categories: tab.
Step 3:- Choose JSF Managed Bean under the File Types: section.
Step 4:- Click Next >. (See fig below)



Step 5:- New JSF Managed Bean dialog box gets open. Enter Class Name: value as Welcome in order to create a Managed Bean "Welcome.java".
Step 6:- Provide a package name here its "com.hubberspot.jsf".
Step 7:- Give a name to the Managed Bean say "welcome". It is been referred by this name into JSF expression language.
Step 8:- Provide a session scope to this Managed Bean.
Step 9:- Click Finish. (See fig below)

































Modify Welcome.java with the following code.

package com.hubberspot.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class Welcome {

    // firstName to store value entered in index.xhtml
    private String firstName;
    
    // lastName to store value entered in index.xhtml
    private String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    
    // method called when user clicks submit button in index.xhtml
    // It returns String as welcome. Based on this String flow gets
    // forwarded to welcome.xhtml
    public String welcomeUser() {
        return "welcome";
    }    
}


Overview of Deployment Descriptor file named as web.xml

Step 1:- Context Parameters with the name as "javax.faces.PROJECT_STAGE" and value as "Development" is provided. It tells that application is in Development phase and it provides debugging facilities.
Step 2:- Servlet and Servlet Mapping is provided in the web.xml for the Faces Servlet. Every request with url pattern as "/faces/*" is forwarded to JSF's Faces Servlet.
Step 3:- It defines a welcome file that points to "faces/index.xhtml". (See fig below)




Complete web.xml is provided below : 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
               http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    
    <servlet>        
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>


Final Package Structure for the web application




Build and Run the web application.

Step 1:- Right click "WelcomeJSF" directory and click "Build".
(See fig below)




Step 2:- Right click "WelcomeJSF" directory and click "Run". (See fig below)






Demo for the Working of the JSF web application

After running the application browse to following link in the browser as : "http://localhost:8080/WelcomeJSF/faces/index.xhtml"

Step 1:- Enter FirstName value in the textfield.
Step 2:- Enter LastName value in the textfield.
Step 3:- Click Submit button. (See fig below)





Step 4 :- On clicking Submit the values of text fields are passed to Managed Bean by name "welcome". It calls welcomeUser() method as a action present in the Managed Bean. On returning "welcome" string it forwards the request to welcome.xhtml. Thus renders output with welcoming the User with its FirstName and LastName.


 






Video tutorial to demonstrate how to create your first Java Server Faces (JSF) Framework Web Application.






 
© 2021 Learn Java by Examples Template by Hubberspot