Software | Version Required |
NetBeans IDE | Java bundle, 6.8 or 6.9 |
Java Development Kit (JDK) | version 6,7 |
GlassFish server | v3 or Open Source Edition 3.0.1 |
SQL Server | 2005 |
Type 4 Driver for SQL Server 2005 | sqljdbc |
SQL Server 2005 Type 4 driver
•First Download the SQL Server 2005 Type 4 driver.
•Set the classpath
Database Create a database and table in SQL Server 2005
Create a new web Application
index.jsp code
<%-- Document : index Created on : Nov 21, 2012, 3:42:17 PM Author : NIIT --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JNDI DEMO</title> </head> <body bgcolor="pink"> <h1>Login Management System</h1> <form action="NewServlet" method="post"> <table align="center"> <tr> <td>User Name </td> <td><input type="text" name="txtuser" ></td> </tr> <tr> <td>Password </td> <td><input type="password" name="txtpass" ></td> </tr> <tr> <td><input type="submit" value="Login"></td> </tr> </table> </form> </body> </html>
Configuration of JNDI
Now Check your Connection Poll is working or not?
Create a Servlet
Servlet Code
import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; @WebServlet(name = "NewServlet1", urlPatterns = {"/NewServlet1"}) public class NewServlet1 extends HttpServlet { Connection con; ResultSet rs; Statement st; InitialContext ctx; DataSource ds; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { String uname,pass; uname=request.getParameter("txtuser"); pass=request.getParameter("txtpass"); String qry="select * from Login"; ctx = new InitialContext(); ds = (DataSource)ctx.lookup("jdbc/myDatasource"); con=ds.getConnection(); st = con.createStatement(); rs = st.executeQuery(qry); rs.next(); if(uname.equals(rs.getString(1)) && pass.equals(rs.getString(2))) { out.println("Your User Name and Password is correct<br>"); out.println("Welcome Mr. "+ uname); } else { out.print("Invalid User Name & Password"); } } catch(SQLException ex) { out.println(ex.getMessage()); } catch(NamingException nx) { out.println(nx.getMessage()); } }
Comments
Post a Comment