Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

• Build a web application based on JSP. • Provide login and register page • If t

ID: 3874032 • Letter: #

Question

• Build a web application based on JSP.

• Provide login and register page

• If the username and password are both verified successfully, the user can login the web site and get all users’ information that is stored in the database and open a new page the display all the information.

Build a web application based on JSP. Use the forms and table built in assignment 1 and assignment 2. Provide login and register page If the username and password are both verified successfully, the user can login the web site and get all users' information that is stored in the database and open a new page the display all the information.

Explanation / Answer

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>JSP Example</title>

    </head>

    <body>

        <form method="post" action="login.jsp">

            <center>

            <table border="1" width="30%" cellpadding="3">

                <thead>

                    <tr>

                        <th colspan="2">Login Here</th>

                    </tr>

                </thead>

                <tbody>

                    <tr>

                        <td>User Name</td>

                        <td><input type="text" name="uname" value="" /></td>

                    </tr>

                    <tr>

                        <td>Password</td>

                        <td><input type="password" name="pass" value="" /></td>

                    </tr>

                    <tr>

                        <td><input type="submit" value="Login" /></td>

                        <td><input type="reset" value="Reset" /></td>

                    </tr>

                    <tr>

                        <td colspan="2">Yet Not Registered!! <a href="reg.jsp">Register Here</a></td>

                    </tr>

                </tbody>

            </table>

            </center>

        </form>

    </body>

</html>

reg.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>Registration</title>

    </head>

    <body>

        <form method="post" action="registration.jsp">

            <center>

            <table border="1" width="30%" cellpadding="5">

                <thead>

                    <tr>

                        <th colspan="2">Enter Information Here</th>

                    </tr>

                </thead>

                <tbody>

                    <tr>

                        <td>First Name</td>

                        <td><input type="text" name="fname" value="" /></td>

                    </tr>

                    <tr>

                        <td>Last Name</td>

                        <td><input type="text" name="lname" value="" /></td>

                    </tr>

                    <tr>

                        <td>Email</td>

                        <td><input type="text" name="email" value="" /></td>

                    </tr>

                    <tr>

                        <td>User Name</td>

                        <td><input type="text" name="uname" value="" /></td>

                    </tr>

                    <tr>

                        <td>Password</td>

                        <td><input type="password" name="pass" value="" /></td>

                    </tr>

                    <tr>

                        <td><input type="submit" value="Submit" /></td>

                        <td><input type="reset" value="Reset" /></td>

                    </tr>

                    <tr>

                        <td colspan="2">Already registered!! <a href="index.jsp">Login Here</a></td>

                    </tr>

                </tbody>

            </table>

            </center>

        </form>

    </body>

</html>

registration.jsp

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

<%

    String user = request.getParameter("uname");   

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

    String fname = request.getParameter("fname");

    String lname = request.getParameter("lname");

    String email = request.getParameter("email");

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

    Connection con = DriverManager.getConnection("jdbc:mysql://dbname",

            "root", "dbpass");

    Statement st = con.createStatement();

    //ResultSet rs;

    int i = st.executeUpdate("insert into members(first_name, last_name, email, uname, pass, regdate) values ('" + fname + "','" + lname + "','" + email + "','" + user + "','" + pwd + "', CURDATE())");

    if (i > 0) {

        //session.setAttribute("userid", user);

        response.sendRedirect("welcome.jsp");

       // out.print("Registration Successfull!"+"<a href="index.jsp">Go to Login</a>");

    } else {

        response.sendRedirect("index.jsp");

    }

%>

welcome.jsp

Registration is Successful.

Please Login Here <a href="index.jsp">Go to Login</a>

login.jsp

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

<%

    String userid = request.getParameter("uname");   

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

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

    Connection con = DriverManager.getConnection("jdbc:mysql://database",

            "root", "dbpass");

    Statement st = con.createStatement();

    ResultSet rs;

    rs = st.executeQuery("select * from members where uname='" + userid + "' and pass='" + pwd + "'");

    if (rs.next()) {

        session.setAttribute("userid", userid);

        //out.println("welcome " + userid);

        //out.println("<a href="logout.jsp">Log out</a>");

        response.sendRedirect("success.jsp");

    } else {

        out.println("Invalid password <a href="index.jsp">try again</a>");

    }

%>

success.jsp

<%

    if ((session.getAttribute("userid") == null) || (session.getAttribute("userid") == "")) {

%>

You are not logged in<br/>

<a href="index.jsp">Please Login</a>

<%} else {

%>

Welcome <%=session.getAttribute("userid")%>

<a href="logout.jsp">Log out</a>

<%

    }

%>

logout.jsp

<%

session.setAttribute("userid", null);

session.invalidate();

response.sendRedirect("index.jsp");

%>

To retrieve the data:

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%
String id = request.getParameter("userid");
String driver = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String database = "test";
String userid = "uname";
String password = "";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<!DOCTYPE html>
<html>
<body>

<h1>Retrieve data from database in jsp</h1>
<table border="1">
<tr>
<td>first name</td>
<td>last name</td>

<td>Email</td>

</tr>
<%
try{
connection = DriverManager.getConnection(connectionUrl+database, userid, password);
statement=connection.createStatement();
String sql ="select * from users";
resultSet = statement.executeQuery(sql);
while(resultSet.next()){
%>
<tr>
<td><%=resultSet.getString("first_name") %></td>
<td><%=resultSet.getString("last_name") %></td>

<td><%=resultSet.getString("email") %></td>
</tr>
<%
}
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</table>
</body>
</html>

<%

session.setAttribute("userid", null);

session.invalidate();

response.sendRedirect("index.jsp");

%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>JSP Example</title>

    </head>

    <body>

        <form method="post" action="login.jsp">

            <center>

            <table border="1" width="30%" cellpadding="3">

                <thead>

                    <tr>

                        <th colspan="2">Login Here</th>

                    </tr>

                </thead>

                <tbody>

                    <tr>

                        <td>User Name</td>

                        <td><input type="text" name="uname" value="" /></td>

                    </tr>

                    <tr>

                        <td>Password</td>

                        <td><input type="password" name="pass" value="" /></td>

                    </tr>

                    <tr>

                        <td><input type="submit" value="Login" /></td>

                        <td><input type="reset" value="Reset" /></td>

                    </tr>

                    <tr>

                        <td colspan="2">Yet Not Registered!! <a href="reg.jsp">Register Here</a></td>

                    </tr>

                </tbody>

            </table>

            </center>

        </form>

    </body>

</html>