Rewriting getAttribute() scriptlet as a JSTL in JSP/HTML?












0














I have a scriptlet within my JSP page that gets an attribute "username" passed from a previous serlvet. The scriptlet validates if the "username" attribute was set, if not it denies access to homepage and instead redirects to the login page:



<%
String validUser = (String) session.getAttribute("username");
if (validUser == null){
session.setAttribute("errorMsg", "Access Denied: Please login to access this page");
session.setAttribute("username", "");
response.sendRedirect("LoginFormError.jsp");
}
%>


Since scriptlets in JSP/HTML code is not ideal, how do I go about re-writing this scriptlet as a JSTL instead?



EDIT:



Ok, so far here's what I've got:



<c:set var="validUser" value='${param.username}' />

<c:if test = "${validUser == null"}
<c:set var="errorMsg" value="${'Access Denied: Please login to access this page'}"/>
<c:set var="username" value=""/>
<c:redirect url="LoginFormError.jsp"/>
</c:if>


Are the getAttribute() and setAttribute() done right?










share|improve this question





























    0














    I have a scriptlet within my JSP page that gets an attribute "username" passed from a previous serlvet. The scriptlet validates if the "username" attribute was set, if not it denies access to homepage and instead redirects to the login page:



    <%
    String validUser = (String) session.getAttribute("username");
    if (validUser == null){
    session.setAttribute("errorMsg", "Access Denied: Please login to access this page");
    session.setAttribute("username", "");
    response.sendRedirect("LoginFormError.jsp");
    }
    %>


    Since scriptlets in JSP/HTML code is not ideal, how do I go about re-writing this scriptlet as a JSTL instead?



    EDIT:



    Ok, so far here's what I've got:



    <c:set var="validUser" value='${param.username}' />

    <c:if test = "${validUser == null"}
    <c:set var="errorMsg" value="${'Access Denied: Please login to access this page'}"/>
    <c:set var="username" value=""/>
    <c:redirect url="LoginFormError.jsp"/>
    </c:if>


    Are the getAttribute() and setAttribute() done right?










    share|improve this question



























      0












      0








      0







      I have a scriptlet within my JSP page that gets an attribute "username" passed from a previous serlvet. The scriptlet validates if the "username" attribute was set, if not it denies access to homepage and instead redirects to the login page:



      <%
      String validUser = (String) session.getAttribute("username");
      if (validUser == null){
      session.setAttribute("errorMsg", "Access Denied: Please login to access this page");
      session.setAttribute("username", "");
      response.sendRedirect("LoginFormError.jsp");
      }
      %>


      Since scriptlets in JSP/HTML code is not ideal, how do I go about re-writing this scriptlet as a JSTL instead?



      EDIT:



      Ok, so far here's what I've got:



      <c:set var="validUser" value='${param.username}' />

      <c:if test = "${validUser == null"}
      <c:set var="errorMsg" value="${'Access Denied: Please login to access this page'}"/>
      <c:set var="username" value=""/>
      <c:redirect url="LoginFormError.jsp"/>
      </c:if>


      Are the getAttribute() and setAttribute() done right?










      share|improve this question















      I have a scriptlet within my JSP page that gets an attribute "username" passed from a previous serlvet. The scriptlet validates if the "username" attribute was set, if not it denies access to homepage and instead redirects to the login page:



      <%
      String validUser = (String) session.getAttribute("username");
      if (validUser == null){
      session.setAttribute("errorMsg", "Access Denied: Please login to access this page");
      session.setAttribute("username", "");
      response.sendRedirect("LoginFormError.jsp");
      }
      %>


      Since scriptlets in JSP/HTML code is not ideal, how do I go about re-writing this scriptlet as a JSTL instead?



      EDIT:



      Ok, so far here's what I've got:



      <c:set var="validUser" value='${param.username}' />

      <c:if test = "${validUser == null"}
      <c:set var="errorMsg" value="${'Access Denied: Please login to access this page'}"/>
      <c:set var="username" value=""/>
      <c:redirect url="LoginFormError.jsp"/>
      </c:if>


      Are the getAttribute() and setAttribute() done right?







      java jsp jstl scriptlet getattribute






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 0:42

























      asked Nov 20 '18 at 23:48









      James McTyre

      535




      535
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Good to see you doing this, You can do using the jstl/core taglib



          <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

          <c:if test="${sessionScope.username == null}">
          <c:redirect url="LoginFormError.jsp" />
          </c:if>





          share|improve this answer





















          • I did an edit before I saw your answer. I'm wondering if ${param.attr["username"]} is equivalent to getAttribute("username")?
            – James McTyre
            Nov 21 '18 at 0:37












          • I thought the username was a session variable as per your original post. Why don't you try what I have posted?
            – Scary Wombat
            Nov 21 '18 at 0:40












          • Ah, yes. Sorry about that, I was following a JSTL guide that had attr as the session variable. Yes, username is my correct variable.
            – James McTyre
            Nov 21 '18 at 0:42












          • What's the difference between ${param.username} and ${sessionScope.username}? A contributor suggests using param according to his answer here (2nd answer): stackoverflow.com/questions/4912690/…
            – James McTyre
            Nov 21 '18 at 0:45










          • Yes, that was my understanding i.e. username would be part of the queryString, but your question posted it as String validUser = (String) session.getAttribute("username"); which is a session variable. Only you can say which is correct. But anyway, please modify my answer as per your requirements.
            – Scary Wombat
            Nov 21 '18 at 0:47













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403323%2frewriting-getattribute-scriptlet-as-a-jstl-in-jsp-html%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Good to see you doing this, You can do using the jstl/core taglib



          <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

          <c:if test="${sessionScope.username == null}">
          <c:redirect url="LoginFormError.jsp" />
          </c:if>





          share|improve this answer





















          • I did an edit before I saw your answer. I'm wondering if ${param.attr["username"]} is equivalent to getAttribute("username")?
            – James McTyre
            Nov 21 '18 at 0:37












          • I thought the username was a session variable as per your original post. Why don't you try what I have posted?
            – Scary Wombat
            Nov 21 '18 at 0:40












          • Ah, yes. Sorry about that, I was following a JSTL guide that had attr as the session variable. Yes, username is my correct variable.
            – James McTyre
            Nov 21 '18 at 0:42












          • What's the difference between ${param.username} and ${sessionScope.username}? A contributor suggests using param according to his answer here (2nd answer): stackoverflow.com/questions/4912690/…
            – James McTyre
            Nov 21 '18 at 0:45










          • Yes, that was my understanding i.e. username would be part of the queryString, but your question posted it as String validUser = (String) session.getAttribute("username"); which is a session variable. Only you can say which is correct. But anyway, please modify my answer as per your requirements.
            – Scary Wombat
            Nov 21 '18 at 0:47


















          0














          Good to see you doing this, You can do using the jstl/core taglib



          <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

          <c:if test="${sessionScope.username == null}">
          <c:redirect url="LoginFormError.jsp" />
          </c:if>





          share|improve this answer





















          • I did an edit before I saw your answer. I'm wondering if ${param.attr["username"]} is equivalent to getAttribute("username")?
            – James McTyre
            Nov 21 '18 at 0:37












          • I thought the username was a session variable as per your original post. Why don't you try what I have posted?
            – Scary Wombat
            Nov 21 '18 at 0:40












          • Ah, yes. Sorry about that, I was following a JSTL guide that had attr as the session variable. Yes, username is my correct variable.
            – James McTyre
            Nov 21 '18 at 0:42












          • What's the difference between ${param.username} and ${sessionScope.username}? A contributor suggests using param according to his answer here (2nd answer): stackoverflow.com/questions/4912690/…
            – James McTyre
            Nov 21 '18 at 0:45










          • Yes, that was my understanding i.e. username would be part of the queryString, but your question posted it as String validUser = (String) session.getAttribute("username"); which is a session variable. Only you can say which is correct. But anyway, please modify my answer as per your requirements.
            – Scary Wombat
            Nov 21 '18 at 0:47
















          0












          0








          0






          Good to see you doing this, You can do using the jstl/core taglib



          <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

          <c:if test="${sessionScope.username == null}">
          <c:redirect url="LoginFormError.jsp" />
          </c:if>





          share|improve this answer












          Good to see you doing this, You can do using the jstl/core taglib



          <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

          <c:if test="${sessionScope.username == null}">
          <c:redirect url="LoginFormError.jsp" />
          </c:if>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 0:27









          Scary Wombat

          35k32252




          35k32252












          • I did an edit before I saw your answer. I'm wondering if ${param.attr["username"]} is equivalent to getAttribute("username")?
            – James McTyre
            Nov 21 '18 at 0:37












          • I thought the username was a session variable as per your original post. Why don't you try what I have posted?
            – Scary Wombat
            Nov 21 '18 at 0:40












          • Ah, yes. Sorry about that, I was following a JSTL guide that had attr as the session variable. Yes, username is my correct variable.
            – James McTyre
            Nov 21 '18 at 0:42












          • What's the difference between ${param.username} and ${sessionScope.username}? A contributor suggests using param according to his answer here (2nd answer): stackoverflow.com/questions/4912690/…
            – James McTyre
            Nov 21 '18 at 0:45










          • Yes, that was my understanding i.e. username would be part of the queryString, but your question posted it as String validUser = (String) session.getAttribute("username"); which is a session variable. Only you can say which is correct. But anyway, please modify my answer as per your requirements.
            – Scary Wombat
            Nov 21 '18 at 0:47




















          • I did an edit before I saw your answer. I'm wondering if ${param.attr["username"]} is equivalent to getAttribute("username")?
            – James McTyre
            Nov 21 '18 at 0:37












          • I thought the username was a session variable as per your original post. Why don't you try what I have posted?
            – Scary Wombat
            Nov 21 '18 at 0:40












          • Ah, yes. Sorry about that, I was following a JSTL guide that had attr as the session variable. Yes, username is my correct variable.
            – James McTyre
            Nov 21 '18 at 0:42












          • What's the difference between ${param.username} and ${sessionScope.username}? A contributor suggests using param according to his answer here (2nd answer): stackoverflow.com/questions/4912690/…
            – James McTyre
            Nov 21 '18 at 0:45










          • Yes, that was my understanding i.e. username would be part of the queryString, but your question posted it as String validUser = (String) session.getAttribute("username"); which is a session variable. Only you can say which is correct. But anyway, please modify my answer as per your requirements.
            – Scary Wombat
            Nov 21 '18 at 0:47


















          I did an edit before I saw your answer. I'm wondering if ${param.attr["username"]} is equivalent to getAttribute("username")?
          – James McTyre
          Nov 21 '18 at 0:37






          I did an edit before I saw your answer. I'm wondering if ${param.attr["username"]} is equivalent to getAttribute("username")?
          – James McTyre
          Nov 21 '18 at 0:37














          I thought the username was a session variable as per your original post. Why don't you try what I have posted?
          – Scary Wombat
          Nov 21 '18 at 0:40






          I thought the username was a session variable as per your original post. Why don't you try what I have posted?
          – Scary Wombat
          Nov 21 '18 at 0:40














          Ah, yes. Sorry about that, I was following a JSTL guide that had attr as the session variable. Yes, username is my correct variable.
          – James McTyre
          Nov 21 '18 at 0:42






          Ah, yes. Sorry about that, I was following a JSTL guide that had attr as the session variable. Yes, username is my correct variable.
          – James McTyre
          Nov 21 '18 at 0:42














          What's the difference between ${param.username} and ${sessionScope.username}? A contributor suggests using param according to his answer here (2nd answer): stackoverflow.com/questions/4912690/…
          – James McTyre
          Nov 21 '18 at 0:45




          What's the difference between ${param.username} and ${sessionScope.username}? A contributor suggests using param according to his answer here (2nd answer): stackoverflow.com/questions/4912690/…
          – James McTyre
          Nov 21 '18 at 0:45












          Yes, that was my understanding i.e. username would be part of the queryString, but your question posted it as String validUser = (String) session.getAttribute("username"); which is a session variable. Only you can say which is correct. But anyway, please modify my answer as per your requirements.
          – Scary Wombat
          Nov 21 '18 at 0:47






          Yes, that was my understanding i.e. username would be part of the queryString, but your question posted it as String validUser = (String) session.getAttribute("username"); which is a session variable. Only you can say which is correct. But anyway, please modify my answer as per your requirements.
          – Scary Wombat
          Nov 21 '18 at 0:47




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403323%2frewriting-getattribute-scriptlet-as-a-jstl-in-jsp-html%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ottavio Pratesi

          Tricia Helfer

          15 giugno