0% found this document useful (0 votes)
0 views

Exp-7_merged

The document outlines the implementation of a simple application using JSON and MySQL database connectivity. It includes a JSON file with user data, HTML pages for displaying and submitting data, and JSP/Servlet code for handling user login authentication against a MySQL database. Instructions for setting up the database and creating necessary tables are also provided.

Uploaded by

ajithprakash0023
Copyright
© © All Rights Reserved
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)
0 views

Exp-7_merged

The document outlines the implementation of a simple application using JSON and MySQL database connectivity. It includes a JSON file with user data, HTML pages for displaying and submitting data, and JSP/Servlet code for handling user login authentication against a MySQL database. Instructions for setting up the database and creating necessary tables are also provided.

Uploaded by

ajithprakash0023
Copyright
© © All Rights Reserved
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/ 14

Implement a Simple Application using JSON

People.json
[
{
"id": "1",
"firstName": "John",
"lastName": "Mohan"
},
{
"id": "2",
"firstName": "Mary",
"lastName": "P"
},
{
"id": "3",
"firstName": "KAMAL",
"lastName": "Hasan"
},
{
"id": "4",
"firstName": "Spandan",
"lastName": "Gunti"
},
{
"id": "5",
"firstName": "Web",
"lastName": "Programming"
}
]

New.html

<!DOCTYPE html>

<html lang="en">

<head>

<title>JSON Test</title>

</head>

<body>

<div id="myData"></div>

<script type="text/JavaScript">

let fetchRes = fetch('people.json');

fetchRes.then(function (response)

return response.json();

})
.then(function (data) {

appendData(data);

})

.catch(function (err) {

console.log('error: ' + err);

});

function appendData(data)

var mainContainer = document.getElementById("myData");

for(var i = 0; i < data.length; i++)

var div = document.createElement("div");

div.innerHTML = 'Name: ' + data[i].firstName + ' ' + data[i].lastName;

mainContainer.appendChild(div);

</script>

</body>

</html>
Install a database (MySql ).
1) Create a table which should contain at least the following fields: email, password Practice
'JDBC' connectivity..
2) Write a jsp program to connect to that database
3) Whenever a user logins with username and password it should be authenticate with database.

Download the MySQL from website: https://dev.mysql.com/downloads/mysql

Step 1:After installing a mysql server login with password

Step 2: Create a database

Step 3: Create a table login


Step 4: insert values into table login

Step 5 :use select command to view inserted values

Index.html

<!doctype html>

<html >

<head>

<title>Document</title>

</head>
<body>

<h1> Welcome to Database Connectivity </h1>

<form action="data.jsp">

<h2> click the submit button to get data from register table </h2>

<input type ="submit" value="submit">

</form>

</body>

</html>

Data.jsp

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

pageEncoding="ISO-8859-1"%>

<%@ page import="java.sql.*" %>

<% Class.forName("com.mysql.jdbc.Driver"); %>

<html>

<head> <title>The Publishers Database Table </title> </head>

<body>

<h1>The Publishers Database Table </h1>

<% Connection con=


DriverManager.getConnection("jdbc:mysql://localhost:3306/user1","root","root");

Statement stmt = con.createStatement();

ResultSet resultset = stmt.executeQuery("select * from login") ; %>

<table border="1">

<tr>

<th>Email</th>

<th>Password</th>

</tr>

<% while(resultset.next()){ %>


<tr>

<td> <%= resultset.getString(1) %></td>

<td> <%= resultset.getString(2) %></td>

</tr>

<% } %>

<% con.close(); %>

</table>

</body>

</html>

Output:
Experiment -8a:
Install a database (MySql ).
1) Create a table which should contain at least the following fields: email, password Practice
'JDBC' connectivity..
2) Write a java program/servlet to connect to that database
3) Whenever a user logins with username and password it should be authenticate with
database.

Download the MySQL from website: https://dev.mysql.com/downloads/mysql

Step 1:After installing a mysql server login with password

Step 2: Create a database


Step 3: Create a table login

Step 4: insert values into table login

Step 5 :use select command to view inserted values

//index.html//
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="http://localhost:8080/Exp8a/LoginController" method="post">
Enter username :<input type="text" name="username"> <br>
Enter password :<input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>

//LoginController.java//

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class LoginController
*/
@WebServlet("/LoginController")
public class LoginController extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public LoginController() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at:
").append(request.getContextPath());
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter pw = response.getWriter();
String un=request.getParameter("username");
String pn=request.getParameter("password");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection c =
DriverManager.getConnection("jdbc:mysql://localhost:3306/user9", "root", "root");

PreparedStatement ps = c.prepareStatement("select * from login where


email=? and pass=?");
ps.setString(1, un);
ps.setString(2, pn);
ResultSet rs = ps.executeQuery();
while (rs.next())
{
pw.println("valid user login successfully");
return;
}
pw.println("invalid user");
return;
}
catch (Exception e )
{
e.printStackTrace();
}
}

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>login</display-name>
<servlet>
<description></description>
<display-name>LoginController</display-name>
<servlet-name>LoginController</servlet-name>
<servlet-class>LoginController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginController</servlet-name>
<url-pattern>/LoginController</url-pattern>
</servlet-mapping>
</web-app>

Output

You might also like