diff --git a/spring-session/http-session-example/README.md b/spring-session/http-session-example/README.md new file mode 100644 index 0000000000..95c7a41e2d --- /dev/null +++ b/spring-session/http-session-example/README.md @@ -0,0 +1,5 @@ +## HttpSession + +This module contains article about Difference Between request.getSession() and request.getSession(true) + +### Relevant Articles: diff --git a/spring-session/http-session-example/pom.xml b/spring-session/http-session-example/pom.xml new file mode 100644 index 0000000000..a77176fb4b --- /dev/null +++ b/spring-session/http-session-example/pom.xml @@ -0,0 +1,26 @@ + + 4.0.0 + com.baeldung.httpsession + http-session-example + war + 0.0.1-SNAPSHOT + http-session-example Maven Webapp + http://maven.apache.org + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + javax.servlet + servlet-api + 2.5 + provided + + + diff --git a/spring-session/http-session-example/src/main/java/com/baeldung/httpsession/FirstServlet.java b/spring-session/http-session-example/src/main/java/com/baeldung/httpsession/FirstServlet.java new file mode 100644 index 0000000000..950bea2c07 --- /dev/null +++ b/spring-session/http-session-example/src/main/java/com/baeldung/httpsession/FirstServlet.java @@ -0,0 +1,33 @@ +package com.baeldung.httpsession; + +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 FirstServlet extends HttpServlet { + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + try { + HttpSession session = request.getSession(); + response.setContentType("text/html"); + PrintWriter out = response.getWriter(); + + String name = request.getParameter("userName"); + session.setAttribute("uname", name); + out.println("Hi " + name + " Your Session Id is : " + session.getId() + " "); + + out.println("
Second Servlet"); + + out.close(); + + } catch (Exception e) { + System.out.println(e); + } + } + +} diff --git a/spring-session/http-session-example/src/main/java/com/baeldung/httpsession/SecondServlet.java b/spring-session/http-session-example/src/main/java/com/baeldung/httpsession/SecondServlet.java new file mode 100644 index 0000000000..6a5ef7e9a8 --- /dev/null +++ b/spring-session/http-session-example/src/main/java/com/baeldung/httpsession/SecondServlet.java @@ -0,0 +1,31 @@ +package com.baeldung.httpsession; + +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 SecondServlet extends HttpServlet { + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + try { + + response.setContentType("text/html"); + PrintWriter out = response.getWriter(); + + HttpSession session = request.getSession(true); + String name = (String) session.getAttribute("uname"); + out.println("Hi " + name + " Your Session Id is : " + session.getId()); + + out.close(); + + } catch (Exception e) { + System.out.println(e); + } + } + +} diff --git a/spring-session/http-session-example/src/main/webapp/WEB-INF/web.xml b/spring-session/http-session-example/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..2c9a4c118b --- /dev/null +++ b/spring-session/http-session-example/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,33 @@ + + + + httpsession + + index.html + + + + + FirstServlet + com.baeldung.httpsession.FirstServlet + + + SecondServlet + com.baeldung.httpsession.SecondServlet + + + + + FirstServlet + /first + + + SecondServlet + /second + + + + \ No newline at end of file diff --git a/spring-session/http-session-example/src/main/webapp/index.html b/spring-session/http-session-example/src/main/webapp/index.html new file mode 100644 index 0000000000..0e5889f21a --- /dev/null +++ b/spring-session/http-session-example/src/main/webapp/index.html @@ -0,0 +1,12 @@ + + + + + + +
+ Name:

+
+ + \ No newline at end of file