• Post Reply Bookmark Topic Watch Topic
  • New Topic

HttpSession

 
vinu pillai
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i maintain a session through out the five different pages with the same session id given to the user..??
[This message has been edited by vinu pillai (edited October 15, 2001).]
 
Dave Van Even
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Putting stuff in
-------------------
HttpSession session = request.getSession();
session.setAttribute("some bean", new SomeBean());
Get stuff out
----------------
SomeBean mySomeBean = (SomeBean)session.getAttribute("some bean");
mySomeBean.doSome();
the setAttribte has parameters setAttribute(Object, Object) so feel free to put in what you like

Dave
 
Andre Moo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Incase you were wondering, the session is maintained for you by the container across pages - you don't need to worry about any of that!
 
vinu pillai
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the solution man..
but what if i am making a project on mail by using HttpSession
and want to maintain session till he has logged in...and when he log out or closes the browser then he has to log in by entering password in the field...
thanks
Vinu
Originally posted by Andre Moo:
Incase you were wondering, the session is maintained for you by the container across pages - you don't need to worry about any of that!

 
Syam Veerakumar
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
U have to set cookies in the client machine for doing that I guess if i am wrong pls correct me.
 
vinu pillai
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Thanks for the suggestion buddy...
Cookies will work but there are several disadvantages behind using cookies as they expire without informing the user or client...and what if the client disables cookies through his browser...what i wanted to know that HttpSession is working very fine but there is some prob..so if apart from u if anyone know plz suggest a way...i am giving away the code too...
HERE IS THE CODE:-
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestServlet extends HttpServlet {
static String URLRedirect = "http://localhost:8080/redirectPage.htm";
static String BadRedirect = "http://localhost:8080/TestServlet.htm";
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection connection = null;
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String dsn = "jdbc dbc:test";
String uname = "scott";
String pword = "tiger";
String user_id = request.getParameter("userid");
String pass_word = request.getParameter("password");
//String dbquery = "select * from adminpanel where USERID ='" +user_id+ "'and PASSWORD ='" +pass_word+"'";
String dbquery = "select * from adminpanel where USERID = ? and PASSWORD = ?";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>This is a Login Page</title></head>");
out.println("<body>");
try {
Class.forName(driver);
connection = DriverManager.getConnection(dsn, uname, pword);
PreparedStatement preparedStatement = connection.prepareStatement(dbquery);
preparedStatement.setString(1,user_id);
preparedStatement.setString(2,pass_word);
ResultSet resultset = preparedStatement.executeQuery();
if(resultset.next()==true) {
HttpSession session = request.getSession(true);
session.putValue(user_id, pass_word);
String getvalue = (String)session.getValue(pass_word);
if(getvalue.equals(pass_word)) {
trueredirectPage(request, response);
System.out.println("U are being redirected to " +URLRedirect);
System.out.println("The value of ur session is " +getvalue);
}
}
else {
System.out.println("Wrong Validity...Redirecting to .."+BadRedirect);
badredirectPage(request, response);
System.out.println("INVALID USERNAME AND PASSWORD");
}
preparedStatement.close();
connection.close();
}catch(Exception exception) {
System.out.println("The Error is " +exception.toString());
}

out.println("</body>");
out.println("</html>");
}
private void trueredirectPage(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.sendRedirect(URLRedirect);
}
private void badredirectPage(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.sendRedirect(URLRedirect);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
In this code the problem is that when I try to access directly the page which i am redirecting..(i.e String BadRedirect)..it's not redirecting the page which i wanted...(i.e String URLRedirect)..plz help..to overcome the problem..
Thanks
vinu
Originally posted by Syam Veerakumar:
U have to set cookies in the client machine for doing that I guess if i am wrong pls correct me.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!