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.
<%@page contentType="text/html" %>de"> <%@page contentType="text/html" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <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 } </script> <title>Registration</title> </head> <body> <%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%> <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%> <f:view> <h2>Registration </h2> <h:form id="registerform"> <table> <tr> <td>Username : </td> <td><h:inputText id="username" value="#{userBean.username}" required="true" onblur="checkUsername()" /> <h:message for="username" /> <span id="usernameresult" /> </tr> <tr> <td>Password : </td> <td><h:inputSecret id="password" value="#{userBean.password}" required="true" /> <h:message for="password" /> </td> </tr> <tr> <td>Re-enter Password : </td> <td><h:inputSecret id="confirmPwd" value="#{userBean.confirmPwd}" required="true" /> <h:message for="confirmPwd" /> </td> </tr> <tr> <td>Email Address : </td> <td><h:inputText id="email" value="#{userBean.email}" required="true" onblur="checkEmail()" /> <h:message for="email" /> </td> <span id="emailresult" /> </tr> </table> <p/> <h:commandButton actionListener="#{userBean.register}" value="Register" /> <p/> <h3><h:outputText value="#{userBean.message}" escape="false" /> </h3> <p/> </h:form> </f:view> </body> </html>lt;/f:view> </body> </html>
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) { this.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!"; return; } // do registration } // register }
<managed-bean> <managed-bean-name>userBean</managed-bean-name> <managed-bean-class>beans.UserBean</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean>
<%@ page import="java.sql.*" contentType="text/plain"%> <% String username = request.getParameter("username"); // sent from client // connect to oracle using thin driver Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","youruser","yourpassword"); PreparedStatement ps = con.prepareStatement("select username from users where username = ?"); ps.setString(1,username); ResultSet rs = ps.executeQuery(); if ( rs.next()) { // found username out.println("Username is already present!"); // send this to client } rs.close(); ps.close(); con.close(); %>