Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Servlet In JSP
In this section, we have developed a web application of login authentication using
Bean in JSP. Five files are used "login.jsp", "loginbean.jsp",
"welcome.jsp","LoginBean.java" and "login.java" in the code given below.
Brief description of the flow of application :
1). Create a webpage "login.jsp" to login the user.
2). Create a webpage "loginbean.jsp" to set the parameter using JSP Bean and forward
to the Servlet page.
3). Create a bean file "LoginBean.java" to mapping the parameter from
"loginbean.jsp".
4). Create a Servlet "login.java" to validate the username and password from the
database.
5). Create a webpage "welcome.jsp" display a message after successfully user login.
Step:1 Create a web page "login.jsp" to login the user.
<html>
<head>
</head>
<body>
<form name="loginform" method="post" action="loginbean.jsp">
<br><br>
<table align="center"><tr><td><h2>Login Authentication</h2></td></tr></table>
<table width="300px" align="center" style="border:1px solid #000000;background-color:#ef
<tr><td colspan=2></td></tr>
<tr><td colspan=2> </td></tr>
<tr>
<td><b>Login Name</b></td>
<td><input type="text" name="userName" value=""></td>
</tr>
<tr>
<td><b>Password</b></td>
<td><input type="password" name="password" value=""></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
<tr><td colspan=2> </td></tr>
</table>
</form>
</body>
</html>
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
javax.sql.*;
import java.sql.*;
public class login extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse re
sponse)
throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "user_register";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
String username="";
String userpass="";
String strQuery= "";
Statement st=null;
ResultSet rs=null;
HttpSession session = request.getSession(true);
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password)
;
if(request.getParameter("username")!=null &&
request.getParameter("username")!="" && request.getParameter
("password")!=null &&
request.getParameter("password")!="")
{
username = request.getParameter("username").toString();
userpass = request.getParameter("password").toString();
strQuery="select * from userregister where
username='"+username+"' and password='"+userpass+"'";
System.out.println(strQuery);
st = conn.createStatement();
rs = st.executeQuery(strQuery);
int count=0;
while(rs.next())
{
session.setAttribute("username",rs.getString(2));
count++;
if(count>0)
response.sendRedirect("welcome.jsp");
}
else
{
response.sendRedirect("login.jsp");
}
}
else
{
response.sendRedirect("login.jsp");
}
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
Output:
Login Authentication form :