Checking the value of a servlet session attribute in a jsp file

I do not have a java framework application. It consists of jsp files for viewing and servlets for business logic. I have to establish a user session, this is a servlet with the firstName parameter. In the jsp file, I need to check if the value of the firstName parameter has a value or not. If the firstName parameter is set, I need to display some html in the jsp file. If it is not installed, I need to display different html in the jsp file.

Servlet.java:

HttpSession session = request.getSession(); session.setAttribute("firstName", customer.getFristName()); String url = "/index.jsp"; RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url); dispatcher.forward(request, response); 

header.jsp:

 // Between the <p> tags bellow I need to put some HTML with the following rules // If firstName exist: Hello ${firstName} <a href="logout.jsp">Log out</a> // Else: <a href="login.jsp">Login</a> or <a href="register.jsp">Register</a> <p class="credentials" id="cr"></p> 

What would be the best way to do this?

Update:

Here is a great tutorial that I found on JSTL if anyone needs it: http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm

+8
jsp servlets
source share
3 answers
 <% if (session.getAttribute("firstName") == null) { %> <p> some content </p> <% } else {%> <p> other content </p> <% } %> 
+9
source share

I think the best way to do this is to use jstl tags. Since for a simple jsp application, it might be a good idea to add all java codes to html but a heavier application, it is best to use minimal html Java code. (Separate presentation layer from the logic) (read this for more https://stackoverflow.com/a/167609/ )
For your expectation, you can easily use the code like below.

 <c:if test="${not empty firstName}"> <%--If you want to print content from session--%> <p>${firstName}</p> <%--If you want to include html--%> <%@include file="/your/path/to/include/file.jsp" %> <%--include only get wrong if you give the incorrect file path --%> </c:if> <c:if test="${empty firstName}"> <p>Jaathi mcn Jaathi</p> </c:if> 

If you did not enable jstl correctly, you will not be able to get the expected result. see this for such an incident https://menukablog.wordpress.com/2016/05/10/add-jstl-tab-library-to-you-project-correctly/

+1
source share

In a servlet, you can write as follows

  HttpSession session = request.getSession(true); session.setAttribute("firstName", customer.getFristName()) response.sendRedirect("index.jsp"); 

request.getSession(true) returns a new session if no sessions exist, otherwise it will return the current session. And on the index.jsp page index.jsp you can do the following:

 <% if(session.getAttribute("firstName")==null) { %> <jsp:include page="firstPage.html"></jsp:include> <% } else { %> <jsp:include page="secondPage.html"></jsp:include> <% }%> 

Here, if firstName is null, then firstPage.html will be included in the page, otherwise secondPage.html .

0
source share

All Articles