0% found this document useful (0 votes)
100 views5 pages

Practical 23 1

The document describes a program to create and manage sessions using HttpSession in Java servlets. It includes two servlets - First.java and SecondApp.java. First.java authenticates the user, creates a session if credentials are valid, and stores user attributes in the session. It then forwards to SecondApp.java, which retrieves the user attributes from the session. The program demonstrates how to get session details like ID, creation time, max inactive interval using HttpSession methods.

Uploaded by

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

Practical 23 1

The document describes a program to create and manage sessions using HttpSession in Java servlets. It includes two servlets - First.java and SecondApp.java. First.java authenticates the user, creates a session if credentials are valid, and stores user attributes in the session. It then forwards to SecondApp.java, which retrieves the user attributes from the session. The program demonstrates how to get session details like ID, creation time, max inactive interval using HttpSession methods.

Uploaded by

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

PRACTICAL 23: WRITE A PROGRAM TO CREATE SESSION USING HTTPSESSION

CLASS

First.java:

import java.io.IOException;
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;
import javax.servlet.http.HttpSession;
 
 
public class First extends HttpServlet
{
 
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException
{
 
 doPost(request, response);
 }
 
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException
{
 
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
  out.println("<HTML>");
  out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
  out.println("  <BODY>");
 
   
  String  name=request.getParameter("uname");
  String  pwd=request.getParameter("pwd");
  HttpSession  session=request.getSession();
  session.setMaxInactiveInterval(10);
  if(name.equals("atharva")&&(pwd.equals("agrawal")))
  {

   out.println("<h1> Login Success ");


   out.println("Session id is :"+session.getId());
   out.println("Session CT is :"+session.getCreationTime());
   out.println("Session Interval time is :"+session.getMaxInactiveInterval());  
   session.setAttribute("userattr",name);
   out.println("<a href='Second'> Go for Next servlet...</a>");
  }
  else
  {
   out.println("<h1> Login Fails ");
   RequestDispatcher rd=request.getRequestDispatcher("/MyHtml.html");
   rd.include(request, response);
  }
   
  out.println("  </BODY>");
  out.println("</HTML>");
  out.flush();
  out.close();
 }
 
}

SecondApp.java:

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;
public class SecondApp extends HttpServlet {   
 public void service(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException
{
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
  out.println("<HTML>");
  out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
  out.println("  <BODY>");
  HttpSession  session=request.getSession();
  out.println("<h3> Session ID is "+session.getId());
  String fetchname=(String) session.getAttribute("userattr");
  if(fetchname!=null)
  {
  out.println(" <h1><b>  Name is :"+fetchname);
  }
  else
  {
   out.println(" <h2> Session Expires , please login again");
   out.println("<a href='/MyHtml.html'> Go for Home page...</a>");
  }
  out.println("  </BODY>");
  out.println("</HTML>");
  out.flush();
  out.close();
 }
 
 
}
MyHtml.html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <body>
   <form action="./First"  method="post">
   <center><h1><u>Session Programming example</u>
</h1>
   <h2>
  Username: <input type="text"  name="uname"/>
  password : <input type="password" name="pwd">
 
   <input type="submit" name="Click" value="click here">
   </h2>
   </center>
   </form>
  </body>
</html>

Output:
1.Develop servlet program to display various details about session using HttpSession methods

First Java

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class first extends HttpServlet {


@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
String name = request.getParameter("user");
String pass = request.getParameter("pass");

if(pass.equals("1234"))
{
//creating a session
HttpSession session = request.getSession();
session.setAttribute("user", name);
response.sendRedirect("Welcome");
}
}
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}

Second Java
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
public class first extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
String name = request.getParameter("user");
String pass = request.getParameter("pass");

if(pass.equals("1234"))
{
//creating a session
HttpSession session = request.getSession();
session.setAttribute("user", name);
response.sendRedirect("Welcome");
}
}
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}

Index.html

<html>
<body>
<form action=”./first”>
User:<input type=”Text” name=”user”/>
User:<input type=”Text” name=”pass”/>
</form>
</html>
</body>

You might also like