0% found this document useful (0 votes)
13 views16 pages

JDBCservlet

The document outlines the steps to connect to a database using JDBC, including installation and usage of JDBC libraries. It also provides example code for servlets, including a login servlet that verifies user credentials against a database and forwards to a congratulatory page upon successful login. Additionally, it includes HTML code for a login form styled with Bootstrap.

Uploaded by

seemachaudharyit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views16 pages

JDBCservlet

The document outlines the steps to connect to a database using JDBC, including installation and usage of JDBC libraries. It also provides example code for servlets, including a login servlet that verifies user credentials against a database and forwards to a congratulatory page upon successful login. Additionally, it includes HTML code for a login form styled with Bootstrap.

Uploaded by

seemachaudharyit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

JDBC

https://dev.mysql.com/downloads/installer/

mysql url

for mac

The steps for connecting to a database with JDBC are as follows:

1. Install or locate the database you want to access.


2. Include the JDBC library.
3. Ensure the JDBC driver you need is on your classpath.
4. Use the JDBC library to obtain a connection to the database.
5. Use the connection to issue SQL commands.
6. Close the connection when you are finished.

https://dev.mysql.com/downloads/mysql/
n

servlet and jsp


import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@WebServlet("/FirstServlet")

//every servlet should have unique name

public class FirstServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

//request will capture data from client

//request is black

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {

response.setContentType("text/html");

System.out.println("hello");//here are are able to print something on console

PrintWriter pw=response.getWriter();

//PrintWriter -- print text data to character stream

//getWriter()-- Returns a PrintWriter object that can send character text to the client

//It is going to work like a pencil/pen to write something on browser

//writing data inside response body


pw.println("Hello...dear friends how are you????");

pw.println("<h1> Subha......Bicky.....Chandra.....Hasina.....</h1>");

pw.println("<h2> Good Morning</h2>");

pw.println("<h3> Good Morning</h3>");

pw.println("<h4> Good Morning</h4>");

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {

doGet(request, response);

Html page1

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>first html</title>

</head>

<body>

<H1>are you feel well??? </H1>

<a href="FirstServlet">click here to execute FirstServlet</a> <br>

<a href="second.html">click here to execute second.html</a>

</body>
</html>
3 day in servlet
[9:36 PM, 7/20/2022] Zaved Sir US: package com.servlet;
import java.io.IOException;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@WebServlet("/loginServlet")

public class LoginServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {

//business logic

String name=request.getParameter("username");

String pass=request.getParameter("password");

if ("kushal".equals(name) && "786".equals(pass)) {

//true

//RequestDispatcher rd=request.getRequestDispatcher("congrats.html");

//rd.forward(request,response);

request.getRequestDispatcher("congrats.html").forward(request,response);;

}else {

//false
request.setAttribute("message","Invalid user...Try to login again");

//RequestDispatcher rd=request.getRequestDispatcher("login.jsp");

//rd.forward(request,response);

//request.getRequestDispatcher("login.jsp").forward(request,response);
//within a server

response.sendRedirect("https://www.google.com/"); //different server

//response.sendRedirect("test.jsp"); //within server

// it may work on same as we as on different server also

[9:36 PM, 7/20/2022] Zaved Sir US: }

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {

doGet(request, response);

}
Bootstrap tutorial

https://www.w3schools.com/bootstrap4/bootstrap_forms.asp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE html>

<html>

<head>

<title>login page</title>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>

</head>

<body>
<div class="container">

<h4> ${msg} </h4>

<h2>Login form</h2>

<form action="loginServlet">

<div class="form-group">

<label for="userName">User Name:</label>

<input type="text" class="form-control" placeholder="Enter user name" name="username">

</div>

<div class="form-group">

<label for="pwd">Password:</label>

<input type="password" class="form-control" placeholder="Enter password" name="password">

</div>

<button type="submit" class="btn btn-danger">Submit</button>

</form>

</div>

</body>

</html>
package com.servlet;

import java.io.IOException;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@WebServlet("/loginServlet")

public class LoginServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {

//read the data from browser(login.jsp) --- data comming from client side

String name=request.getParameter("username");

String pass=request.getParameter("password");

try {
//1.Load/register driver class

Class.forName("com.mysql.jdbc.Driver");

try {

//2.Create a connection

Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/sampledb","root","password");

//3.create a statement

PreparedStatement ps=conn.prepareStatement("select * from student


where username=? and password=?");

ps.setString(1,name); // name=bipin

ps.setString(2,pass); // pass=788

//4. execute query

ResultSet rs=ps.executeQuery();

//if u wanna print the record on console

if(rs.next()) {

System.out.println(" You are valid User");

String username=rs.getString(1);

String password=rs.getString(2);

String namee=rs.getString(3);

String email=rs.getString(4);

String ssn=rs.getString(5);

request.setAttribute("username",username);

request.setAttribute("password",password);
request.setAttribute("name",name);

request.setAttribute("email",email);

request.setAttribute("ssn",ssn);

request.getRequestDispatcher("congratulation.jsp").forward(request,response);

System.out.println(rs.getString(1)+" "+rs.getString(2)+"
"+rs.getString(3)+" "+rs.getString(4)+" "+rs.getString(5));

else {

System.out.println(" You are invalid User");

request.setAttribute("msg","Re-try...login again!!!");

request.getRequestDispatcher("login.jsp").forward(request,response);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {

doGet(request, response);
}

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<h2> Congratulations----You have successfully logged-in</h2>

${username} <br>

${password} <br>

${name} <br>

${email} <br>

${ssn}

</body>

</html>

You might also like