0% found this document useful (0 votes)
16 views3 pages

Session

The document contains code for two Java servlets (Serv1 and Serv2) and an HTML file (Index.html). Serv1 retrieves a user name from a request, stores it in the session, and provides a link to Serv2. Serv2 retrieves the stored user name from the session and displays it. Index.html contains a form that takes a user name as input and submits it to Serv1.

Uploaded by

arjun kumar
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)
16 views3 pages

Session

The document contains code for two Java servlets (Serv1 and Serv2) and an HTML file (Index.html). Serv1 retrieves a user name from a request, stores it in the session, and provides a link to Serv2. Serv2 retrieves the stored user name from the session and displays it. Index.html contains a form that takes a user name as input and submits it to Serv1.

Uploaded by

arjun kumar
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/ 3

Serv1.

java
package 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 Serv1 extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {

try{

response.setContentType("text/html");

PrintWriter out=response.getWriter();

String n= request.getParameter("user");

out.print("Welcom : --"+n);

HttpSession ses=request.getSession();

ses.setAttribute("uname",n);

out.print("<a href='Serv2'> Visit></a>");

out.close();

catch(Exception e){

System.out.print(e);}

}
Serv2.java
package 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 Serv2 extends HttpServlet {

private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {

try{

response.setContentType("text/html");

PrintWriter out=response.getWriter();

HttpSession ses=request.getSession(false);

String n=(String)ses.getAttribute("uname");

out.print("Hello : "+n);

out.close();

}catch(Exception e){

System.out.print(e);}

}
Index.html
<!DOCTYPE html>

<html>

<head>

<meta charset="ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<form method="get" action="Serv1">

Name:<input type="text" name="user"><br>

<input type="submit" value="GO">

</form>

</body>

</html>

Output:

You might also like