You might consider writing a servlet class that your Servlets and JSP extend
from.
In this class implement the service method and also read in system
properties that can be used to set the default error page, login-required
page, etc.
>From within your service method you can call methods implement default
behavior that your JSP's can override if necessary. You could also put your
common session code here so that you would not need to include it in your
JSP code. The service method would also eventually invoke the
super.service(HttpServletRequest request,HttpServletResponse response)
method after the session checking is performed.
You would also have a class that extends from this BaseServlet class that
your servlets would in turn extend and also another class that extends this
servlet that your JSPs would extend from.
In your BaseJSP class, you would have methods like:
public void
doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException
{
_jspService(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse
response)
throws IOException, ServletException
{
_jspService(request,response);
}
This allows your JSP's that extend BaseJSP to work under this 'framework'
Your JSP's will only get invoked if a valid session exists, otherwise the
LoginRequired page will appear.
-Richard Yee
-----Original Message-----
From: Singh, Jasbinder [mailto:[EMAIL PROTECTED]]
Sent: Friday, February 16, 2001 3:18 PM
To: [EMAIL PROTECTED]
Subject: Using session
Hi All,
I am developing an application that requires secured login where the page
should not be reached without logging.
I have a common jsp that has the following scriptlet and also includes the
logo and other header information that has to be displayed.
common.jsp
<%
javax.servlet.http.HttpSession sess = request.getSession(false);
if ( sess.isNew() )
{
System.out.println("Session new");
response.sendRedirect("/login.jsp");
return;
}
else
System.out.println("Session not new");
if ( sess.getValue("user")==null )
{
System.out.println("userid is null");
response.sendRedirect("/login.jsp");
return;
}
%>
I include this jsp in all my other JSP's as
<jsp:include page="/body.jsp" flush="true" />
When I try to invoke a page directly by typing the url, I get the above
println statements
correctly("Session new" and "userid is null"), the included common.jsp page
is not displayed but the rest of the page is displayed. The conrol doesn't
go to the login.jsp. But when I invoke the common.jsp directly, the control
does go to the login.jsp page.
Am I missing something ? Is this possible or I have to include a similar
scriptlet in every jsp that I write.
Thanks in advance
JS
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets