1

I have two JSP pages. One of them is call validate.jsp and the code is as below:

 String username = request.getParameter("username");
                String password = request.getParameter("password");
                String referrerURL = request.getParameter("referrerURL");

                if(userFacade.validate(username, password) == false){
                    throw new Exception(userFacade.getMsg());
                }

                // Extract the Auser object of the user and stores it in the HttpSession object
                Auser auser = userFacade.get(username, true);
                if (auser == null) {
                    throw new Exception("Invalid user.");
                }

                String usertype = auser.getAusertype();

I have another JSP called email.jsp and I want to call the variable in validate.jsp

<%@include file = "../../../login/validate.jsp" %>

   Auser auser = userFacade.get(username, true);

My directory for include file is definitely correct but however it couldn't resolve username in this JSP.

Can anyone point out any mistake I made?

EDIT:

Found out that username is inside a try catch block and if i placed it outside, I can call the variable. Is there a way to call it inside a try catch from another jsp?

Daredevil
  • 1,672
  • 3
  • 18
  • 47
  • It seems worthwile for you to learn a bit on JSPs, because JSP comes with several techniques. Maybe some demo course or such. (Meant well.) – Joop Eggen Mar 29 '19 at 10:28
  • And how is my technique wrong? – Daredevil Mar 29 '19 at 10:31
  • it's wrong because you don't write your java code inside the jsp. – Jonathan Laliberte Mar 29 '19 at 10:33
  • One can put a user object into the session, and much more. It might not be better, but for instance reduces database usage. Or you might use a method (`<%!`) or import methods/classes. Probably you know some techniques, so it really was my feeling that it would be to your advantage to dive into techniques, such as tag libraries. Model-View-Controller paradigm using a preceding servlet and so on. – Joop Eggen Mar 29 '19 at 10:36

1 Answers1

0

You mentioned that removing the try/catch block "fixed" the issue. If that is the case then just define the variable outside the block, otherwise the scope is limited to only inside the block.

String username = "";
try {
    username = request.getParameter("username");
    ...

} catch (...)

Would something like this make sense?

käyrätorvi
  • 371
  • 1
  • 9
  • It does help but when I execute the JSP,it throws an error saying a variable already exists. – Daredevil Mar 29 '19 at 10:31
  • Could you share the stack trace that you're getting? I quickly tried a similar construct and I was able to run it succesfully. As others have mentioned, probably is indeed best to avoid such design altogether. – käyrätorvi Mar 29 '19 at 10:50
  • The reason is because the validate.jsp is a login page which captures the "username" and somewhere in the another jsp page, i need to get the same value of the username – Daredevil Mar 29 '19 at 10:53
  • I tried debugging but it keeps returning null value to my caller page,why is that? – Daredevil Mar 29 '19 at 10:53
  • Seems like when I use include tag, it calls validate.jsp to run again to the username returns null. Is there a way I can just get the value of the username without running the validate.jsp but i need that variable value. – Daredevil Mar 29 '19 at 10:59
  • I'm pretty sure you need to reconsider the design of your system. Try storing the necessary information in session instead of getting it from request. Check out https://stackoverflow.com/questions/17419727/how-to-use-session-in-jsp-pages-to-get-information. – käyrätorvi Mar 29 '19 at 11:21