SlideShare a Scribd company logo
Servlet/JSP
DB Connection
Arulkumar Varatharajan – AP/CSE
1
DB
2
register.html
3
<form action="register" method="post">
Name:<input type="text" name="userName"/><br/>
Password:<input type="password" name="password"/><br/>
Email Id:<input type="text" name="email" /><br/>
Language: <select name="language">
<option>Hindi</option> <option>English</option> <option>French</option>
</select> <br/>
<input type="submit" value="Submit"/> </form></body></html>
4
package com.jwt.servlet;
import java.io.IOException; import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class RegisterServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
RegisterServlet.java
5
String n = request.getParameter("userName");
String p = request.getParameter("password");
String e = request.getParameter("email");
String c = request.getParameter("language");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/servlet", "root", "mukesh");
PreparedStatement ps = con
.prepareStatement("insert into USERDETAILS values(?,?,?,?)");
ps.setString(1, n);
ps.setString(2, p);
ps.setString(3, e);
ps.setString(4, c);
RegisterServlet.java
6
int i = ps.executeUpdate(); // returns the binary value 0/1
if (i > 0)
out.print("You are successfully registered...");
} catch (Exception e2) {
System.out.println(e2);
}
out.close();
}
}
RegisterServlet.java
7
<servlet>
<servlet-name>Register</servlet-name>
<servlet-class>com.jwt.servlet.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Register</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>register.jsp</welcome-file>
</welcome-file-list>
WEB.xml
8
output
9
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="com.example.util.DBUtil"%>
<%
String userName = request.getParameter("username");
String password = request.getParameter("password");
Connection con = DBUtil.getMySqlConnection();
Statement st = con.createStatement();
ResultSet rs;
rs = st.executeQuery("select * from EMPLOYEE where USER_NAME='"
+ userName + "' and PASSWORD='" + password + "'");
if (rs.next()) {
session.setAttribute("username", userName);
response.sendRedirect("success.jsp");
}
else
{
out.println("Invalid password <a href='index.jsp'>try again</a>");
} %>
Login.jsp
10
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="com.example.util.DBUtil"%>
<%
String userName = request.getParameter("username");
String password = request.getParameter("password");
String firstName = request.getParameter("firstname");
String lastName = request.getParameter("lastname");
String email = request.getParameter("email");
Connection con = DBUtil.getMySqlConnection();
Statement st = con.createStatement();
int i = st.executeUpdate
registration.jsp
11
("insert into EMPLOYEE
(FIRST_NAME, LAST_NAME, EMAIL, USER_NAME, PASSWORD)
values (‘”+ firstName+ "',‘”+ lastName+ "',‘”+ email+ "',‘”+
userName+ "',‘”+ password+ "')");
if (i > 0) {
response.sendRedirect("welcome.jsp");
} else {
response.sendRedirect("index.jsp");
}
%>
Login.jsp
12
Login.jsp
13
success.jsp

More Related Content

PPT
PHP: Debugger, Profiler and more
PPT
Heavy Web Optimization: Backend
PPTX
Wt unit 5
PPT
Build your own PHP extension
PDF
Php workshop L03 superglobals
PPTX
Session 3 Java Script
PPTX
Introduction to JavaScript
PPT
Asp #1
PHP: Debugger, Profiler and more
Heavy Web Optimization: Backend
Wt unit 5
Build your own PHP extension
Php workshop L03 superglobals
Session 3 Java Script
Introduction to JavaScript
Asp #1

Similar to servlet db connectivity (20)

PPTX
Java Training Ahmedabad , how to Insert Data in Servlet, iOS Classes Ahmedabad
PPT
php_postgresql.pptyyguyg7g7g777g76776777
DOCX
การเข ยนโปรแกรมต ดต_อฐานข_อม_ล
PPT
php_postgresql.ppt
PPT
Introduction to php and POSTGRESQL. ....
PPT
PHP with Postgres SQL connection string and connecting
PPT
php_postgresql.ppt
PPT
Lecture7 form processing by okello erick
PPTX
deDacota: Toward Preventing Server-Side XSS via Automatic Code and Data Separ...
PPTX
18.register login
PPT
Rapid java backend and api development for mobile devices
PDF
JSP TUTORIAL for students M.Sc computer Science
PPT
Struts Overview
PDF
How to connect redis and mule esb using spring data redis module
PPT
PHP-04-Forms.ppt
PPT
JSP/Servlet Core Concept
PDF
PPT
Spsl v unit - final
PPT
Class 6 - PHP Web Programming
DOC
Html basics 10 form
 
Java Training Ahmedabad , how to Insert Data in Servlet, iOS Classes Ahmedabad
php_postgresql.pptyyguyg7g7g777g76776777
การเข ยนโปรแกรมต ดต_อฐานข_อม_ล
php_postgresql.ppt
Introduction to php and POSTGRESQL. ....
PHP with Postgres SQL connection string and connecting
php_postgresql.ppt
Lecture7 form processing by okello erick
deDacota: Toward Preventing Server-Side XSS via Automatic Code and Data Separ...
18.register login
Rapid java backend and api development for mobile devices
JSP TUTORIAL for students M.Sc computer Science
Struts Overview
How to connect redis and mule esb using spring data redis module
PHP-04-Forms.ppt
JSP/Servlet Core Concept
Spsl v unit - final
Class 6 - PHP Web Programming
Html basics 10 form
 
Ad

More from PhD Research Scholar (20)

PPTX
Quiz servlet
PPTX
2.java script dom
PPTX
1.java script
PPTX
Quiz javascript
PPTX
Thread&amp;multithread
PPTX
Streams&amp;io
PPTX
PPTX
Interface in java
PPTX
Inner classes in java
PPTX
PPTX
Exception handling
PPTX
Abstract class
PPTX
7. tuples, set &amp; dictionary
PPTX
PPTX
4. functions
Quiz servlet
2.java script dom
1.java script
Quiz javascript
Thread&amp;multithread
Streams&amp;io
Interface in java
Inner classes in java
Exception handling
Abstract class
7. tuples, set &amp; dictionary
4. functions
Ad

Recently uploaded (20)

PDF
Business Ethics Teaching Materials for college
PPTX
Institutional Correction lecture only . . .
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Cell Types and Its function , kingdom of life
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Basic Mud Logging Guide for educational purpose
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Complications of Minimal Access Surgery at WLH
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Pharma ospi slides which help in ospi learning
Business Ethics Teaching Materials for college
Institutional Correction lecture only . . .
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPH.pptx obstetrics and gynecology in nursing
Cell Types and Its function , kingdom of life
Module 4: Burden of Disease Tutorial Slides S2 2025
Basic Mud Logging Guide for educational purpose
01-Introduction-to-Information-Management.pdf
Microbial diseases, their pathogenesis and prophylaxis
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Microbial disease of the cardiovascular and lymphatic systems
Supply Chain Operations Speaking Notes -ICLT Program
Complications of Minimal Access Surgery at WLH
TR - Agricultural Crops Production NC III.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Pharma ospi slides which help in ospi learning

servlet db connectivity