際際滷

際際滷Share 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

servlet db connectivity