This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Servlets and the fly likes HttpSession Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "HttpSession" Watch "HttpSession" New topic
Author

HttpSession

vinu pillai
Greenhorn

Joined: Oct 05, 2001
Posts: 7
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

Joined: Jul 19, 2001
Posts: 101
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

Joined: Sep 29, 2001
Posts: 22
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

Joined: Oct 05, 2001
Posts: 7
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

Joined: Sep 20, 2001
Posts: 49
U have to set cookies in the client machine for doing that I guess if i am wrong pls correct me.


A Quitter Never Wins<br />A Winner Never Quits
vinu pillai
Greenhorn

Joined: Oct 05, 2001
Posts: 7
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HttpSession
 
Similar Threads
session time out
what does session invalidate means?
Session problem in NetBeans
Field set in action method coming null in JSP
problem if web.xml is edited