Menu
- 14. A web application for implementation: The user is first served a login page which takes user's name and password. After Submitting the details the server checks these values against the data from a database and takes the following decisions.
- • If name and password matches, serves a welcome page with user's full name.
- • If name matches and password doesn't match, then serves “password mismatch” page
- • If name is not found in the database, serves a registration page, where user’s full name is asked and on submitting the full name, it stores, the login name, password and full name in the database (hint: use session for storing the submitted login name and password)
Aim: A web application for implementation: The user is first served a login page which takes user's name and password. After Submitting the details the server checks these values against the data from a database and takes the following decisions.
- If name and password matches, serves a welcome page with user’s full
- If name matches and password doesn’t match, then serves “password mismatch” page
- If name is not found in the database, serves a registration page, where user’s full name is asked and on submitting the full name, it stores, the login name, password and full name in the database (hint: use session for storing the submitted login name and password)
File: login7.html
<form name="login" method="post" action="sls">
<table width="370" border="1" align="center" style="background: silver;">
<tr height="25">
<th colspan="4" align="center">User Login</th>
</tr>
<tr>
<td>User Name</td>
<td><input type="text" name="uname" required></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pwd" required></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" name="ok" value="Submit">
</td>
</tr>
</table>
</form>
File: StudentLoginServlet.java
package pres;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.*;
import javax.servlet.http.*;
public class StudentLoginServlet extends HttpServlet {
private Connection con = null;
public void init() throws ServletException {
try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mrecw","root","naresh");
} catch (Exception e) {
e.printStackTrace();
}
}
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
Statement statement = null;
ResultSet rs = null;
HttpSession session = req.getSession();
String uname = req.getParameter("uname");
String pwd = req.getParameter("pwd");
try {
PrintWriter out = resp.getWriter();
statement = con.createStatement();
rs = statement.executeQuery("select uname, pwd, fullname from login where uname='" + uname + "' ");
if (rs.next()) {
if (pwd.equals(rs.getString("pwd"))) {
session.setAttribute("fullname", rs.getString("fullname"));
RequestDispatcher rd = req.getRequestDispatcher("home8.jsp");
rd.forward(req, resp);
} else {
out.println("<h3 align='center' style='color:red;'>Password Mismatch</h3>");
RequestDispatcher rd = req.getRequestDispatcher("login7.html");
rd.include(req, resp);
}
} else {
out.println("<h3 align='center' style='color:red;'>Please register your details</h3>");
RequestDispatcher rd = req.getRequestDispatcher("registration.jsp");
rd.include(req, resp);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
File: home8.jsp
<h2>Hi <%=(String)session.getAttribute("fullname") %>!</h2><br/>
<h2>Welcome to the home page - You are successfully logged in</h2><br/>
<a href="login7.html">Back</a>
File: web.xml
<web-app>
<servlet>
<servlet-name>sls</servlet-name>
<servlet-class>pres.StudentLoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sls</servlet-name>
<url-pattern>/sls</url-pattern>
</servlet-mapping>
</web-app>