You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Julien Martin <ju...@wanadoo.fr> on 2003/10/11 13:51:59 UTC
Question about the Petstore's SignOnFilter class
Hello,
I am going through the Petstore's SignOnFilter class and I am wondering why it has been named SignOnFilter and not SignInFilter. Isn't the businesss logic of the class to help the signing in and not the signing on? Am I right or wrong?
Thanks in advance for your replies.
Julien.
Here is the source for the class:
package com.sun.j2ee.blueprints.signon.web;import java.io.PrintWriter;import java.io.OutputStreamWriter;import java.io.IOException;import java.util.HashMap;import java.util.Iterator;import java.net.URL;// J2EE importsimport javax.servlet.ServletException;import javax.servlet.ServletContext;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.http.HttpSession;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.Cookie;import javax.ejb.CreateException;import javax.naming.NamingException;import javax.naming.InitialContext;// SignOn EJB Importsimport com.sun.j2ee.blueprints.signon.ejb.SignOnLocalHome;import com.sun.j2ee.blueprints.signon.ejb.SignOnLocal;public class SignOnFilter implements Filter { // these static strings define where to put/get things public static final String FORM_SIGNON_URL = "j_signon_check"; public static final String FORM_USER_NAME = "j_username"; public static final String FORM_PASSWORD = "j_password"; public static final String REMEMBER_USERNAME = "j_remember_username"; public static final String USER_NAME = "j_signon_username"; public static final String SIGNED_ON_USER = "j_signon"; public static final String ORIGINAL_URL = "j_signon_original_url"; public static final String CREATE_USER_URL = "j_create_user"; public static final String COOKIE_NAME = "bp_signon"; private HashMap protectedResources; private FilterConfig config = null; private String signOnErrorPage = null; private String signOnPage = null; private String userCreationError = null; public void init(FilterConfig config) throws ServletException { this.config = config; URL protectedResourcesURL = null; try { protectedResourcesURL = config.getServletContext().getResource("/WEB-INF/signon-config.xml"); SignOnDAO dao = new SignOnDAO(protectedResourcesURL); signOnErrorPage = dao.getSignOnErrorPage(); signOnPage = dao.getSignOnPage(); protectedResources = dao.getProtectedResources(); } catch (java.net.MalformedURLException ex) { System.err.println("SignonFilter: malformed URL exception: " + ex); } } public void destroy() { config = null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest hreq = (HttpServletRequest)request; String currentURI = hreq.getRequestURL().toString(); String currentURL = hreq.getRequestURI(); // get everything after the context root int firstSlash = currentURL.indexOf("/",1); // jump past the starting slash String targetURL = null; if (firstSlash != -1) targetURL = currentURL.substring(firstSlash + 1, currentURL.length()); if ((targetURL != null) && targetURL.equals(FORM_SIGNON_URL)) { validateSignOn(request, response, chain); // jump out of this method return; } // check if the user is signed on boolean signedOn = false; if (hreq.getSession().getAttribute(SIGNED_ON_USER) != null) { signedOn =((Boolean)hreq.getSession().getAttribute(SIGNED_ON_USER)).booleanValue(); } else { hreq.getSession().setAttribute(SIGNED_ON_USER, new Boolean(false)); } // jump to the resource if signed on if (signedOn) { chain.doFilter(request,response); return; } // find out if the patterns match the target URL Iterator it = protectedResources.keySet().iterator(); while (it.hasNext()) { String protectedName = (String)it.next(); ProtectedResource resource = (ProtectedResource)protectedResources.get(protectedName); String urlPattern = resource.getURLPattern(); // now check agains the targetURL if (urlPattern.equals(targetURL)) { // put the orginal url in the session so others can access hreq.getSession().setAttribute(ORIGINAL_URL, targetURL); config.getServletContext().getRequestDispatcher("/" + signOnPage).forward(request, response); // Jump out of the filter and go to the next page return; } } // No matches if we made it to here chain.doFilter(request,response); } public void validateSignOn(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // convert to a http servlet request for now HttpServletRequest hreq = (HttpServletRequest)request; HttpServletResponse hres = (HttpServletResponse)response; // get the user name String userName = hreq.getParameter(FORM_USER_NAME); // get the password String password = hreq.getParameter(FORM_PASSWORD); // check if the user wants userName set in cookie String rememberUserName = hreq.getParameter(REMEMBER_USERNAME); if (rememberUserName != null) { // set a cookie with the username in it Cookie userNameCookie = new Cookie(COOKIE_NAME, userName); // set cookie to last for one month userNameCookie.setMaxAge(2678400); hres.addCookie(userNameCookie); } else { // see if the cookie exists and remove accordingly Cookie[] cookies = hreq.getCookies(); if (cookies != null) { for (int loop=0; loop < cookies.length; loop++) { if (cookies[loop].getName().equals(COOKIE_NAME)) { cookies[loop].setMaxAge(0); hres.addCookie(cookies[loop]); } } } } //validate against the registered users SignOnLocal signOn = getSignOnEjb(); boolean authenticated = signOn.authenticate(userName, password); if (authenticated) { // place a true boolean in the session if (hreq.getSession().getAttribute(USER_NAME) != null) { hreq.getSession().removeAttribute(USER_NAME); } hreq.getSession().setAttribute(USER_NAME, userName); // remove the sign on user key before putting it back in if (hreq.getSession().getAttribute(SIGNED_ON_USER) != null) { hreq.getSession().removeAttribute(SIGNED_ON_USER); } hreq.getSession().setAttribute(SIGNED_ON_USER, new Boolean(true)); // redirect to the original destination String targetURL = (String)hreq.getSession().getAttribute(ORIGINAL_URL); hres.sendRedirect(targetURL); return; } else { hres.sendRedirect(signOnErrorPage); return; } } private SignOnLocal getSignOnEjb() throws ServletException { SignOnLocal signOn = null; try { InitialContext ic = new InitialContext(); Object o = ic.lookup("java:comp/env/ejb/SignOn"); SignOnLocalHome home =(SignOnLocalHome)o; signOn = home.create(); } catch (javax.ejb.CreateException cx) { throw new ServletException("Failed to Create SignOn EJB: caught " + cx); } catch (javax.naming.NamingException nx) { throw new ServletException("Failed to Create SignOn EJB: caught " + nx); } return signOn; }}
Re: Difficulty in compiling a servlet.
Posted by Holger Klawitter <li...@klawitter.de>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
> On compiling the addDataServlet.java, an error is coming - Unresolved
> symbol:Class DBUpadate is not resolved
DBUpdate is not in the classpath of the compiler when the servlet is being
compiled.
Mit freundlichem Gruß / With kind regards
Holger Klawitter
- --
lists <at> klawitter <dot> de
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)
iD8DBQE/iAQC1Xdt0HKSwgYRAnGTAJ9mVognPcpOFgLfwBU94CxNvK/9+wCeMw9N
xDuLDar8XqXTOOncKktqvq4=
=p4dV
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
Re: Difficulty in compiling a servlet.
Posted by anunay ashish <an...@tis.co.in>.
I don't have a syntax error any where.
----- Original Message -----
From: "Eric C" <ch...@wanadoo.fr>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Sunday, October 12, 2003 2:00 PM
Subject: Re: Difficulty in compiling a servlet.
> Don't you have a syntax error somewhere ?
>
> DBUpadate
>
>
>
> ----- Original Message -----
> From: "anunay ashish" <an...@tis.co.in>
> To: "Tomcat Users List" <to...@jakarta.apache.org>
> Sent: Saturday, October 11, 2003 2:27 PM
> Subject: Difficulty in compiling a servlet.
>
>
> > Hi,
> >
> > The code for my bean is:
> >
> > package com.scheduler;
> > import java.sql.*;
> > public class DBUpdate
> > {
> > private Connection conn;
> > private Statement stmt;
> > private ResultSet rs;
> > public DBUpdate()
> > {
> > conn = null;
> > stmt = null;
> > rs = null;
> > }
> > public void process(String query)
> > {
> > try
> > {
> >
> Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
> > Connection conn =
> >
DriverManager.getConnection("jdbc:oracle:thin:@192.168.10.36:1521:myData",
> > "mangesh", "mangesh");
> > stmt = conn.createStatement();
> > rs = stmt.executeQuery(query);
> > System.out.println("Query: " + query + " executed");
> > }
> > catch(Exception exception1)
> > {
> > System.out.println("Error occured in DBUpdate.process()
> > SQLException :" + exception1);
> > }
> > }
> > public ResultSet getResultSet() throws SQLException
> > {
> > return rs;
> > }
> > public void destroy()
> > {
> > conn = null;
> > stmt = null;
> > rs = null;
> > }
> > }
> >
> > And for my servlet is:
> >
> > package com.scheduler;
> > import java.io.*;
> > import java.sql.*;
> > import java.text.*;
> > import java.util.*;
> > import javax.servlet.*;
> > import javax.servlet.http.*;
> >
> > public class addDataServlet extends HttpServlet
> > {
> > private String pageFormat;
> > public void doPost(HttpServletRequest request, HttpServletResponse
> > response) throws ServletException, IOException
> > {
> > ResultSet testRS;
> > DBUpdate dbupdate = new DBUpdate();
> > pageFormat = request.getParameter("newPageFormat");
> > PrintWriter out = response.getWriter();
> > out.print(pageFormat);
> > String query = "Select * from lookup_page_format";
> > dbupdate.process(query);
> > try
> > {
> > testRS = dbupdate.getResultSet();
> > while(testRS.next())
> > {
> > out.print(testRS.getString(2));
> > out.print("hi");
> > }
> > }
> > catch (SQLException e)
> > {
> > out.println("SQLException :" + e);
> > }
> > try
> > {
> > //set the attribute and forward to pageFormat.jsp
> > request.setAttribute("servletName", "addDataServlet");
> >
> >
>
getServletConfig().getServletContext().getRequestDispatcher("/Tracking_syste
> > m/pageFormat.jsp").forward(request, response);
> > }
> > catch (Exception ex)
> > {
> > ex.printStackTrace ();
> > }
> > }
> > }
> >
> > On compiling the addDataServlet.java, an error is coming - Unresolved
> > symbol:Class DBUpadate is not resolved
> > Where I am doing wrong?
> >
> > Thanks in advance.
> > Regards,
> > Anunay.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
Re: Difficulty in compiling a servlet.
Posted by Eric C <ch...@wanadoo.fr>.
Don't you have a syntax error somewhere ?
DBUpadate
----- Original Message -----
From: "anunay ashish" <an...@tis.co.in>
To: "Tomcat Users List" <to...@jakarta.apache.org>
Sent: Saturday, October 11, 2003 2:27 PM
Subject: Difficulty in compiling a servlet.
> Hi,
>
> The code for my bean is:
>
> package com.scheduler;
> import java.sql.*;
> public class DBUpdate
> {
> private Connection conn;
> private Statement stmt;
> private ResultSet rs;
> public DBUpdate()
> {
> conn = null;
> stmt = null;
> rs = null;
> }
> public void process(String query)
> {
> try
> {
>
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
> Connection conn =
> DriverManager.getConnection("jdbc:oracle:thin:@192.168.10.36:1521:myData",
> "mangesh", "mangesh");
> stmt = conn.createStatement();
> rs = stmt.executeQuery(query);
> System.out.println("Query: " + query + " executed");
> }
> catch(Exception exception1)
> {
> System.out.println("Error occured in DBUpdate.process()
> SQLException :" + exception1);
> }
> }
> public ResultSet getResultSet() throws SQLException
> {
> return rs;
> }
> public void destroy()
> {
> conn = null;
> stmt = null;
> rs = null;
> }
> }
>
> And for my servlet is:
>
> package com.scheduler;
> import java.io.*;
> import java.sql.*;
> import java.text.*;
> import java.util.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
>
> public class addDataServlet extends HttpServlet
> {
> private String pageFormat;
> public void doPost(HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException
> {
> ResultSet testRS;
> DBUpdate dbupdate = new DBUpdate();
> pageFormat = request.getParameter("newPageFormat");
> PrintWriter out = response.getWriter();
> out.print(pageFormat);
> String query = "Select * from lookup_page_format";
> dbupdate.process(query);
> try
> {
> testRS = dbupdate.getResultSet();
> while(testRS.next())
> {
> out.print(testRS.getString(2));
> out.print("hi");
> }
> }
> catch (SQLException e)
> {
> out.println("SQLException :" + e);
> }
> try
> {
> //set the attribute and forward to pageFormat.jsp
> request.setAttribute("servletName", "addDataServlet");
>
>
getServletConfig().getServletContext().getRequestDispatcher("/Tracking_syste
> m/pageFormat.jsp").forward(request, response);
> }
> catch (Exception ex)
> {
> ex.printStackTrace ();
> }
> }
> }
>
> On compiling the addDataServlet.java, an error is coming - Unresolved
> symbol:Class DBUpadate is not resolved
> Where I am doing wrong?
>
> Thanks in advance.
> Regards,
> Anunay.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
Difficulty in compiling a servlet.
Posted by anunay ashish <an...@tis.co.in>.
Hi,
The code for my bean is:
package com.scheduler;
import java.sql.*;
public class DBUpdate
{
private Connection conn;
private Statement stmt;
private ResultSet rs;
public DBUpdate()
{
conn = null;
stmt = null;
rs = null;
}
public void process(String query)
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connection conn =
DriverManager.getConnection("jdbc:oracle:thin:@192.168.10.36:1521:myData",
"mangesh", "mangesh");
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
System.out.println("Query: " + query + " executed");
}
catch(Exception exception1)
{
System.out.println("Error occured in DBUpdate.process()
SQLException :" + exception1);
}
}
public ResultSet getResultSet() throws SQLException
{
return rs;
}
public void destroy()
{
conn = null;
stmt = null;
rs = null;
}
}
And for my servlet is:
package com.scheduler;
import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class addDataServlet extends HttpServlet
{
private String pageFormat;
public void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
ResultSet testRS;
DBUpdate dbupdate = new DBUpdate();
pageFormat = request.getParameter("newPageFormat");
PrintWriter out = response.getWriter();
out.print(pageFormat);
String query = "Select * from lookup_page_format";
dbupdate.process(query);
try
{
testRS = dbupdate.getResultSet();
while(testRS.next())
{
out.print(testRS.getString(2));
out.print("hi");
}
}
catch (SQLException e)
{
out.println("SQLException :" + e);
}
try
{
//set the attribute and forward to pageFormat.jsp
request.setAttribute("servletName", "addDataServlet");
getServletConfig().getServletContext().getRequestDispatcher("/Tracking_syste
m/pageFormat.jsp").forward(request, response);
}
catch (Exception ex)
{
ex.printStackTrace ();
}
}
}
On compiling the addDataServlet.java, an error is coming - Unresolved
symbol:Class DBUpadate is not resolved
Where I am doing wrong?
Thanks in advance.
Regards,
Anunay.
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org