DII
DII
DII
A user validation web application, where the user submits the login name and password to
the server.
i. The name and password are checked against the data, if the data matches, a
successful login page is returned. Otherwise a failure message is shown to the
user ( use an xml file to read parameters).
Login.html
<HTML>
<BODY>
<FORM METHOD="get" ACTION="init">
<U><B><CENTRE><h1>Login page</h1></CENTRE></B></U>
<BR>
<CENTRE><TABLE>
<TR>
<TD><b>Username:</b></TD>
<TD><INPUT TYPE="text" NAME="uname"></TD>
</TR>
<TR>
<TD><b>password:</b></TD>
<TD><INPUT TYPE="password" NAME="pwd"></TD>
</TR>
<TR>
<TD><INPUT TYPE="submit" value="Login"> <INPUT
TYPE="reset" value="Reset"></TD>
</TR>
</TABLE>
</CENTRE>
</FORM>
</BODY>
</HTML>
InitParamServlet.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class InitParamServlet extends HttpServlet
{
String u,p;
public void init(ServletConfig config)throws ServletException
{
u=config.getInitParameter("un");
p=config.getInitParameter("pwd");
}
public void doGet(HttpServletRequestreq,HttpServletResponse res)throws
ServletException,IOException
{
String un=req.getParameter("uname");
String pass=req.getParameter("pwd");
res.setContentType("text/html");
PrintWriter out=res.getWriter();
if(u.equals(un) &&p.equals(pass))
out.println("<h1>Hi"+un+"...<br><center>Successfully Logged into the
system</centre></h1>");
else
out.println("<h1>Hi"+un+"...<br><centre>Invalid Username or
Password</centre></h1>");
}
}
Web.xml
<web-app>
<servlet>
<servlet-name>ip</servlet-name>
<servlet-class>InitParamServlet</servlet-class>
<init-param>
<param-name>un</param-name>
<param-value>veda</param-value>
</init-param>
<init-param>
<param-name>pwd</param-name>
<param-value>kotagiri</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ip</servlet-name>
<url-pattern>/init</url-pattern>
</servlet-mapping>
</web-app>
DINESH
DINESH
Calc.html
<html>
<head>
<title>Calculator Example</title>
<style type="text/css">
<!--
.style2 {font-size: 16px;}
.style3 {font-size: 24px;font-weight: bold;}
.style4 {font-size: 24px;}
-->
</style>
</head>
<body>
<form action="CalcServlet" method="get">
<center>
<h1>Calculator Example</h1>
<br><b>
Enter First Number:
<input type="text" name="t1">
Enter Second Number:
<input type="text" name="t2"><br>
<br>
Select Any Operation:<br>
<table width="461" height="104" border="1" align="center"
cellpadding="0" cellspacing="0">
<tr>
<td width="126"><div align="center" class="style3">
<input name="add" type="radio" value="+">+</div></td>
<td width="163"><div align="center" class="style3">
<input name="add" type="radio" value="-">-</div></td>
<td width="150"><div align="center" class="style3">
<input name="add" type="radio" value="*">*</div></td>
</tr>
<tr>
<td><div align="center" class="style4"><strong><span
class="style3">
<input name="add" type="radio" value="/"></span>/
</strong></div>
<div align="center" class="style4"></div></td>
<td><div align="center" class="style4"><strong><span
class="style3">
<input name="add" type="radio" value="%"></span>%
</strong></div></td>
<td><div align="center"><span class="style2"><span
class="style4"></span></span></div></td>
</tr>
</table><br>
<input type="submit" value=" Submit ">
</center>
</form>
</body>
</html>
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.PrintWriter;
CalcServlet.java
import java.io.IOException;
public class CalcServlet extends HttpServlet{
public void doPost(HttpServletRequestreq,HttpServletResponse resp) throws
ServletException{
process(req,resp);
}
public void doGet(HttpServletRequestreq,HttpServletResponse resp) throws
ServletException{
process(req,resp);
}
public void process(HttpServletRequestreq,HttpServletResponse resp) throws
ServletException{
resp.setContentType("text/html");
try{
PrintWriter out=resp.getWriter();
int no1,no2;
String op;
no1=Integer.parseInt(req.getParameter("t1"));
no2=Integer.parseInt(req.getParameter("t2"));
op=req.getParameter("add");
out.println("Number1:"+no1);
out.println("Number2 is:"+no2);
out.println("operator:"+op);
int res=0;
if(op.equals("+"))
res=no1+no2;
if(op.equals("-"))
res=no1-no2;
if(op.equals("*"))
res=no1*no2;
if(op.equals("/"))
res=no1/no2;
if(op.equals("%"))
res=no1%no2;
out.println("<h1>Result = "+res+"</h1>");
}
catch(Exception e){
e.printStackTrace();
}
}
}
Web.xml
<web-app>
<servlet>
<servlet-name>CalcServlet</servlet-name>
<servlet-class>CalcServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CalcServlet</servlet-name>
<url-pattern>/CalcServlet</url-pattern>
</servlet-mapping>
</web-app>
3. A web application takes a name as input and on submit it shows a hello<name> page
where <name>is taken from the request. It shows the start time at the right top corner
of the page and provides a logout button. On clicking this button, it should show a
logout page with Thank You <name> message with the duration of usage(hint: Use
session to store name and time).
Session.html
<html>
<head>
<title>session test</title>
</head>
<body>
<center><h1>HOME PAGE</h1><br>
<form action="session" method="get">
Enter Your Name:<input type="text" name="uname"/><br><br>
<input type="submit" value="submit"/>
</form>
</center></body>
</html>
//session.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class session extends HttpServlet
{
public void doGet(HttpServletRequestrequest,HttpServletResponse response) throws
IOException,ServletException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<form method=get action=session2>");
Date d=new Date();
out.println("<p align=right>Time:"+d.getTime()+"</p>");
String un=request.getParameter("uname");
HttpSession session=request.getSession();
session.setAttribute("user",un);
session.setAttribute("time",d.getTime());
out.println("Hello"+un);
out.println("<br><br><input type=submit value=logout>");
out.println("</form>");
}
}
//session2.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class session2 extends HttpServlet
{
public void doGet(HttpServletRequestrequest,HttpServletResponse response) throws
IOException,ServletException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
HttpSession session=request.getSession();
Date d2=new Date();
String un=(String) session.getAttribute("user");
Long t1=(Long)session.getAttribute("time");
Long t2=d2.getTime();
session.invalidate();
out.println("Thank You"+un+"!!!");
out.print("<br><br>session duration:"+(t2-t1)/(60*60)+"Seconds");
}
}
DINESH
DINESH!!!
A web application that takes name and age from an HTML page. If the age is less than 18,
it should send a page with “Hello <name>, you are not authorized to visit this site”
message, where <name>should be replaced with the entered name. Otherwise it should
send “Welcome <name> to this site” message
Acess.html
<html>
<head>
<title>Acess</title></head>
<body>
<br><br><center><h1>User Eligibilty</h1><br>
<form method="get" action="check">
<input type="text" name="name" placeholder="enter your name">
<input type="text" name="age" placeholder="enter your age">
<input type="submit" name="sub" value="Submit">
</form>
</center>
</body>
</html>
checkage.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class checkage extends HttpServlet
{
public void doGet(HttpServletRequestreq,HttpServletResponse response) throws
IOException,ServletException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
int age=Integer.parseInt(req.getParameter("age"));
String name=req.getParameter("name");
if(age<=18)
{out.println("<h1>Hello"+name+",you r not authorised to visit this site</h1>");
}else
{out.println("<h1>Welcome"+name+",to this site</h1>");
}
}
}
DINESH