index.html
[html]
<html>
<form action="./welcome">
Enter name:<input type="text" name="username">
Enter Email:<input type="text" name="emailId">
<input type="submit" value="submit">
</form>
</html>
[/html]
Here the developer just created two text boxes one is to
enter the name and second one is
enter email.
web.xml
[xml]
<web-app>
<servlet>
<servlet-name>
DemoSessionId
</servlet-name>
<servlet-class>
DemoSessionId
</servlet-class>
</servlet>
<servlet>
<servlet-name>
SessionId
</servlet-name>
<servlet-class>
SessionId
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>
DemoSessionId
</servlet-name>
<url-pattern>
/welcome
</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>
SessionId
</servlet-name>
<url-pattern>
/servlet2
</url-pattern>
</servlet-mapping>
</web-app>
[/xml]
Make sure that should be same and also
URL name should be match with HTML form.
DemoSessionId.java
[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 DemoSessionId extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//get the value from the text from HTML
String uname = request.getParameter("username");
String email = request.getParameter("emailId");
//Create HttpSession object
HttpSession session = request.getSession();
//set the username in the session
session.setAttribute("uname", uname);
session.setAttribute("emailId", email);
out.print("<h2>Enter your country:<input type='text' name='country'></h2>");
out.println("<a href='servlet2'>
<h2>go</h2>
</a>");
out.flush();
out.close();
}
}
[/java]
get the value from the text from HTML.
[java]String uname = request.getParameter("username");
String email = request.getParameter("emailId");[/java]
Create HttpSession object.
[java] HttpSession session = request.getSession();[/java]
Set the username in the session.
[java]session.setAttribute("uname", uname);
session.setAttribute("emailId", email);[/java]
SessioId.java
[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 SessionId extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//get the values from the request
String country = request.getParameter("country");
HttpSession session = request.getSession(false);
//This from get sessionID
String sessionId = session.getId();
//Get the username from the session
String userName = (String)session.getAttribute("uname");
//Get the emailId from the session
String email = (String)session.getAttribute("emailId");
out.println("<h2>This is your name get from the session::"+userName+"</h2>");
out.println("<h2>This is your emailId get from the session::"+email+"</h2>");
out.println("<h2>This is your country Show it null because it is not in session ::"+country+"</h2>");
out.println("<h2>This is the sessionId value :::"+sessionId+"</h2>");
out.flush();
out.close();
}
}[/java]
Get the values from the request.
[java]String country = request.getParameter("country");
HttpSession session = request.getSession(false);[/java]
This from get sessionID.
[java]String sessionId = session.getId();[/java]
Get the username from the session
[java]String userName = (String)session.getAttribute("uname");[/java]
Get the emailId from the session.
[java]String email = (String)session.getAttribute("emailId"); [/java]
Output
Following is the page generated from the HTML where details can be enterd.
Following is the servlet2 page where enter the country name.
Here the total information will be displayed.