Cookie and Httpsession is important in terms of session managemnet, Cookie will help to identify client even after logoff from session, but seesion maintains current activity while client is connected with server.
HttpSession session=req.getSession();
Cookie c= new Cookie("name"+,"value");
to access Cookie, we should store in an array and use foreach loop to getName() from cookies
Cookie[] c=req.getCookies();
beans definition should be written in cofiguration xml file
the bean definition for XMl config
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean name="car" class="Car"></bean>
</beans>
``````````````````````````````````````````````````````````````````````````````````````````
####annotation based
for annotation based we also need definition in xml
definition for xml annotation
<context:component-scan base-package="l"></context:component-scan>
STS-4 --> MAVEN project in Spring starter --> add tools of web and java ee from eclips marketplace in sts else jsp will not created --> add tomcat jasper in dependency for jsp to servlet
this annoted class is a main (Dispatcher servlet)
IT will look for Controller where we marked Controller to a class
and @RequestMapping("/") will map the required method and returns the view.jsp file
src-main-webapp-a.jsp
spring.datasource.url=jdbc:mysql://localhost:3306/testhibernate
spring.datasource.username=root
spring.datasource.password=8052608851
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.ddl-auto=update
{
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.2.RELEASE
<groupId>com.example</groupId>
<artifactId>firstBootMVC</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>firstBootMVC</name>
<description>first project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Add anotation as @ResponseBody which will let view know that ,Now he will convert response to json
use Optional (for handling null also)