Anthony Diodato wrote:
> Hello All,
>
> Here is my scenario.
> I have Tomcat 3.2.4 running on IIS 5.0
>
> I have an html page as my entrance to the website.
> (http://www.domainname.com/index.html)
> On this site is a form to login, with you user name and password.
>
> When the user clicks submit, they get sent to a servlet that I wrote.
> This servlet verify's their username and password, and should re-direct them
> to a certain pace depending on who they are.
> If I use response.sendRedirect(site);
> it sends them to the right webpage, but it doesn't seem like the session is
> created.
>
> I test the page to see if there is a valid session, and I get a Null
> Pointer.
> Here is what Im doing there.
>
> if (userSesson.getValue("userName") == null) {
> // redirect them to login because the session isn't valid
> } else {
> // display the page..
> }
>
> If I use the rd.forward() method
> they never get re-directed anywhere
>
>
> Here is my doPost() code...
> [...]
> if
> (Customers.isAcclaim(request.getParameter("userName"))) {
> if
> (Customers.isWholesale(request.getParameter("userName"))) {
> site = "/cocoon/choose.xml";
> } else {
> site = "/cocoon/acclaim/index.xml";
> }
> } else {
> if
> (Customers.isWholesale(request.getParameter("userName"))) {
> site = "/cocoon/yCust/index.xml";
> } else {
> site = "/cocoon/choose.xml";
> }
> [...]
> rd = servletContext.getRequestDispatcher(site);
>
> rd.forward(request, response);
> }
Most likely this is a problem with the type of paths you use.
I assume that "/coocon" is the context path prefix for your
main application. If so, you should *not* include this prefix
in the call to getRequestDispatcher(), since this method takes
a "context-relative path", i.e. a path starting with a slash
representing the path to the servlet/JSP page *within* the
application (the context path prefix is decided at deployment
so it may differ per installation; hence, a portable path must
not include the prefix). In your case, this means a path like
"/choose.xml" instead of "/cocoon/choose.xml".
If, on the other hand, you use the response.sendRedirect() method,
you *must* use a path that includes the context path prefix, e.g.
"/cocoon/choose.xml". This is because the sendRedirect() method
tells the browser (as opposed to the container for a forward) to
make a new request for the resource, and the browser doesn't know
about contexts and can't figure out how to interpret a context-relative
path.
In order to create a session in the login servlet and then access
the same session in the rest of the application, you must also
make sure that the login servlet is a part of the same context
as the rest of the application. First of all, this means that
your login form at "/index.html" must refer to the servlet with a
path that starts with the same context path prefix as the other
resources, likely something like this in your case:
<form action="/cocoon/loginServlet">
assuming you have defined a URL mapping for "/loginServlet/*" to
your login servlet in the deployment descriptor (web.xml):
<web-app>
...
<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.mycomp.MyServlet</servlet>
</servlet>
<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet/*</servlet>
</servlet-mapping>
...
</web-app>
Secondly, the class file for the servlet must be stored in
either in the WEB-INF/classes directory or in a JAR file in the
WEB-INF/lib directory for the web application deployed with
the "/cocoon" context path prefix.
> What I want to do is this.
> I want my servlet to re-direct them to the proper page while passing my
> session to it, so I can verify it in my xml pages.
I hope this helps. If not, I suggest that you read the Servlet 2.3
specification or a book about servlets that cover at least the 2.2
version of the servlet specification.
Hans
--
Hans Bergsten [EMAIL PROTECTED]
Gefion Software http://www.gefionsoftware.com
JavaServer Pages http://TheJSPBook.com
===========================================================================
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://archives.java.sun.com/jsp-interest.html
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.jsp
http://www.jguru.com/faq/index.jsp
http://www.jspinsider.com