0

I'm new to JSP, and would like to offer users an option to logout from their current session.

I was wondering if it is possible to call a method which contains the logging-out logic, from a link tag (i.e. <a href=...>).

Something like this:

out.println("You are already logged in. Click <a href="methodName()">here</a> to go to the login page");

Is this possible?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Dot NET
  • 4,891
  • 13
  • 55
  • 98
  • Hover the `[jsp]` and `[servlets]` tags which you placed on the question until a black info box shows up and then click therein the *info* link to get started with JSP/Servlet the right way. – BalusC Nov 20 '12 at 16:40

2 Answers2

0

You can't directly call a method like that. You need to call URL of the Servlet. Servlet should contain logout logic.

Another way you can embed logout logic in JSP itself using scriptlets (Which is very bad approach by the way).

kosa
  • 65,990
  • 13
  • 130
  • 167
  • So do I need to create a servlet just for this functionality? – Dot NET Nov 20 '12 at 16:02
  • Thanks @Nambari. These things take some getting used to when coming from an ASP.NET environment :) – Dot NET Nov 20 '12 at 16:06
  • Agree. I am having same learning curve with Dot NET too. ha ha. – kosa Nov 20 '12 at 16:07
  • So is this all it takes? here – Dot NET Nov 20 '12 at 16:10
  • It depends on how you mapped LogoutServlet. Mapping url should match here. – kosa Nov 20 '12 at 16:12
  • I've just called in LogoutServlet.jsp. Is there something else I have to do? – Dot NET Nov 20 '12 at 16:24
  • Shouldn't be LogoutServlet.java? I would suggest don't use JSP unless you want to render something and spend sometime in understanding basics http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/ – kosa Nov 20 '12 at 16:26
  • Sorry my mistake. It is infact LogoutServlet.java. I've just added that in the href path, but it's not finding it – Dot NET Nov 20 '12 at 16:27
  • If you are using annotations, you might have defined url there, otherwise you might have defined mapping in web.xml. Using one of these ways you need to define mapping between Servlet class and url. – kosa Nov 20 '12 at 16:28
  • Dot NET, please don't compare ASP.NET(MVC) with JSP. They've definitely not the same ideology. You've indeed to bring in quite some boilerplate MVC code in flavor of servlets yourself. You've probably had Java EE's real MVC framework JSF in mind. In C#/NET terms, JSP is more comparable to "Classic ASP". See also http://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp for an elaborate explanation. – BalusC Nov 20 '12 at 16:38
0

You just can't call a method directily from a link.

First of all, you must understand the JSP lifecycle (see Java EE Tutorial). To do so, you must call an Servlet that contains the logout logic using the Servlet's URI in href paramater.

Instead of learning JSP, learn about JSF. It's easier, more powerful and has a lot of cool features. A good JSF implementation is Primefaces.

Just a last advice; before coding, LEARN the concepts about JSF. You'll be a better developer and at the end it's faster when you learn JSF on the right way.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Rafael Borja
  • 4,487
  • 7
  • 29
  • 33