Add files via upload
This commit is contained in:
parent
7ff9bc6ac8
commit
ccfbdd3f9c
5
spring-session/http-session-example/README.md
Normal file
5
spring-session/http-session-example/README.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
## HttpSession
|
||||||
|
|
||||||
|
This module contains article about Difference Between request.getSession() and request.getSession(true)
|
||||||
|
|
||||||
|
### Relevant Articles:
|
26
spring-session/http-session-example/pom.xml
Normal file
26
spring-session/http-session-example/pom.xml
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<project
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.baeldung.httpsession</groupId>
|
||||||
|
<artifactId>http-session-example</artifactId>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>http-session-example Maven Webapp</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-boot-2</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../parent-boot-2</relativePath>
|
||||||
|
</parent>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>servlet-api</artifactId>
|
||||||
|
<version>2.5</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
@ -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("<br/><a href='second'>Second Servlet</a>");
|
||||||
|
|
||||||
|
out.close();
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://java.sun.com/xml/ns/javaee"
|
||||||
|
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
||||||
|
id="WebApp_ID" version="3.0">
|
||||||
|
|
||||||
|
<display-name>httpsession</display-name>
|
||||||
|
<welcome-file-list>
|
||||||
|
<welcome-file>index.html</welcome-file>
|
||||||
|
</welcome-file-list>
|
||||||
|
|
||||||
|
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>FirstServlet</servlet-name>
|
||||||
|
<servlet-class>com.baeldung.httpsession.FirstServlet</servlet-class>
|
||||||
|
</servlet>
|
||||||
|
<servlet>
|
||||||
|
<servlet-name>SecondServlet</servlet-name>
|
||||||
|
<servlet-class>com.baeldung.httpsession.SecondServlet</servlet-class>
|
||||||
|
</servlet>
|
||||||
|
|
||||||
|
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>FirstServlet</servlet-name>
|
||||||
|
<url-pattern>/first</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
<servlet-mapping>
|
||||||
|
<servlet-name>SecondServlet</servlet-name>
|
||||||
|
<url-pattern>/second</url-pattern>
|
||||||
|
</servlet-mapping>
|
||||||
|
|
||||||
|
|
||||||
|
</web-app>
|
@ -0,0 +1,12 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="ISO-8859-1">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<form action="first">
|
||||||
|
Name: <input type="text" name="userName" /><br /> <br /> <input
|
||||||
|
type="submit" value="submit" />
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
x
Reference in New Issue
Block a user