Re: Still no luck with Web App

Discussion in 'Java' started by Talking with Tonz - Emery Z. Balint Jr., Jul 25, 2003.

  1. Hi Andy,

    Thank you for testing the app. I'm using Tomcat 4.1.24.

    Did you try doing the following: after immediately logging out, hitting the
    brower's back button and then hitting refresh. That should take you back to
    the login screen, but then the app fails. It won't login or logout properly
    again. It works great only the first time around.

    I also noticed that the first time I logged out, the System.out.println's in
    the logout section did what they were supposed to. But the second time I
    tried logging out, they would no longer show up. I don't know what's going
    on. :(

    Emery.
    /\^/\^/\
    Sun Certified Java Programmer
    www.websamba.com/javarobotics/
    E-stronomy - Astronomical Resrouces
    www.websamba.com/e-stronomy/


    "Andy Flowers" <> wrote in message
    news:16gUa.1771$...
    > Runs perfect for me.
    >
    > What version of Tomcat are you using ? I have 4.0.3 running under JBuilder
    > 7.
    >
    > "Talking with Tonz - Emery Z. Balint Jr."
    > <-> wrote in message
    > news:xyeUa.31419$...
    > > Hello Folks,
    > >
    > > Argg. I am still not having any luck with this Web app. I've done stuff

    > like
    > > this in ASP easily. But somehow it just isn't working here. I've taken

    the
    > > advice of some helpful people but it hasn't worked. Something tells me

    > that
    > > this should be working, but yet isn't.
    > >
    > > 1. Login with a fake username (any username works at this point).
    > > 2. You can switch between the whatever and user_mod JSP pages. Great.
    > > 3. You can logout THE FIRST TIME.
    > > 4. At this point you can hit the link to login again. Loggin in again

    > works.
    > > The back button/refresh can get goofy though.
    > > 5. You can't logout anymore!! The logout page seems not to do anything
    > > anymore! At least no output is show in the Tomcat console.
    > > 6. Everything goes wrong again. Somehow the servlet isn't run again, or
    > > appears that it's not running. Logging in no longer function properly.
    > >
    > > All the code is posted below. I would again appreciate some help, I

    really
    > > don't know why this is behaving the way it is. There are lots of
    > > System.out.println's in the code so you can see the output in Tomcat.

    > Many,
    > > many thanks in advance for any help!
    > >
    > > Emery.
    > > /\^/\^/\
    > > Sun Certified Java Programmer
    > > www.websamba.com/javarobotics/
    > > E-stronomy - Astronomical Resrouces
    > > www.websamba.com/e-stronomy/
    > >
    > > ===================================
    > > Controller.java
    > > (in the "src\com\nsae\testdb1\controller" folder)
    > > ===================================
    > > package com.nsae.testdb1.controller;
    > >
    > > import javax.servlet.*;
    > > import javax.servlet.http.*;
    > >
    > > import java.io.*;
    > >
    > > import com.nsae.testdb1.*;
    > >
    > > public class Controller extends HttpServlet {
    > >
    > > protected static final String LOGIN_PAGE = "/login.html";
    > > protected static final String MOD_USER_PAGE = "/mod_user.jsp";
    > > protected static final String ADD_USER_PAGE = "/add_user.jsp";
    > > protected static final String DEL_USER_PAGE = "/del_user.jsp";
    > > protected static final String UPD_USER_PAGE = "/upd_user.jsp";
    > > protected static final String WHATEVER_PAGE = "/whatever.jsp";
    > > protected static final String LOGOUT_PAGE = "/logout.html";
    > >
    > > public void doGet(HttpServletRequest req, HttpServletResponse res)

    throws
    > > ServletException, IOException {
    > > doPost(req, res);
    > > }
    > >
    > > public void doPost(HttpServletRequest req, HttpServletResponse res)

    > throws
    > > ServletException, IOException {
    > > String formAction = req.getParameter("form_action");
    > > String forwardTo = LOGIN_PAGE;
    > >
    > > if ((formAction==null) || (formAction.equals(""))) {
    > > HttpSession session = req.getSession(true);
    > > System.out.println("formAction==null");
    > >
    > > if (!session.isNew()) {
    > > System.out.println("formAction==null | session is not new");
    > > session.invalidate();
    > > session = req.getSession(true);
    > > System.out.println(session.isNew());
    > > }
    > >
    > > } else if (formAction.equals("login")) {
    > > HttpSession session = req.getSession(false);
    > > System.out.println("formAction==login");
    > >
    > > if (session!=null) {
    > > System.out.println("formAction==login | session!=null");
    > > if (!session.isNew()) {
    > > UserCheck uC = new UserCheck(req.getParameter("username"));
    > > session.setAttribute("user", uC.getUserName());
    > > session.setAttribute("servletPath", getFullServletPath(req));
    > > session.setMaxInactiveInterval(300);
    > > forwardTo = MOD_USER_PAGE;
    > > System.out.println("formAction==login | session is not new");
    > > } else {
    > > session.invalidate();
    > > session = req.getSession(true);
    > > System.out.println("formAction==login | session was new");
    > > }
    > > }
    > >
    > > } else if (formAction.equals("user_mod")) {
    > > HttpSession session = req.getSession(false);
    > > System.out.println("formAction==user_mod");
    > >
    > > if (session.getAttribute("user")!=null) {
    > > forwardTo = MOD_USER_PAGE;
    > > System.out.println("formAction==user_mod | user!=null");
    > > } else {
    > > System.out.println("formAction==user_mod | user==null");
    > > }
    > >
    > > } else if (formAction.equals("whatever")) {
    > > HttpSession session = req.getSession(false);
    > > System.out.println("formAction==whatever");
    > >
    > > if (session.getAttribute("user")!=null) {
    > > forwardTo = WHATEVER_PAGE;
    > > System.out.println("formAction==whatever | user!=null");
    > > } else {
    > > System.out.println("formAction==whatever | user==null");
    > > }
    > >
    > > } else if (formAction.equals("logout")) {
    > > HttpSession session = req.getSession(false);
    > > System.out.println("formAction==logout: " + session.isNew());
    > >
    > > if (session!=null) {
    > > session.removeAttribute("user");
    > > forwardTo = LOGOUT_PAGE;
    > > System.out.println("formAction==logout | session!=null");
    > > } else {
    > > forwardTo = LOGOUT_PAGE;
    > > System.out.println("formAction==logout | session==null");
    > > session = req.getSession(true);
    > > }
    > > }
    > >
    > > System.out.println(res.encodeURL(forwardTo));
    > > RequestDispatcher rD =

    > req.getRequestDispatcher(res.encodeURL(forwardTo));
    > > rD.forward(req, res);
    > >
    > > }
    > >
    > > protected String getFullServletPath(HttpServletRequest req) {
    > > String servlet = req.getServletPath();
    > > String ctxPath = req.getContextPath();
    > > return (ctxPath + servlet);
    > > }
    > >
    > > }
    > >
    > > ===================================
    > > UserCheck.java (will be implemented later)
    > > (in the "src\com\nsae\testdb1" folder)
    > > ===================================
    > > package com.nsae.testdb1;
    > >
    > > public class UserCheck {
    > >
    > > private String userName = "";
    > >
    > > public UserCheck() {
    > > }
    > >
    > > public UserCheck(String uN) {
    > > userName = uN;
    > > }
    > >
    > > public String getUserName() {
    > > return userName;
    > > }
    > >
    > > public void setUserName(String uN) {
    > > userName = uN;
    > > }
    > >
    > > }
    > >
    > > ===================================
    > > index.jsp
    > > (in the "web" folder)
    > > (this simply forwards the browser to
    > > the servlet so an initial session can be established)
    > > ===================================
    > > <jsp:forward page="testdb1"/>
    > >
    > > ===================================
    > > login.html
    > > (in the "web" folder)
    > > ===================================
    > > <html>
    > >
    > > <head>
    > > <title>Login</title>
    > > </head>
    > >
    > > <body bgcolor="#FFFFFF">
    > > Please login:
    > >
    > > <p>
    > > <form action="testdb1" method="post">
    > > <input type="hidden" name="form_action" value="login">
    > > <input type="text" name="username" size="20"> - Username<br>
    > > <input type="text" name="password" size="20"> - Password<br>
    > > <input type="submit" value="Submit">
    > > <input type="reset" value="Reset">
    > > </form>
    > >
    > > </body>
    > >
    > > </html>
    > >
    > > ===================================
    > > logout.html
    > > (in the "web" folder)
    > > ===================================
    > > <html>
    > >
    > > <head>
    > > <title>Logout</title>
    > > </head>
    > >
    > > <body bgcolor="#FFFFFF">
    > > Thanks for logging out!
    > >
    > > <p>
    > > <a href="testdb1" target="_parent">Login Again?</a>
    > >
    > > </body>
    > >
    > > </html>
    > >
    > > ===================================
    > > mod_user.jsp
    > > (in the "web" folder)
    > > ===================================
    > > <jsp:useBean id="servletPath" class="java.lang.String" scope="session"/>
    > > <jsp:useBean id="user" class="java.lang.String" scope="session"/>
    > >
    > > <html>
    > >
    > > <head>
    > > <title>Modify User</title>
    > > </head>
    > >
    > > <body bgcolor="#FFFFFF">
    > > <a href="testdb1?form_action=whatever">Whatever</a> |
    > > <a href="testdb1?form_action=logout">Logout</a>
    > >
    > > <p>
    > > Hi <%=user%>!
    > >
    > > <p>
    > > Add User:
    > >
    > > <p>
    > > <form action="testdb1" method="post">
    > > <input type="hidden" name="form_action" value="add_user">
    > > <input type="text" name="username" size="20"> - Username<br>
    > > <input type="text" name="password" size="20"> - Password<br>
    > > <input type="submit" value="Add">
    > > <input type="reset" value="Reset">
    > > </form>
    > >
    > > Modify User Information:
    > >
    > > <p>
    > > <form action="testdb1" method="post">
    > > <input type="hidden" name="form_action" value="mod_user">
    > > <input type="hidden" name="user_id" value="user_id">
    > > <input type="text" name="username" size="20"> - Username<br>
    > > <input type="text" name="password" size="20"> - Password<br>
    > > <input type="submit" value="Update">
    > > <input type="reset" value="Reset">
    > > </form>
    > >
    > > <p>
    > > Delete User:
    > >
    > > <p>
    > > <form action="testdb1" method="post">
    > > <input type="hidden" name="form_action" value="del_user">
    > > <input type="hidden" name="user_id" value="user_id">
    > > <input type="submit" value="Delete">
    > > <input type="reset" value="Reset">
    > > </form>
    > >
    > > </body>
    > >
    > > </html>
    > >
    > > ===================================
    > > whatever.jsp
    > > (in the "web" folder)
    > > ===================================
    > > <jsp:useBean id="servletPath" class="java.lang.String" scope="session"/>
    > > <jsp:useBean id="user" class="java.lang.String" scope="session"/>
    > >
    > > <html>
    > >
    > > <head>
    > > <title>Just a Session Test</title>
    > > </head>
    > >
    > > <body bgcolor="#FFFFFF">
    > > <a href="testdb1?form_action=user_mod">Modify Users?</a> |
    > > <a href="testdb1?form_action=logout">Logout</a>
    > >
    > > <p>
    > > Hi <%=user%>!
    > >
    > > </body>
    > >
    > > </html>
    > >
    > > ===================================
    > > web.xml
    > > (in the "web/WEB-INF" folder)
    > > ===================================
    > > <?xml version="1.0" encoding="ISO-8859-1" ?>
    > >
    > > <!DOCTYPE web-app
    > > PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    > > "http://java.sun.com/dtd/web-app_2_3.dtd" >
    > >
    > > <web-app>
    > > <icon>
    > > </icon>
    > > <display-name>Test Database Application</display-name>
    > > <description>
    > > This is a test of a database editing application.
    > > </description>
    > >
    > > <servlet>
    > > <servlet-name>Controller</servlet-name>
    > > <description>
    > > Controller Servlet
    > > </description>
    > > <servlet-class>
    > > com.nsae.testdb1.controller.Controller
    > > </servlet-class>
    > > </servlet>
    > >
    > > <servlet-mapping>
    > > <servlet-name>Controller</servlet-name>
    > > <url-pattern>/testdb1</url-pattern>
    > > </servlet-mapping>
    > > </web-app>
    > >
    > >

    >
    >
     
    Talking with Tonz - Emery Z. Balint Jr., Jul 25, 2003
    #1
    1. Advertising

  2. Talking with Tonz - Emery Z. Balint Jr.

    Andy Flowers Guest

    Still works fine even with the refresh.

    There's a couple of suggestions about the code though. First off use JSP
    pages for everything, even the HTML. This makes it easier in the long run
    should something need to become dynamic. Also there's no need to use
    isNew(). Rely on the fact that if it's a new session there are no values in
    it's attributes, and that these will disappear with the invalidate().

    Perhaps isNew() is failing in your scenario, there are a few bugs/caveats
    around the use of isNew() in Tomcat 4.1.x ?

    "Talking with Tonz - Emery Z. Balint Jr."
    <-> wrote in message
    news:XNhUa.36060$...
    > Hi Andy,
    >
    > Thank you for testing the app. I'm using Tomcat 4.1.24.
    >
    > Did you try doing the following: after immediately logging out, hitting

    the
    > brower's back button and then hitting refresh. That should take you back

    to
    > the login screen, but then the app fails. It won't login or logout

    properly
    > again. It works great only the first time around.
    >
    > I also noticed that the first time I logged out, the System.out.println's

    in
    > the logout section did what they were supposed to. But the second time I
    > tried logging out, they would no longer show up. I don't know what's going
    > on. :(
    >
    > Emery.
    > /\^/\^/\
    > Sun Certified Java Programmer
    > www.websamba.com/javarobotics/
    > E-stronomy - Astronomical Resrouces
    > www.websamba.com/e-stronomy/
    >
    >
    > "Andy Flowers" <> wrote in message
    > news:16gUa.1771$...
    > > Runs perfect for me.
    > >
    > > What version of Tomcat are you using ? I have 4.0.3 running under

    JBuilder
    > > 7.
    > >
    > > "Talking with Tonz - Emery Z. Balint Jr."
    > > <-> wrote in message
    > > news:xyeUa.31419$...
    > > > Hello Folks,
    > > >
    > > > Argg. I am still not having any luck with this Web app. I've done

    stuff
    > > like
    > > > this in ASP easily. But somehow it just isn't working here. I've taken

    > the
    > > > advice of some helpful people but it hasn't worked. Something tells me

    > > that
    > > > this should be working, but yet isn't.
    > > >
    > > > 1. Login with a fake username (any username works at this point).
    > > > 2. You can switch between the whatever and user_mod JSP pages. Great.
    > > > 3. You can logout THE FIRST TIME.
    > > > 4. At this point you can hit the link to login again. Loggin in again

    > > works.
    > > > The back button/refresh can get goofy though.
    > > > 5. You can't logout anymore!! The logout page seems not to do anything
    > > > anymore! At least no output is show in the Tomcat console.
    > > > 6. Everything goes wrong again. Somehow the servlet isn't run again,

    or
    > > > appears that it's not running. Logging in no longer function properly.
    > > >
    > > > All the code is posted below. I would again appreciate some help, I

    > really
    > > > don't know why this is behaving the way it is. There are lots of
    > > > System.out.println's in the code so you can see the output in Tomcat.

    > > Many,
    > > > many thanks in advance for any help!
    > > >
    > > > Emery.
    > > > /\^/\^/\
    > > > Sun Certified Java Programmer
    > > > www.websamba.com/javarobotics/
    > > > E-stronomy - Astronomical Resrouces
    > > > www.websamba.com/e-stronomy/
    > > >
    > > > ===================================
    > > > Controller.java
    > > > (in the "src\com\nsae\testdb1\controller" folder)
    > > > ===================================
    > > > package com.nsae.testdb1.controller;
    > > >
    > > > import javax.servlet.*;
    > > > import javax.servlet.http.*;
    > > >
    > > > import java.io.*;
    > > >
    > > > import com.nsae.testdb1.*;
    > > >
    > > > public class Controller extends HttpServlet {
    > > >
    > > > protected static final String LOGIN_PAGE = "/login.html";
    > > > protected static final String MOD_USER_PAGE = "/mod_user.jsp";
    > > > protected static final String ADD_USER_PAGE = "/add_user.jsp";
    > > > protected static final String DEL_USER_PAGE = "/del_user.jsp";
    > > > protected static final String UPD_USER_PAGE = "/upd_user.jsp";
    > > > protected static final String WHATEVER_PAGE = "/whatever.jsp";
    > > > protected static final String LOGOUT_PAGE = "/logout.html";
    > > >
    > > > public void doGet(HttpServletRequest req, HttpServletResponse res)

    > throws
    > > > ServletException, IOException {
    > > > doPost(req, res);
    > > > }
    > > >
    > > > public void doPost(HttpServletRequest req, HttpServletResponse res)

    > > throws
    > > > ServletException, IOException {
    > > > String formAction = req.getParameter("form_action");
    > > > String forwardTo = LOGIN_PAGE;
    > > >
    > > > if ((formAction==null) || (formAction.equals(""))) {
    > > > HttpSession session = req.getSession(true);
    > > > System.out.println("formAction==null");
    > > >
    > > > if (!session.isNew()) {
    > > > System.out.println("formAction==null | session is not new");
    > > > session.invalidate();
    > > > session = req.getSession(true);
    > > > System.out.println(session.isNew());
    > > > }
    > > >
    > > > } else if (formAction.equals("login")) {
    > > > HttpSession session = req.getSession(false);
    > > > System.out.println("formAction==login");
    > > >
    > > > if (session!=null) {
    > > > System.out.println("formAction==login | session!=null");
    > > > if (!session.isNew()) {
    > > > UserCheck uC = new UserCheck(req.getParameter("username"));
    > > > session.setAttribute("user", uC.getUserName());
    > > > session.setAttribute("servletPath", getFullServletPath(req));
    > > > session.setMaxInactiveInterval(300);
    > > > forwardTo = MOD_USER_PAGE;
    > > > System.out.println("formAction==login | session is not new");
    > > > } else {
    > > > session.invalidate();
    > > > session = req.getSession(true);
    > > > System.out.println("formAction==login | session was new");
    > > > }
    > > > }
    > > >
    > > > } else if (formAction.equals("user_mod")) {
    > > > HttpSession session = req.getSession(false);
    > > > System.out.println("formAction==user_mod");
    > > >
    > > > if (session.getAttribute("user")!=null) {
    > > > forwardTo = MOD_USER_PAGE;
    > > > System.out.println("formAction==user_mod | user!=null");
    > > > } else {
    > > > System.out.println("formAction==user_mod | user==null");
    > > > }
    > > >
    > > > } else if (formAction.equals("whatever")) {
    > > > HttpSession session = req.getSession(false);
    > > > System.out.println("formAction==whatever");
    > > >
    > > > if (session.getAttribute("user")!=null) {
    > > > forwardTo = WHATEVER_PAGE;
    > > > System.out.println("formAction==whatever | user!=null");
    > > > } else {
    > > > System.out.println("formAction==whatever | user==null");
    > > > }
    > > >
    > > > } else if (formAction.equals("logout")) {
    > > > HttpSession session = req.getSession(false);
    > > > System.out.println("formAction==logout: " + session.isNew());
    > > >
    > > > if (session!=null) {
    > > > session.removeAttribute("user");
    > > > forwardTo = LOGOUT_PAGE;
    > > > System.out.println("formAction==logout | session!=null");
    > > > } else {
    > > > forwardTo = LOGOUT_PAGE;
    > > > System.out.println("formAction==logout | session==null");
    > > > session = req.getSession(true);
    > > > }
    > > > }
    > > >
    > > > System.out.println(res.encodeURL(forwardTo));
    > > > RequestDispatcher rD =

    > > req.getRequestDispatcher(res.encodeURL(forwardTo));
    > > > rD.forward(req, res);
    > > >
    > > > }
    > > >
    > > > protected String getFullServletPath(HttpServletRequest req) {
    > > > String servlet = req.getServletPath();
    > > > String ctxPath = req.getContextPath();
    > > > return (ctxPath + servlet);
    > > > }
    > > >
    > > > }
    > > >
    > > > ===================================
    > > > UserCheck.java (will be implemented later)
    > > > (in the "src\com\nsae\testdb1" folder)
    > > > ===================================
    > > > package com.nsae.testdb1;
    > > >
    > > > public class UserCheck {
    > > >
    > > > private String userName = "";
    > > >
    > > > public UserCheck() {
    > > > }
    > > >
    > > > public UserCheck(String uN) {
    > > > userName = uN;
    > > > }
    > > >
    > > > public String getUserName() {
    > > > return userName;
    > > > }
    > > >
    > > > public void setUserName(String uN) {
    > > > userName = uN;
    > > > }
    > > >
    > > > }
    > > >
    > > > ===================================
    > > > index.jsp
    > > > (in the "web" folder)
    > > > (this simply forwards the browser to
    > > > the servlet so an initial session can be established)
    > > > ===================================
    > > > <jsp:forward page="testdb1"/>
    > > >
    > > > ===================================
    > > > login.html
    > > > (in the "web" folder)
    > > > ===================================
    > > > <html>
    > > >
    > > > <head>
    > > > <title>Login</title>
    > > > </head>
    > > >
    > > > <body bgcolor="#FFFFFF">
    > > > Please login:
    > > >
    > > > <p>
    > > > <form action="testdb1" method="post">
    > > > <input type="hidden" name="form_action" value="login">
    > > > <input type="text" name="username" size="20"> - Username<br>
    > > > <input type="text" name="password" size="20"> - Password<br>
    > > > <input type="submit" value="Submit">
    > > > <input type="reset" value="Reset">
    > > > </form>
    > > >
    > > > </body>
    > > >
    > > > </html>
    > > >
    > > > ===================================
    > > > logout.html
    > > > (in the "web" folder)
    > > > ===================================
    > > > <html>
    > > >
    > > > <head>
    > > > <title>Logout</title>
    > > > </head>
    > > >
    > > > <body bgcolor="#FFFFFF">
    > > > Thanks for logging out!
    > > >
    > > > <p>
    > > > <a href="testdb1" target="_parent">Login Again?</a>
    > > >
    > > > </body>
    > > >
    > > > </html>
    > > >
    > > > ===================================
    > > > mod_user.jsp
    > > > (in the "web" folder)
    > > > ===================================
    > > > <jsp:useBean id="servletPath" class="java.lang.String"

    scope="session"/>
    > > > <jsp:useBean id="user" class="java.lang.String" scope="session"/>
    > > >
    > > > <html>
    > > >
    > > > <head>
    > > > <title>Modify User</title>
    > > > </head>
    > > >
    > > > <body bgcolor="#FFFFFF">
    > > > <a href="testdb1?form_action=whatever">Whatever</a> |
    > > > <a href="testdb1?form_action=logout">Logout</a>
    > > >
    > > > <p>
    > > > Hi <%=user%>!
    > > >
    > > > <p>
    > > > Add User:
    > > >
    > > > <p>
    > > > <form action="testdb1" method="post">
    > > > <input type="hidden" name="form_action" value="add_user">
    > > > <input type="text" name="username" size="20"> - Username<br>
    > > > <input type="text" name="password" size="20"> - Password<br>
    > > > <input type="submit" value="Add">
    > > > <input type="reset" value="Reset">
    > > > </form>
    > > >
    > > > Modify User Information:
    > > >
    > > > <p>
    > > > <form action="testdb1" method="post">
    > > > <input type="hidden" name="form_action" value="mod_user">
    > > > <input type="hidden" name="user_id" value="user_id">
    > > > <input type="text" name="username" size="20"> - Username<br>
    > > > <input type="text" name="password" size="20"> - Password<br>
    > > > <input type="submit" value="Update">
    > > > <input type="reset" value="Reset">
    > > > </form>
    > > >
    > > > <p>
    > > > Delete User:
    > > >
    > > > <p>
    > > > <form action="testdb1" method="post">
    > > > <input type="hidden" name="form_action" value="del_user">
    > > > <input type="hidden" name="user_id" value="user_id">
    > > > <input type="submit" value="Delete">
    > > > <input type="reset" value="Reset">
    > > > </form>
    > > >
    > > > </body>
    > > >
    > > > </html>
    > > >
    > > > ===================================
    > > > whatever.jsp
    > > > (in the "web" folder)
    > > > ===================================
    > > > <jsp:useBean id="servletPath" class="java.lang.String"

    scope="session"/>
    > > > <jsp:useBean id="user" class="java.lang.String" scope="session"/>
    > > >
    > > > <html>
    > > >
    > > > <head>
    > > > <title>Just a Session Test</title>
    > > > </head>
    > > >
    > > > <body bgcolor="#FFFFFF">
    > > > <a href="testdb1?form_action=user_mod">Modify Users?</a> |
    > > > <a href="testdb1?form_action=logout">Logout</a>
    > > >
    > > > <p>
    > > > Hi <%=user%>!
    > > >
    > > > </body>
    > > >
    > > > </html>
    > > >
    > > > ===================================
    > > > web.xml
    > > > (in the "web/WEB-INF" folder)
    > > > ===================================
    > > > <?xml version="1.0" encoding="ISO-8859-1" ?>
    > > >
    > > > <!DOCTYPE web-app
    > > > PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    > > > "http://java.sun.com/dtd/web-app_2_3.dtd" >
    > > >
    > > > <web-app>
    > > > <icon>
    > > > </icon>
    > > > <display-name>Test Database Application</display-name>
    > > > <description>
    > > > This is a test of a database editing application.
    > > > </description>
    > > >
    > > > <servlet>
    > > > <servlet-name>Controller</servlet-name>
    > > > <description>
    > > > Controller Servlet
    > > > </description>
    > > > <servlet-class>
    > > > com.nsae.testdb1.controller.Controller
    > > > </servlet-class>
    > > > </servlet>
    > > >
    > > > <servlet-mapping>
    > > > <servlet-name>Controller</servlet-name>
    > > > <url-pattern>/testdb1</url-pattern>
    > > > </servlet-mapping>
    > > > </web-app>
    > > >
    > > >

    > >
    > >

    >
    >
     
    Andy Flowers, Jul 26, 2003
    #2
    1. Advertising

  3. Hello Andy,

    Again, thank you for your assistance. I actually ended up renaming all my
    html files to jsp and then everything started working. I also posted my new
    code (Post subject: Fixed Web App Issue But Do Servlets Ignore HTML Pages?)
    where my app relies on whether or not the "user" is still in the session
    instead of isNew(). Less code and works better too. I can finally rest easy.
    8^D

    Emery.
    /\^/\^/\
    Sun Certified Java Programmer
    www.websamba.com/javarobotics/
    E-stronomy - Astronomical Resrouces
    www.websamba.com/e-stronomy/


    "Andy Flowers" <> wrote in message
    news:Y6qUa.2116$...
    > Still works fine even with the refresh.
    >
    > There's a couple of suggestions about the code though. First off use JSP
    > pages for everything, even the HTML. This makes it easier in the long run
    > should something need to become dynamic. Also there's no need to use
    > isNew(). Rely on the fact that if it's a new session there are no values

    in
    > it's attributes, and that these will disappear with the invalidate().
    >
    > Perhaps isNew() is failing in your scenario, there are a few bugs/caveats
    > around the use of isNew() in Tomcat 4.1.x ?
    >
    > "Talking with Tonz - Emery Z. Balint Jr."
    > <-> wrote in message
    > news:XNhUa.36060$...
    > > Hi Andy,
    > >
    > > Thank you for testing the app. I'm using Tomcat 4.1.24.
    > >
    > > Did you try doing the following: after immediately logging out, hitting

    > the
    > > brower's back button and then hitting refresh. That should take you back

    > to
    > > the login screen, but then the app fails. It won't login or logout

    > properly
    > > again. It works great only the first time around.
    > >
    > > I also noticed that the first time I logged out, the

    System.out.println's
    > in
    > > the logout section did what they were supposed to. But the second time I
    > > tried logging out, they would no longer show up. I don't know what's

    going
    > > on. :(
    > >
    > > Emery.
    > > /\^/\^/\
    > > Sun Certified Java Programmer
    > > www.websamba.com/javarobotics/
    > > E-stronomy - Astronomical Resrouces
    > > www.websamba.com/e-stronomy/
    > >
    > >
    > > "Andy Flowers" <> wrote in message
    > > news:16gUa.1771$...
    > > > Runs perfect for me.
    > > >
    > > > What version of Tomcat are you using ? I have 4.0.3 running under

    > JBuilder
    > > > 7.
    > > >
    > > > "Talking with Tonz - Emery Z. Balint Jr."
    > > > <-> wrote in message
    > > > news:xyeUa.31419$...
    > > > > Hello Folks,
    > > > >
    > > > > Argg. I am still not having any luck with this Web app. I've done

    > stuff
    > > > like
    > > > > this in ASP easily. But somehow it just isn't working here. I've

    taken
    > > the
    > > > > advice of some helpful people but it hasn't worked. Something tells

    me
    > > > that
    > > > > this should be working, but yet isn't.
    > > > >
    > > > > 1. Login with a fake username (any username works at this point).
    > > > > 2. You can switch between the whatever and user_mod JSP pages.

    Great.
    > > > > 3. You can logout THE FIRST TIME.
    > > > > 4. At this point you can hit the link to login again. Loggin in

    again
    > > > works.
    > > > > The back button/refresh can get goofy though.
    > > > > 5. You can't logout anymore!! The logout page seems not to do

    anything
    > > > > anymore! At least no output is show in the Tomcat console.
    > > > > 6. Everything goes wrong again. Somehow the servlet isn't run again,

    > or
    > > > > appears that it's not running. Logging in no longer function

    properly.
    > > > >
    > > > > All the code is posted below. I would again appreciate some help, I

    > > really
    > > > > don't know why this is behaving the way it is. There are lots of
    > > > > System.out.println's in the code so you can see the output in

    Tomcat.
    > > > Many,
    > > > > many thanks in advance for any help!
    > > > >
    > > > > Emery.
    > > > > /\^/\^/\
    > > > > Sun Certified Java Programmer
    > > > > www.websamba.com/javarobotics/
    > > > > E-stronomy - Astronomical Resrouces
    > > > > www.websamba.com/e-stronomy/
    > > > >
    > > > > ===================================
    > > > > Controller.java
    > > > > (in the "src\com\nsae\testdb1\controller" folder)
    > > > > ===================================
    > > > > package com.nsae.testdb1.controller;
    > > > >
    > > > > import javax.servlet.*;
    > > > > import javax.servlet.http.*;
    > > > >
    > > > > import java.io.*;
    > > > >
    > > > > import com.nsae.testdb1.*;
    > > > >
    > > > > public class Controller extends HttpServlet {
    > > > >
    > > > > protected static final String LOGIN_PAGE = "/login.html";
    > > > > protected static final String MOD_USER_PAGE = "/mod_user.jsp";
    > > > > protected static final String ADD_USER_PAGE = "/add_user.jsp";
    > > > > protected static final String DEL_USER_PAGE = "/del_user.jsp";
    > > > > protected static final String UPD_USER_PAGE = "/upd_user.jsp";
    > > > > protected static final String WHATEVER_PAGE = "/whatever.jsp";
    > > > > protected static final String LOGOUT_PAGE = "/logout.html";
    > > > >
    > > > > public void doGet(HttpServletRequest req, HttpServletResponse res)

    > > throws
    > > > > ServletException, IOException {
    > > > > doPost(req, res);
    > > > > }
    > > > >
    > > > > public void doPost(HttpServletRequest req, HttpServletResponse res)
    > > > throws
    > > > > ServletException, IOException {
    > > > > String formAction = req.getParameter("form_action");
    > > > > String forwardTo = LOGIN_PAGE;
    > > > >
    > > > > if ((formAction==null) || (formAction.equals(""))) {
    > > > > HttpSession session = req.getSession(true);
    > > > > System.out.println("formAction==null");
    > > > >
    > > > > if (!session.isNew()) {
    > > > > System.out.println("formAction==null | session is not new");
    > > > > session.invalidate();
    > > > > session = req.getSession(true);
    > > > > System.out.println(session.isNew());
    > > > > }
    > > > >
    > > > > } else if (formAction.equals("login")) {
    > > > > HttpSession session = req.getSession(false);
    > > > > System.out.println("formAction==login");
    > > > >
    > > > > if (session!=null) {
    > > > > System.out.println("formAction==login | session!=null");
    > > > > if (!session.isNew()) {
    > > > > UserCheck uC = new UserCheck(req.getParameter("username"));
    > > > > session.setAttribute("user", uC.getUserName());
    > > > > session.setAttribute("servletPath", getFullServletPath(req));
    > > > > session.setMaxInactiveInterval(300);
    > > > > forwardTo = MOD_USER_PAGE;
    > > > > System.out.println("formAction==login | session is not new");
    > > > > } else {
    > > > > session.invalidate();
    > > > > session = req.getSession(true);
    > > > > System.out.println("formAction==login | session was new");
    > > > > }
    > > > > }
    > > > >
    > > > > } else if (formAction.equals("user_mod")) {
    > > > > HttpSession session = req.getSession(false);
    > > > > System.out.println("formAction==user_mod");
    > > > >
    > > > > if (session.getAttribute("user")!=null) {
    > > > > forwardTo = MOD_USER_PAGE;
    > > > > System.out.println("formAction==user_mod | user!=null");
    > > > > } else {
    > > > > System.out.println("formAction==user_mod | user==null");
    > > > > }
    > > > >
    > > > > } else if (formAction.equals("whatever")) {
    > > > > HttpSession session = req.getSession(false);
    > > > > System.out.println("formAction==whatever");
    > > > >
    > > > > if (session.getAttribute("user")!=null) {
    > > > > forwardTo = WHATEVER_PAGE;
    > > > > System.out.println("formAction==whatever | user!=null");
    > > > > } else {
    > > > > System.out.println("formAction==whatever | user==null");
    > > > > }
    > > > >
    > > > > } else if (formAction.equals("logout")) {
    > > > > HttpSession session = req.getSession(false);
    > > > > System.out.println("formAction==logout: " + session.isNew());
    > > > >
    > > > > if (session!=null) {
    > > > > session.removeAttribute("user");
    > > > > forwardTo = LOGOUT_PAGE;
    > > > > System.out.println("formAction==logout | session!=null");
    > > > > } else {
    > > > > forwardTo = LOGOUT_PAGE;
    > > > > System.out.println("formAction==logout | session==null");
    > > > > session = req.getSession(true);
    > > > > }
    > > > > }
    > > > >
    > > > > System.out.println(res.encodeURL(forwardTo));
    > > > > RequestDispatcher rD =
    > > > req.getRequestDispatcher(res.encodeURL(forwardTo));
    > > > > rD.forward(req, res);
    > > > >
    > > > > }
    > > > >
    > > > > protected String getFullServletPath(HttpServletRequest req) {
    > > > > String servlet = req.getServletPath();
    > > > > String ctxPath = req.getContextPath();
    > > > > return (ctxPath + servlet);
    > > > > }
    > > > >
    > > > > }
    > > > >
    > > > > ===================================
    > > > > UserCheck.java (will be implemented later)
    > > > > (in the "src\com\nsae\testdb1" folder)
    > > > > ===================================
    > > > > package com.nsae.testdb1;
    > > > >
    > > > > public class UserCheck {
    > > > >
    > > > > private String userName = "";
    > > > >
    > > > > public UserCheck() {
    > > > > }
    > > > >
    > > > > public UserCheck(String uN) {
    > > > > userName = uN;
    > > > > }
    > > > >
    > > > > public String getUserName() {
    > > > > return userName;
    > > > > }
    > > > >
    > > > > public void setUserName(String uN) {
    > > > > userName = uN;
    > > > > }
    > > > >
    > > > > }
    > > > >
    > > > > ===================================
    > > > > index.jsp
    > > > > (in the "web" folder)
    > > > > (this simply forwards the browser to
    > > > > the servlet so an initial session can be established)
    > > > > ===================================
    > > > > <jsp:forward page="testdb1"/>
    > > > >
    > > > > ===================================
    > > > > login.html
    > > > > (in the "web" folder)
    > > > > ===================================
    > > > > <html>
    > > > >
    > > > > <head>
    > > > > <title>Login</title>
    > > > > </head>
    > > > >
    > > > > <body bgcolor="#FFFFFF">
    > > > > Please login:
    > > > >
    > > > > <p>
    > > > > <form action="testdb1" method="post">
    > > > > <input type="hidden" name="form_action" value="login">
    > > > > <input type="text" name="username" size="20"> - Username<br>
    > > > > <input type="text" name="password" size="20"> - Password<br>
    > > > > <input type="submit" value="Submit">
    > > > > <input type="reset" value="Reset">
    > > > > </form>
    > > > >
    > > > > </body>
    > > > >
    > > > > </html>
    > > > >
    > > > > ===================================
    > > > > logout.html
    > > > > (in the "web" folder)
    > > > > ===================================
    > > > > <html>
    > > > >
    > > > > <head>
    > > > > <title>Logout</title>
    > > > > </head>
    > > > >
    > > > > <body bgcolor="#FFFFFF">
    > > > > Thanks for logging out!
    > > > >
    > > > > <p>
    > > > > <a href="testdb1" target="_parent">Login Again?</a>
    > > > >
    > > > > </body>
    > > > >
    > > > > </html>
    > > > >
    > > > > ===================================
    > > > > mod_user.jsp
    > > > > (in the "web" folder)
    > > > > ===================================
    > > > > <jsp:useBean id="servletPath" class="java.lang.String"

    > scope="session"/>
    > > > > <jsp:useBean id="user" class="java.lang.String" scope="session"/>
    > > > >
    > > > > <html>
    > > > >
    > > > > <head>
    > > > > <title>Modify User</title>
    > > > > </head>
    > > > >
    > > > > <body bgcolor="#FFFFFF">
    > > > > <a href="testdb1?form_action=whatever">Whatever</a> |
    > > > > <a href="testdb1?form_action=logout">Logout</a>
    > > > >
    > > > > <p>
    > > > > Hi <%=user%>!
    > > > >
    > > > > <p>
    > > > > Add User:
    > > > >
    > > > > <p>
    > > > > <form action="testdb1" method="post">
    > > > > <input type="hidden" name="form_action" value="add_user">
    > > > > <input type="text" name="username" size="20"> - Username<br>
    > > > > <input type="text" name="password" size="20"> - Password<br>
    > > > > <input type="submit" value="Add">
    > > > > <input type="reset" value="Reset">
    > > > > </form>
    > > > >
    > > > > Modify User Information:
    > > > >
    > > > > <p>
    > > > > <form action="testdb1" method="post">
    > > > > <input type="hidden" name="form_action" value="mod_user">
    > > > > <input type="hidden" name="user_id" value="user_id">
    > > > > <input type="text" name="username" size="20"> - Username<br>
    > > > > <input type="text" name="password" size="20"> - Password<br>
    > > > > <input type="submit" value="Update">
    > > > > <input type="reset" value="Reset">
    > > > > </form>
    > > > >
    > > > > <p>
    > > > > Delete User:
    > > > >
    > > > > <p>
    > > > > <form action="testdb1" method="post">
    > > > > <input type="hidden" name="form_action" value="del_user">
    > > > > <input type="hidden" name="user_id" value="user_id">
    > > > > <input type="submit" value="Delete">
    > > > > <input type="reset" value="Reset">
    > > > > </form>
    > > > >
    > > > > </body>
    > > > >
    > > > > </html>
    > > > >
    > > > > ===================================
    > > > > whatever.jsp
    > > > > (in the "web" folder)
    > > > > ===================================
    > > > > <jsp:useBean id="servletPath" class="java.lang.String"

    > scope="session"/>
    > > > > <jsp:useBean id="user" class="java.lang.String" scope="session"/>
    > > > >
    > > > > <html>
    > > > >
    > > > > <head>
    > > > > <title>Just a Session Test</title>
    > > > > </head>
    > > > >
    > > > > <body bgcolor="#FFFFFF">
    > > > > <a href="testdb1?form_action=user_mod">Modify Users?</a> |
    > > > > <a href="testdb1?form_action=logout">Logout</a>
    > > > >
    > > > > <p>
    > > > > Hi <%=user%>!
    > > > >
    > > > > </body>
    > > > >
    > > > > </html>
    > > > >
    > > > > ===================================
    > > > > web.xml
    > > > > (in the "web/WEB-INF" folder)
    > > > > ===================================
    > > > > <?xml version="1.0" encoding="ISO-8859-1" ?>
    > > > >
    > > > > <!DOCTYPE web-app
    > > > > PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    > > > > "http://java.sun.com/dtd/web-app_2_3.dtd" >
    > > > >
    > > > > <web-app>
    > > > > <icon>
    > > > > </icon>
    > > > > <display-name>Test Database Application</display-name>
    > > > > <description>
    > > > > This is a test of a database editing application.
    > > > > </description>
    > > > >
    > > > > <servlet>
    > > > > <servlet-name>Controller</servlet-name>
    > > > > <description>
    > > > > Controller Servlet
    > > > > </description>
    > > > > <servlet-class>
    > > > > com.nsae.testdb1.controller.Controller
    > > > > </servlet-class>
    > > > > </servlet>
    > > > >
    > > > > <servlet-mapping>
    > > > > <servlet-name>Controller</servlet-name>
    > > > > <url-pattern>/testdb1</url-pattern>
    > > > > </servlet-mapping>
    > > > > </web-app>
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Talking with Tonz - Emery Z. Balint Jr., Jul 26, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Barry

    VS.NET major bug or bad luck?

    Barry, Aug 14, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    328
    S. Justin Gengo
    Aug 15, 2003
  2. Edwin Knoppert

    Session expires, but no luck to jump

    Edwin Knoppert, Oct 10, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    295
    Edwin Knoppert
    Oct 10, 2005
  3. Edwin Knoppert
    Replies:
    0
    Views:
    319
    Edwin Knoppert
    Nov 14, 2005
  4. metaperl
    Replies:
    6
    Views:
    300
    metaperl
    Sep 23, 2006
  5. Linuxwell

    "Pythoner",Wish me luck!

    Linuxwell, Apr 3, 2009, in forum: Python
    Replies:
    11
    Views:
    543
    Jeroen Ruigrok van der Werven
    Apr 5, 2009
Loading...

Share This Page