You are on page 1of 13

NAME: Vithurun V

REG NO: 20MID0229

COURSE: CSI3018 - Advanced Java


SLOT: L53+L54

Lab Assessment – 3

1) Develop an application to demonstrate session management using


Servlet. Create a index file which provides the hyper link for the
following session management techniques (heading).

HTTT Session

Cookies

URL rewriting

Hidden Field Form.

For each of the technique, you need to handle the request by separate
servlet. Use the different attribute for each of the technique while
sending the response.
index.html:

OUTPUT:
Httpsession.html:

Servlet code:

import java.io.IOException;

import java.io.PrintWriter;

import jakarta.servlet.ServletException;

import jakarta.servlet.http.HttpServlet;

import jakarta.servlet.http.HttpServletRequest;

import jakarta.servlet.http.HttpServletResponse;

import jakarta.servlet.http.HttpSession;
public class Servlet extends HttpServlet {

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {

res.setContentType("text/html");

HttpSession session = req.getSession();

res.setContentType("text/html");

PrintWriter pw=res.getWriter();

Integer attribute=(Integer)session.getAttribute("attribute");

if (attribute == null)

attribute = 1;

pw.println("<h1><center>NewUser</center></h1>");

} else {

pw.println("<h1><center>Welcome back</center></h1>");

attribute = attribute+1;

session.setAttribute("attribute",attribute);

pw.println("<h2>Number of time visit:"+attribute+"</h2>");

}
OUTPUT:

Cookie.html:
Servlet code:

import java.io.IOException;

import java.io.PrintWriter;

import jakarta.servlet.ServletException;

import jakarta.servlet.http.HttpServlet;

import jakarta.servlet.http.HttpServletRequest;

import jakarta.servlet.http.HttpServletResponse;

import jakarta.servlet.http.Cookie;

public class Servlet1 extends HttpServlet {

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse


response)

throws ServletException, IOException {

PrintWriter out=response.getWriter();

// Create cookies for first and last names.

Cookie UserName = new Cookie("username",


request.getParameter("username"));

// Set expiry date after 24 Hrs for both the cookies.

UserName.setMaxAge(60*60*24);
// Add both the cookies in the response header.

response.addCookie( UserName );

// Get an array of Cookies associated with this domain

Cookie ck[] = request.getCookies();

if( ck != null ) {

out.println("<h2> Found Cookies Name and Value</h2>");

for (int i = 0; i < ck.length; i++) {

Cookie ck1 = ck[i];

out.print("Name : " + ck1.getName( ) + ", ");

out.print("Value: " + ck1.getValue( ) + " <br/>");

} else {

out.println("<h2>No cookies founds</h2>");

}
OUTPUT:
URL.html

Servlet code:
import java.io.IOException;

import java.io.PrintWriter;

import jakarta.servlet.ServletException;

import jakarta.servlet.http.HttpServlet;

import jakarta.servlet.http.HttpServletRequest;

import jakarta.servlet.http.HttpServletResponse;

public class Servlet2 extends HttpServlet {

protected void doGet(HttpServletRequest req, HttpServletResponse res)


throws ServletException, IOException {

res.setContentType("text/html");

PrintWriter pw=res.getWriter();

String path = req.getContextPath();

String addURL = res.encodeURL(path + "/Welcome.html");

pw.println("<html>");

pw.println("<head>");

pw.println("<title>Example</title>");

pw.println("</head>");

pw.println("<body><center>");

pw.println("<h1>URL Rewriting Demo</h1>");

pw.println("<h3>For next page - <a href=\"" + addURL + "\"> Click


Here</a></h3>");

pw.println("</center></body>");

pw.println("</html>");

}
Output:
Hidden field form.html:

Servlet code:
import java.io.IOException;

import java.io.PrintWriter;

import jakarta.servlet.ServletException;

import jakarta.servlet.http.HttpServlet;

import jakarta.servlet.http.HttpServletRequest;

import jakarta.servlet.http.HttpServletResponse;

public class Servlet3 extends HttpServlet {

protected void doPost(HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {


String web = req.getParameter("website");

res.setContentType("text/html");

PrintWriter out = res.getWriter();

out.println("<h1><center>Welcome to " + web+"</center></h1>");

Output:

You might also like