KEMBAR78
Servlets and JDBC | PDF
0% found this document useful (0 votes)
68 views3 pages

Servlets and JDBC

This document provides instructions for user authentication using servlets and JDBC in Java. It includes: 1. Creating an HTML form to collect a username and password from the user. 2. Using a servlet to connect to a database using JDBC, execute a SQL query to check if the username and password match a record, and return a success or failure message. 3. The SQL query selects from a "student" table to check if the entered username and password are in the table. 4. If a match is found, it authenticates the user and displays a success message. If no match, it displays a failure message.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views3 pages

Servlets and JDBC

This document provides instructions for user authentication using servlets and JDBC in Java. It includes: 1. Creating an HTML form to collect a username and password from the user. 2. Using a servlet to connect to a database using JDBC, execute a SQL query to check if the username and password match a record, and return a success or failure message. 3. The SQL query selects from a "student" table to check if the entered username and password are in the table. 4. If a match is found, it authenticates the user and displays a success message. If no match, it displays a failure message.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Servlets and JDBC (User authentication)

For : Beginners and Intermediate (Those who have basic knowledge on JDBC, Java, Servlets) This tutorial will tell you about the JDBC connection with Servlet and let to update or insert of query the Database table based on the dynamic Queries. Also this Example will show you how to authenticate a user based on the data stored in the Database Table Step 1: To create a DSN name, and to create a Database using MS Access, see this Post Following is the table structure: Name of the table is student regno pradeepkumar amiitesh password hellohowareyou welcome

Create a HTML Form that asks for a username and Password FileName: msindex.html <html> <head> <title> Form Processing </title> </head> <body> <form method=post action=FormServlet> Enter your username : <input type=text name=regno><br> Enter your Password : <input type=password name=password><br> <input type=submit> </form> </body> </html> FileName: FormServlet.java import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class FormServlet extends HttpServlet {

public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { res.setContentType(text/html); ServletOutputStream out=res.getOutputStream(); String s1=req.getParameter(user); String s2=req.getParameter(password); out.println(<html><body>); try { Class.forName(sun.jdbc.odbc.JdbcOdbcDriver); //JDBC ODBC Driver } catch (Exception e) { out.println(Driver Issues+e.getMessage()); } try { Connection con=DriverManager.getConnection(jdbc:odbc:pradeep); Statement stmt=con.createStatement(); ResultSet rs = stmt.executeQuery(select * from student where regno=+s1+ and password=+s2+); int count=0; while(rs.next()) { count++; } if(count>0) out.println(Success+s1+You are authenticated); else { out.println(Your password or username is wrong); //res.sendRedirect(form.html); } } catch (SQLException ee) { out.println(DSN Issues+ee.getMessage()); } } } Compile the file and Make an entry in the web.xml file and run the HTML file using the link http://localhost:8080/examples/msindex.html

Provide a name and password (which is there in the student table given above) and click Submit. You can see a page that authenticates you, if your username and password are correct, else it will show you a warning message.

You might also like