Srikanth Technologies

Using JQuery with JSF

In this blog, I show how to use JQuery in JSF to make an AJAX request. Assume, we need to take username entered in inputText element of JSF form and check whether the username is unique in USERS table of Oracle database. For this we can use JQuery to make an AJAX request to checkuser.jsp, which checks for uniqueness of username and sends a message back to client. We then display the message sent from checkuser.jsp in a span item on the right of inputText component.

I assume you know how to use JQuery. In case if you are new to JQuery, please read my blog on how to use JQuery.

Follow the steps given below to integrate JSF with JQuery to make AJAX request.

Creating a new web project with JSF support

Take the following steps to create a new web application using NetBeans.

  1. Start NetBeans.
  2. Select File->New Project.
  3. Select Java Web in Categories and select Web Application in Projects.
  4. Enter JSFJQueryDemo as the name of the project and specify the folder where you want the project to be created.
  5. Select Apache Tomcat as the server. You can even select Glassfish.
  6. In Frameworks window, select JavaServer Faces as the framework.
  7. Click on Finish.

NetBeans creates a new web application with JSF 1.2 and JSTL libraries included. It also provides required JSF entries in web.xml (configuration of FacesServlet) and faces-config.xml .

Add OJDBC14.JAR, which contains Oracle Driver, to project using Libraries node in Project window.

Adding JQuery to project

Take the following steps to download and add JQuery library to your web application in NetBeans.

  1. Download JQuery from The latest version as of now is 1.4.2.
  2. Place jquery-1.4.2.js file in Web Pages folder of your project and you are ready to use JQuery.


The following is register.jsp, which takes required information from user regarding registration. For this example, we focus only on validation of username and not the actual registration process.

The most important thing is to know how to access JSF component from JQuery. The id given to inputText is consisting of formid:componentid. So in this example the id given to textbox is  registerform:username. But the presence of : (colon) causes problem to JQuery. So, we need to escape : (colon) using two \\ characters before colon - registerform\\:username.

<%@page contentType="text/html" %>de">

<%@page contentType="text/html" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        <script language="javascript" src="jquery-1.4.2.js"></script>
        <script language="javascript">
            function checkUsername(){
                $.get( "checkusername.jsp",{username : $("#registerform\\:username").val()},updateUsername);
            function updateUsername(response)
                if (response) {
                    $("#usernameresult").text(response);  // update SPAN item with result
        <%@taglib prefix="f" uri=""%>
        <%@taglib prefix="h" uri=""%>
           <h2>Registration </h2>
           <h:form  id="registerform">
                        <td>Username : </td>
                        <td><h:inputText id="username" value="#{userBean.username}"  required="true" onblur="checkUsername()" />
                            <h:message for="username" />
                            <span id="usernameresult" />
                        <td>Password : </td>
                        <td><h:inputSecret id="password" value="#{userBean.password}"  required="true" /> <h:message for="password" /> </td>
                        <td>Re-enter Password : </td>
                        <td><h:inputSecret id="confirmPwd" value="#{userBean.confirmPwd}"  required="true" /> <h:message for="confirmPwd" /> </td>
                        <td>Email Address  : </td>
                        <td><h:inputText id="email" value="#{}" required="true" onblur="checkEmail()"  /> <h:message for="email" /> </td>
                            <span id="emailresult" />
              <h:commandButton actionListener="#{userBean.register}" value="Register" />
              <h3><h:outputText value="#{userBean.message}" escape="false"  /> </h3>

The above JSF Form uses userBean, which is the name given to beans.UserBean class. The class and its entries in faces-config.xml file are given below.

beans.UserBean class

UserBean is the managed bean that stores data coming from JSF form. It contains an action listener - register(), which is supposed to process the data to complete registration process. We don't deal with it as our focus is only on validating username.

package beans;

public class UserBean {
    private String username, password, email,confirmPwd, message;

    public UserBean() {

    public String getPassword() {
        return password;

    public void setPassword(String password) {
        this.password = password;

    public String getUsername() {
        return username;

    public void setUsername(String username) {
        this.username = username;

    public String getConfirmPwd() {
        return confirmPwd;

    public void setConfirmPwd(String confirmPwd) {
        this.confirmPwd = confirmPwd;

    public String getEmail() {
        return email;

    public void setEmail(String email) { = email;

    public String getMessage() {
        return message;

    public void setMessage(String message) {
        this.message = message;

    public void  register(ActionEvent evt) {
       if (! password.equals(confirmPwd))
             message = "Passwords do not match!";
       // do registration
    } // register


The following entry is required in faces-config.xml for UserBean managed bean.



Now create a checkusername.jsp to check whether given username is valid. It sends a message if username already exists otherwise it sends empty string (nothing).

<%@ page import="java.sql.*"  contentType="text/plain"%>
 String username = request.getParameter("username");  // sent from client
 // connect to oracle using thin driver
 Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","youruser","yourpassword");
 PreparedStatement ps = con.prepareStatement("select username from users where username = ?");
 ResultSet  rs = ps.executeQuery();
 if ( { // found username
    out.println("Username is already present!");  // send this to client

Deploy and Test

Now deploy the web application and run register.jsp. If you enter a username that is already present in USERS table then we get message - Username is already present - in SPAN item on the right of username field. If username is unique then SPAN item is set to empty string ( as JSP returns nothing).