11

I have a no framework java application. It consists of jsp files for view and servlets for the business logic. I must set the user session is the servlet with a firstName parameter. In the jsp file, I need to check if my 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 set, 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 I found on JSTL, in case anyone needs it: http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm

Marta
  • 252
  • 3
  • 7
  • 16

3 Answers3

10
<% if (session.getAttribute("firstName") == null) { %>
    <p> some content </p>
<% } else {%>
    <p> other content </p>
<% } %>
Isaac
  • 2,701
  • 4
  • 30
  • 47
  • Thank you Ibrahim! This solution is clean and simple. It did exactly what I wanted it to do. I was messing with jsp EL and it was getting really messy. :) – Marta Nov 30 '12 at 03:25
  • 5
    Messy? Apparently you did something wrong. With JSTL/EL it's as simple as `

    some content

    other content

    `. I'm not sure how that's more messy.
    – BalusC Dec 04 '12 at 01:59
  • @BalusC I sure did! I was looking for an if-else construct in EL, and couldn't get it to work, so I abandoned the idea. Looking at your example, it would do what I wanted also. I will probably use this one instead of putting java code in my JSP. I read that it is better practice to use EL/JSTL than java in your JSPs. Thanks for your help! – Marta Dec 04 '12 at 16:34
  • 1
    Very bad...! Use of scriptlets is worst idea. – Shailesh Saxena Sep 13 '13 at 04:54
  • see answer from @Menuka Ishan, using jstl - way to go. – magulla Oct 26 '16 at 17:35
4

I think best way to do it is use of jstl tags. Because for simple jsp application it might good idea to add all java code to html but more heavier application it is best practice to use minimum java code on html.(seperate view layer from logic) (read this for more https://stackoverflow.com/a/3180202/2940265)
For your expectation you can easily use code like bellow

<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 didn't included jstl correctly you will unable to get expected output. refer this for such incident https://menukablog.wordpress.com/2016/05/10/add-jstl-tab-library-to-you-project-correctly/

Community
  • 1
  • 1
Menuka Ishan
  • 5,164
  • 3
  • 50
  • 66
1

In the servlet you can write as follows

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

The request.getSession(true) returns a new session if it doesn't exist any session otherwise it will return the current session. And, in the index.jsp page you can do as follows :

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

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

Visruth
  • 3,430
  • 35
  • 48
  • Thank you Visruth! Great explanation on the getSession(true). – Marta Nov 30 '12 at 14:54
  • 1
    The `request.getSession()` does **exactly** the same under the covers. See also [the javadoc](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getSession()). In fact, the `true` argument is totally superfluous. – BalusC Dec 04 '12 at 01:57