In this tutorial you are going to learn create a simple login form in jsp
create a table named "login" with username and password as two fields
Note for msaccess give dsn as s2sgateway and for mysql database name as s2sgateway for this program to work or give your database name and change the code appropriately.
First create a form page that gets input for username and password
"login.jsp"
Quote:<form name="lo" method="post" action="loginaction.jsp">
<input type="text" name="username">
<input type="password" name="pass">
<input type="submit" value="submit">
</form>
Next create loginaction.jsp,success.jsp for success page and invalid.html for invalid page
<%@page language="java" import="java.sql.*;" session="true"%>
<%! Connection con;
ResultSet rs;String s1;String s2; int f=0;%>
<%try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:s2sgateway");
//if you are using mysql as database change the driver as com.mysql.jdbc.Driver and connection as jdbc:mysql//localhost:3306/s2sgateway,"root","root" where s2sgateway is database name
Statement st=con.createStatement();
rs=st.executeQuery("select * from login");
s1=request.getParameter("username");
s2=request.getParameter("pass");
if(s1==""&&s2=="")
{
response.sendRedirect("invalid.html");
}
else
{
session.setAttribute("uname",s1);
out.println("valid");
while(rs.next())
{
String s3=rs.getString(1);
String s4=rs.getString(2);
if (s1.equals(s3)&&s2.equals(s4))
{
f=1;
}
}
if(f==1)
{
response.sendRedirect("success.jsp");
}
else
{
response.sendRedirect("invalid.html");
}
}
}
catch(Exception e)
{
System.out.println(e);
}
%>
Any doubt ask us.
happy coding