Week7
Aim:
Design a controller with servlet that provides the interaction with application
developed in experiment 1 and the database created in experiment 5.
ShoppingCartController.java
package com.cmrcet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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("/ShoppingCartController")
public class ShoppingCartController extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String userName=request.getParameter("userName");
String password=request.getParameter("password");
//JDBC CONNECTION
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//create the connection
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/week8","root","Nagaveni12
3@");
Statement st= con.createStatement();
String query="select * from users where userName='"+userName+"' and
password='"+password+"'";
ResultSet rs=st.executeQuery(query);
if(rs.next()) {
out.print("<h1>"+userName+":welcome to home page</h1><br>");
out.print("<h1>loginsuccessfully</h1><br>");
}else {
//the user Id and password not available
out.print("<h1>"+userName+":please enter correct user id and password</h1><br>");
out.print("<h1>loginfailed</h1><br>");
}
rs.close();
st.close();
con.close();
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException e) {
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
login.html
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
<link rel="stylesheet"
         href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css"
integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI
2xXr2"
         crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+
OrCXaRkfj"
         crossorigin="anonymous"></script>
<script
          src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/
tE3MoK7ZeZDyx"
          crossorigin="anonymous"></script>
</head>
<body>
          <div class="container">
                 <h1 class="text-danger">Login Page</h1>
                 <form action="ShoppingCartController" method="post">
                        <div class="form-group">
                                 <label class=""form-label">UserName</label> <input
type="text"
                                        class="form-control" name="userName">
                        </div>
                        <div class="form-group">
                                 <label class=""form-label">Password</label> <input
type="password"
                                        class="form-control" name="password">
                        </div>
<button type="submit" class="btn btn-primary">Login</button>
                 </form>
</body>
</html>