Reputation: 4294
I'm passing data from my browser to HomeController.java
by just changing the URL,
My HomeController.java
as follows,
package com.example.demo;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("home")
public String home(HttpServletRequest req) {
HttpSession session=req.getSession();
String name=req.getParameter("name");
//Fetch data comming from client
System.out.println("hi "+name);
session.setAttribute(name, name);
return "home";
}
}
I'm passing session attribute using session
object but how should I fetch that in my JSP file(home.jsp
).
my home.jsp
looks like :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Welcome ${name}
</body>
</html>
I want to write java code in home.jsp
file and use the session object instead of the expression language
format. Can anyone tell how to use that session object?
Thanks in advance!
Upvotes: 1
Views: 796
Reputation: 2817
To answer your question directly, you can call your variable from the session object like so :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Welcome <%=session.getAttribute("name")%>
</body>
</html>
Where name
is the name of the object stored in your session.
PS : use a Logger instead of System.out
Upvotes: 4