File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes How to use Session scope in jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to use Session scope in jsp page" Watch "How to use Session scope in jsp page" New topic

How to use Session scope in jsp page

viral patel

Joined: Feb 25, 2003
Posts: 16
Hello, I have login form, where user provides username and password. Then click on submit, it will forward to validation.jsp. Where it will check in database make sure username and password exit. Now i can also retrive accountid of perticular user. I want to put that username and accountid in session scope. so all other pages can use that username and accountid. How can i do that. I'm new at this, so please provide me example too. Here is my code for login.jsp and validation.jsp..
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
//String name = request.getParameter( "username" );//
//session.setAttribute( "accountID",accountID );//
String connURL = "jdbc RCL";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
conn = DriverManager.getConnection(connURL, "vapatel","pjdas");
stmt = conn.createStatement();

String user=request.getParameter("userName");
String password=request.getParameter("password");
boolean entrance=false;
stmt.excuteQuery("SELECT AccountID From Password WHERE USERNAME='"+user+"'");
rs = stmt.executeQuery("SELECT * FROM Password WHERE USERNAME='"+user+"' AND PASSWORD='"+password+"'");
String dbUser = rs.getString("USERNAME");
String dbPassword= rs.getString("PASSWORD");
if ((user.equals(dbUser)) && (password.equals(dbPassword))){
if (entrance==true){%>
<jsp:forward page="form.jsp"/>
<jsp:forward page="login.jsp"/>
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 832
Hi Viral,
I have only done 1 or 2 JSP small projects and, other than that, I have done absolutely nothing with JSP (during schooling or at work). I have used the session object in this way though :
session.setValue(name of string, string value); // where you set it on the validation page
and the :
session.getValue(name of string); // on subsequent pages where you access it
I'm hoping that's what your looking for and, since I use it when using RedBeans, I hope I'm not mixing up the ***Value and the ***Property methods.
Geeta Ramasami
Ranch Hand

Joined: Mar 05, 2003
Posts: 72
In JSP, the session object is available by defualt unless and otherwise u make it false using the page directive
<%@ page session = false%>
so u can put it in session directly using
Ivan Matmati
Ranch Hand

Joined: Feb 26, 2003
Posts: 41
as you may know all the processing should be implemented in a servlet not in a jsp!!You should redesign your application in order to comply with this basic statement for robust application.
Good bye.

No Gates!<p>SCPJ 1.4<br />SCWCD 1.4
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
well, it depends on the application you're developing, as far as jsp vs. jsp/servlet. If the application is complex, or of a decent size, then jsp/servlet makes a lot more sense as it is a lot easier on your brain as a developer to keep the presentation and logic parts of the app separated. But if it's a small app, or a quick and dirty one, there's no problem in using just jsp.
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
My $0.02, I would think about encapsulating your datasource/connection and logon logic in a bean. A servlet is fine, but in my opinion a bean would be better suited in this particular instance.
For example I would create a bean which will expose the interface of the bean through a public method called for example:

The above method will return true if a valid user or false if not.

This above method will return a value object of the user information or null if invalid.
The reason why I would choose to use a bean instead of a servlet, is that I can use this particular bean in other jsps and/or servlets without having to duplicate the business logic.
Like I said, that's my opinion.
I agree. Here's the link:
subject: How to use Session scope in jsp page
jQuery in Action, 3rd edition