In: Computer Science
I need a sample of code of the below senario in Java with Spring Boot framework
Create a login and register page and user be able to register and then log in.
Data need to be stored from the user are:
Id, First Name, Last Name, DOB, Email, and Phone Number.
if user inputs the wrong credential system displays error.
please use JSPs. and JSTL dependencies
A USER NEED PASSWORD WITH HIS USER ID TO LOGIN OR SIGN WHICH YOU HAVE NOT MENTIONED IN YOUR QUESTION ANYWAY I AM TELLING YOU THIS WHOLE PROCESS WITH THE PASSWORD OKK MY FRIEND LETS UNDERSTAND OUR NEED FIRST WHAT WE HAVE TO DO FOR MAKING A SIGN UP AND A LOGIN PAGE ----
For making this type of page you need to develop several files like for frontend user side u have to make Two HTML FORM and by USING THAT FORMS that user can signup by filling his details and after that it will login by using that credentials --
Data of user's signup form will be saved to database for further LOGIN process and in ;LOGIN PROCESS WE MATCH THE GIVEN FOEM DATA FROM OUR DATABASE AND IF IT MATCHED THEN USER WILL LOGIN SUCCESFULLY ELSE if any one insert's a wrong credential then it will get "Invalid password or username." message . okk lets we start-----
index.html // your login form
<!DOCTYPE html>
<html>
<head>
<title>Login Form</title>
</head>
<body>
<form action="login.jsp" method="post">
id :<input type="text" name="usr" /><br>
password :<input type="password" name="password"
/><br>
<input type="submit" /> </form>
<p>New user. <a href="signup.html">signup
Here</a>. </body>
</html>
login.jsp
<%@ page language="java" contentType="text/html;"%>
<%@page import="java.sql.*,java.util.*"%>
<%
String userid=request.getParameter("userid");
session.putValue("userid",userid);
String password=request.getParameter("password");
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","");
Statement st= con.createStatement();
ResultSet rs=st.executeQuery("select * from users where
userid='"+userid+"' and password='"+password+"'");
try{
rs.next();
if(rs.getString("password").equals(password)&&rs.getString("userid").equals(userid))
{
out.println("Welcome " +userid);
}
else{
out.println("Invalid password or username.");
}
}
catch (Exception e) {
e.printStackTrace();
}
%>
signup.html // [your Sign up form ]
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>new registration</title>
</head>
<body>
<form action="signupdata.jsp" method="post">
User ID :<input type="text" name="userid" />
password :<input type="password" name="password" />
First name :<input type="text" name="fname" />
Last name :<input type="text" name="lname" />
Birthday: <input type="date" name="bday"/>
Email ID :<input type="text" name="email" />
Phone no: <input type="tel" name="usrtel"/>
<input type="submit" />
</form>
</body>
</html>
This file will save your signup form data to your database-----
signupdata.jsp
<%@ page language="java" %>
<%@page import="java.sql.*,java.util.*"%>
<%
String userid=request.getParameter("userid");
String password=request.getParameter("password");
String fname=request.getParameter("fname");
String lname=request.getParameter("lname");
String bday=request.getParameter("bday");
String email=request.getParameter("email");
String usrtel=request.getParameter("usrtel");
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/student",
"root", "");
Statement st=conn.createStatement();
int i=st.executeUpdate("insert into
users(userid,password,fname,lname,bday,email,usrtel)values('"+userid+"','"+password+"','"+fname+"','"+lname+"',''+bday+"",'"+email+"',""+usrtel+"")");
out.println("registered succesfully ! Please <a
href='index.html'>Login</a> ");
}
catch(Exception e)
{
System.out.print(e);
e.printStackTrace();
}
%>
DON'T FORGET TO UPVOTE IF YOU FIND IT USEFUL FOR YOURSELF
AND IF YOU HAVE ANY DOUBT PLEASE ASK IN COMMENT