Q.1) Create A Web Application To Implement Servlet Lifecycle Using Servlet Interface. Ans 1)
Q.1) Create A Web Application To Implement Servlet Lifecycle Using Servlet Interface. Ans 1)
Q.1) Create a web application to implement Servlet Lifecycle using Servlet interface.
Ans 1)
Lifecyclenormal.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.*;
{System.out.println("DESTROY");
{System.out.println("INIT");
System.out.println("SERVICE");
PrintWriter out=res.getWriter();
public ServletConfiggetServletConfig()
return null;
giBS 01619114416
return null;
Web.xml
<web-app 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"
version="3.0">
<servlet>
<servlet-name>VIJ</servlet-name>
<servlet-class>Lifecyclenormal</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>VIJ</servlet-name>
<url-pattern>/Lifecyclenormal</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
giBS 01619114416
Output1:
giBS 01619114416
Ans 2)
Index.html
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<Form action="Validation">
Gender:
Hobbies:
<option>MCA</option>
<option>BCA</option>
<option>MCADD</option>
</select><br>
giBS 01619114416
</form>
</body>
</html>Web.xml
<web-app 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"
version="3.0">
<servlet>
<servlet-name>VIJ1</servlet-name>
<servlet-class>Validation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>VIJ1</servlet-name>
<url-pattern>/Validation</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Validation.java
import java.io.IOException;
giBS 01619114416
import java.io.PrintWriter;
import javax.servlet.GenericServlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
@Override
PrintWriter out=res.getWriter();
String name=req.getParameter("Vijender");
String Email=req.getParameter("Email");
String Password=req.getParameter("Password");
String Phonenumber=req.getParameter("Phonenumber");
String gender=req.getParameter("Gender");
String hobbies[]=req.getParameterValues("Hobbies");
String Course=req.getParameter("Course");
if(name.equals("")||Password.equals("")||Email.equals("")||Phonenumber.equals(""))
RequestDispatcherrd= req.getRequestDispatcher("index.jsp");
rd.forward(req, res);
else
{out.println(name);
out.println(Email);
giBS 01619114416
out.println(Password);
out.println(Phonenumber);
out.println(gender);
for(int i=0;i<hobbies.length;i++)
out.println(hobbies[i]);
out.println(Course);
Output2:
giBS 01619114416
Ques 3) Create a web application using Http Servlet to implement page hit counter.
Ans 3)
Counter.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Override
ServletContext context=getServletConfig().getServletContext();
context.setAttribute("count",0);
@Override
ServletContext context=getServletConfig().getServletContext();
int count=(Integer)context.getAttribute("count");
count++;
PrintWriter out=res.getWriter();
out.println("<h1>");
out.println(count);
out.println("</h1>");
context.setAttribute("count",count);
}
giBS 01619114416
doGet(req,res);
Index.html
<!DOCTYPE html>
<html>
<head>
<title>Counter</title>
</head>
<body>
</body>
</html>
Web.xml
<web-app 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"
version="3.0">
<servlet>
<servlet-name>Counter1</servlet-name>
<servlet-class>Counter1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Counter1</servlet-name>
<url-pattern>/count</url-pattern>
</servlet-mapping>
giBS 01619114416
<session-config>
<session-timeout>
40
</session-timeout>
</session-config>
</web-app>
Output3:
giBS 01619114416
Ques 4) Create a web application containing a login page and perform validation(using
servlet Config and request dispatcher).
Ans 4)
Web.xml
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>login</servlet-class>
<init-param>
<param-name>Uname</param-name>
<param-value>admin </param-value>
</init-param>
<init-param>
<param-name>Upassword</param-name>
<param-value>admin123</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Login.java
import java.io.IOException;
giBS 01619114416
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Override
res.setContentType("text/html");
String Uname=req.getParameter("Name");
String Password=req.getParameter("Password");
String Uname=getServletConfig().getInitParameter("Uname");
String Upassword=getServletConfig().getInitParameter("Upassword");
PrintWriter out=res.getWriter();
if(uname.equals(Uname)&&Password.equals(Upassword))
else
RequestDispatcherrd=req.getRequestDispatcher("index.jsp");
rd.include(req, res);
doPost(req,res);
Index.jsp
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<h1>Login</h1><form action="login">
</form>
</body>
</html>
giBS 01619114416
Output:
giBS 01619114416
Ques 5) Create a web application to show session management: Hidden fields technique
by creating a simple shopping application.
INDEX.html
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<h1>WELCOME!</h1>
</form>
</body>
</html>
First.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
PrintWriter out=res.getWriter();
String id[]=req.getParameterValues("id");
giBS 01619114416
out.println("<form action='Second'>");
for(int i=0;i<id.length;i++)
out.println("</form>");
SERVLET(Second)
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
PrintWriter out=res.getWriter();
String id[]=req.getParameterValues("id");
for(int i=0;i<id.length;i++)
out.println(id[i]);
String id1[]=req.getParameterValues("id1");
for(int j=0;j<id1.length;j++)
giBS 01619114416
out.println(id1[j]);
Web
<web-app 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"
version="3.0">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<servlet>
<servlet-name>First</servlet-name>
<servlet-class>First</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>First</servlet-name>
<url-pattern>/First</url-pattern>
</servlet-mapping>
giBS 01619114416
<servlet>
<servlet-name>Second</servlet-name>
<servlet-class>Second</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Second</servlet-name>
<url-pattern>/Second</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
giBS 01619114416
Output5:
giBS 01619114416
Ques 7) Create a web application showing the use of JSP implicit objects.
Welcome.jsp
out.println("HELLO "+name);
%>
Newhtml.html
<form action="welcome.jsp">
</form>
OUTPUT7a:
test.html
<html>
</head>
giBS 01619114416
<body>
visit again
</body>
</html>
Welcome.jsp
Index.html
<form action="welcome.jsp">
</form>
OUTPUT7b:
Index.jsp
<html>
<body>
</body>
</html>
OUTPUT7c:
giBS 01619114416
JSP 1(ERROR)
Exception is:<%=exception%>
JSP2(INDEX)
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<form action="process.jsp">
</form>
giBS 01619114416
</body>
</html>
JSP 3(PROCESS)
<%
String num1=request.getParameter("n1");
String num2=request.getParameter("n2");
int a=Integer.parseInt(num1);
int b=Integer.parseInt(num2);
int c=a/b;
%>
OUTPUT7d:
giBS 01619114416
Index.html
<form action="welcome">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
Web.xml
<web-app>
<servlet>
<servlet-name>sonoojaiswal</servlet-name>
<jsp-file>/welcome.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>sonoojaiswal</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
<context-param>
<param-name>dname</param-name>
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
</context-param>
</web-app>
Welcome.jsp
<%
out.print("Welcome "+request.getParameter("uname"));
String driver=application.getInitParameter("dname");
out.print("driver name is="+driver);
%>
giBS 01619114416
OUTPUT7e:
giBS 01619114416
INDEX.HTML
<html>
<head>
<title></title>
</head>
<body>
<a href="login.html">Login</a>
<a href="LogoutServlet">Logout</a>
<a href="ProfileServlet">Profile</a>
</body>
</html>
Link
<html>
<head>
<title></title>
</head>
<body>
<a href="login.html">Login</a>
<a href="LogoutServlet">Logout</a>
<a href="ProfileServlet">Profile</a>
<hr>
</body>
</html>
giBS 01619114416
Log in
<html>
<head>
<title></title>
</head>
<body>
</form>
</body>
</html>
LoginServlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
response.setContentType("text/html");
PrintWriter out=response.getWriter();
request.getRequestDispatcher("link.html").include(request,response);
giBS 01619114416
String name=request.getParameter("name");
String password=request.getParameter("password");
if(password.equals("admin123")){
out.println("");
HttpSession session=request.getSession();
session.setAttribute("name", name);
else{
request.getRequestDispatcher("login.html").include(request, response);
out.close();
LogoutServlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
giBS 01619114416
import javax.servlet.http.HttpSession;
response.setContentType("text/html");
PrintWriter out=response.getWriter();
request.getRequestDispatcher("link.html").include(request, response);
HttpSession session=request.getSession();
session.invalidate();
out.close();
ProfileServlet
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
giBS 01619114416
response.setContentType("text/html");
request.getRequestDispatcher("link.html").include(request,response);
HttpSession session=request.getSession(false);
if(session!=null)
String name=(String)session.getAttribute("name");
else
request.getRequestDispatcher("login.html").include(request, response);
out.close();
}
giBS 01619114416
Xml
<web-app 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"
version="3.0">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<servlet>
<description></description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>ProfileServlet</display-name>
giBS 01619114416
<servlet-name>ProfileServlet</servlet-name>
<servlet-class>ProfileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProfileServlet</servlet-name>
<url-pattern>/ProfileServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>LogoutServlet</display-name>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/LogoutServlet</url-pattern>
</servlet-mapping>
</web-app>
giBS 01619114416
OUTPUT 8:
Home Page:
LoginPage
Profile:
Logout
Error Page
giBS 01619114416
JSP 1(ERROR)
Exception is:<%=exception%>
JSP2(INDEX)
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<form action="process.jsp">
</form>
</body>
</html>
JSP 3(PROCESS)
<%
String num1=request.getParameter("n1");
String num2=request.getParameter("n2");
int a=Integer.parseInt(num1);
int b=Integer.parseInt(num2);
giBS 01619114416
int c=a/b;
%>
OUTPUT:
giBS 01619114416
Ques 10) Create a web application to show use of Java Bean in JSP.
JSP(INDEX)
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<form action="displaydetails.jsp">
Name:
Password:
Gender:<br>
</form>
</body>
</html>
JSP(DISPLAY)
<!DOCTYPE html>
<html>
<head>
giBS 01619114416
<title>JSP Page</title>
</head>
<body>
Details are:
</body>
</html>
BEAN CLASS
package pkg;
return txtname;
this.txtname = txtname;
return txtpass;
this.txtpass = txtpass;
}
giBS 01619114416
return gender;
this.gender = gender;
}
giBS 01619114416
OUTPUT10: