In this article I am very happy to share with you my readers of my site a simple program that I wrote how to write a system using Java Server Pages and MySQL to login and register a user for our website. Basically an JSP code or snipplets is also a servlet used primarily for presentation in Java. This program will allow the visitor of the website to register their user name and password. After they are registered they can able to login in the website.
I make sure the code is not very long and very easy to understand. I hope you will find my wok useful in your programming projects and assignments.
If you have some questions about programming, about my work please send mu an email at jakerpomperada@gmail.comand jakerpomperada@yahoo.com. People here in the Philippines can contact me at my mobile number 09173084360.
Thank you very much and Happy Programming.
Sample Program Output
Program Listing
<!-- login.jsp -->
<!-- Written By: Mr. Jake R. Pomperada,MAED-IT -->
<!-- Date : July 24, 2015 -->
<!-- Tools : Netbeans IDE 8.0.2, Java SE 8 and Apache Tomcat Server -->
<html>
<script>
function validate(){
var username=document.form.user.value;
var password=document.form.pass.value;
if(username==""){
alert("Enter Username!");
return false;
}
if(password==""){
alert("Enter Password!");
return false;
}
return true;
}
</script>
<style>
table, td, th,h3 {
border: 1px solid green;
font-family: arial;
color: blue;
}
table {
background-color: lightgreen;
}
</style>
<form name="form" method="post" action="check.jsp" onsubmit="javascript:return validate();">
<h3> Login System and Registration System in JSP and MySQL </h3>
<table>
<tr><td>Username:</td><td><input type="text" name="user"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass"></td></tr>
<tr><td></td><td><input type="submit" value="Login"></td></tr>
<tr><td></td><td><a href="register.jsp" title="Click here to register account">
Register </a></td></tr>
</table>
</form>
</html>
// check.jsp
// Written By: Mr. Jake R. Pomperada,MAED-IT
// Date : July 24, 201
// Tools : Netbeans IDE 8.0.2, Java SE 8 and Apache Tomcat Server
<%@page import="java.sql.*"%>
<%
try{
String user=request.getParameter("user");
String pass=request.getParameter("pass");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/users","root","");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from login where username='"+user+"' and password='"+pass+"'");
int count=0;
while(rs.next()){
count++;
}
if(count>0){
out.println("<body bgcolor='lightgreen'>");
out.println("<h1> Welcome "+ user + " in the website. </h1>");
out.println("</body>");
}
else{
response.sendRedirect("login.jsp");
}
}
catch(Exception e){
System.out.println(e);
}
%>
<!-- register.jsp -->
<!-- Written By: Mr. Jake R. Pomperada,MAED-IT -->
<!-- Date : July 24, 2015 -->
<!-- Tools : Netbeans IDE 8.0.2, Java SE 8 and Apache Tomcat Server -->
<%@ page language="java" import="java.sql.*,java.util.*,java.text.*" %>
<html>
<head>
<title>Registration Page </title>
</head>
<body>
<style>
table, td, th {
border: 1px solid green;
font-family: arial;
color: blue;
}
table {
background-color: lightgreen;
}
</style>
<%
out.println("<style> p {font-family: arial;"
+ " color: red; font-size: 16; }; "
+ "</style>");
out.println("<style> a,b {font-family: arial;"
+ " color: blue; font-size: 16; }; "
+ "</style>");
%>
<table border="1" width="50%">
<tr>
<td width="100%">
<form method="POST" action="save_login.jsp">
<h2 align="center">Register User Name and Password</h2>
<table border="1" width="100%">
<tr>
<td width="50%"><b>Username:</b></td>
<td width="50%"><input type="text" name="username" size="20"/> </td>
</tr>
<tr>
<td width="50%"><b>Password:</b></td>
<td width="50%"><input type="text" name="password" size="20"></td>
</tr>
<tr>
</table>
<p><input type="submit" value="Submit" name="submit">
<input type="reset" value="Reset" name="reset"></p>
</form>
</td>
</tr>
</table>
<% out.println("<br>");
out.println("<a href='login.jsp'> RETURN TO LOGIN PAGE </a>");
%>
</body>
</html>
// save_login.jsp
// Written By: Mr. Jake R. Pomperada,MAED-IT
// Date : July 24, 201
// Tools : Netbeans IDE 8.0.2, Java SE 8 and Apache Tomcat Server
<%@ page language="java" import="java.sql.*,java.util.*,java.text.*" %>
<%
out.println("<style> p {font-family: arial;"
+ " color: red; font-size: 16; }; "
+ "</style>");
out.println("<style> a,b {font-family: arial;"
+ " color: blue; font-size: 16; }; "
+ "</style>");
%>
<%
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";;
String db = "users";
String driver = "com.mysql.jdbc.Driver";
try{
Class.forName(driver);
con = DriverManager.getConnection(url+db,"root","");
try{
Statement st = con.createStatement();
String username=request.getParameter("username");
String password=request.getParameter("password");
int val = st.executeUpdate("INSERT login "
+ "VALUES(id,'"+username+"','"+password+"')");
con.close();
out.println("<p> The record is successfully saved. </p>");
out.println("<br>");
out.println("<a href='login.jsp'> RETURN TO LOGIN PAGE </a>");
}
catch (SQLException ex){
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e){
e.printStackTrace();
}
%>
No comments:
Post a Comment