0% found this document useful (0 votes)
127 views44 pages

WAD Lab Manual

This document provides steps to create a web application for user authentication using Java servlets and filters. It describes creating HTML, servlet and filter files, and configuring the web.xml file to set up authentication that checks the submitted password and allows or denies access accordingly.

Uploaded by

Gopl Kuppa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views44 pages

WAD Lab Manual

This document provides steps to create a web application for user authentication using Java servlets and filters. It describes creating HTML, servlet and filter files, and configuring the web.xml file to set up authentication that checks the submitted password and allows or denies access accordingly.

Uploaded by

Gopl Kuppa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Web Application Development Lab Manual

1. Authentication using Java Servlet.

Step 1: Open the NetBeans IDE.

Step 2: Select "Java web" -> "Web application" as in the following:

Step 3: Type your project name as UserAuthenticationDemo.


Step 4: Click on "Next" then select your Java version and server details as in the following:

Step 5: Now delete your default "index.jsp" file and create a new "index.html" file and
write the following code for it.

index.html
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->

<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body bgcolor="pink">
<form method="get" action="Admin">
<table>
<tr>
<td>
<b>Name:</b>
</td>
<td>
<input type="text" name="name"/><br/>
</td>
</tr>
<tr>
<td>
<b>Password:</b>
</td>
<td>
<input type="password" name="password"/><br/>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input type="submit" value="Login">
</td>
</tr>
</table>

</form>
</body>
</html>

Step 6: Create a new servlet file named "Filter.java" and write the following code for it.
Filter.java
import java.io. IOException;
import java.io.PrintWriter;
import javax.servlet.*;

public class FilterOne implements Filter {

@Override
public void init(FilterConfig arg0) throws ServletException {
}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {

PrintWriter out = response.getWriter();

String password = request.getParameter("password");


if (password.equals("admin")) {
chain.doFilter(request, response);//sends request to next resource
} else {
out.print("username or password error!");
RequestDispatcher rd = request.getRequestDispatcher("index.html");
}

@Override
public void destroy() {
}

}
Step 7: Now create another servlet named "Admin.java" and write the following code for
it.

Admin.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Admin extends HttpServlet {

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("Welcome ADMIN");

out.close();
}

Step 8: Check your "web.xml" file.


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<filter>
<filter-name>FilterOne</filter-name>
<filter-class>FilterOne</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterOne</filter-name>
<url-pattern>/Admin</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>Admin</servlet-name>
<servlet-class>Admin</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Admin</servlet-name>
<url-pattern>/Admin</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>

Step 9: Now your project is ready to run.


Right-click on the Project menu and select Run. The following output is generated from
this application.
Username: Murali

Password :murali123
Step 10:
Now enter a username and password depending on your choice. If you enter the
password as "admin" then you are permited to visit the site else a warning is shown each
time when you enter an incorrect password.
2.Authentication using JSP
}

You might also like