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: Modify the above program to use an xml file instead of database
login.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"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr align="center">
<td align="center" colspan="2">
<input type="submit" name="ok" value="Submit">
</td>
</tr>
</table>
</form>
StudentLoginServlet.java html Copy Edit
package pres;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class StudentLoginServlet extends HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
ServletConfig config = getServletConfig();
String un = req.getParameter("uname");
String pw = req.getParameter("pwd");
String uname = config.getInitParameter("uname");
String pwd = config.getInitParameter("pwd");
if (un.equals(uname) && pw.equals(pwd)) {
RequestDispatcher rd = req.getRequestDispatcher("home.jsp");
rd.forward(req, resp);
} else {
RequestDispatcher rd1 = req.getRequestDispatcher("login.html");
rd1.include(req, resp);
out.println("<h3 align='center' style='color:red;'>Wrong credentials</h3>");
}
}
}
home.jsp
<h2>Welcome to the Home Page - You are successfully logged in</h2><br/>
<a href="login.html">Back</a>
web.xml
<web-app>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>sls</servlet-name>
<servlet-class>pres.StudentLoginServlet</servlet-class>
<init-param>
<param-name>uname</param-name>
<param-value>kumar</param-value>
</init-param>
<init-param>
<param-name>pwd</param-name>
<param-value>kumar</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>sls</servlet-name>
<url-pattern>/sls</url-pattern>
</servlet-mapping>
</web-app>