How to implement session variables

How to implement session variables

Hi,
I have a servlet which gets parameter from a jsp page(x). it is stored in a variable in servlet as shown below. the servlet response is another jsp page(y). I should put the value of amp_num in the Jsp page (y).
How should i do it? I'm trying using session.getAttribute

SERVLET:
-------------------
HttpSession session = request.getSession(false);

String amp_number = request.getParameter("amp1").toString();
int amp_num = Integer.parseInt(amp_number);
session.setAttribute("xyz",lamp_num);

JSP Page(y):
---------------
<input type="text" name="xyz" value="<%session.getAttribute(xyz); %>" size="20">

I dont need to put in a textfield, its ok if i print it. Do I need to add any path like (@servlet) because how the jsp know from which servlet i'm calling the variable xyz. please help me.

Thanks,
View Answers

June 10, 2010 at 11:33 AM

Hi Friend,

Try the following code:

1)no.jsp:

<form name="form" method="post" action="../ServletExample" >
Enter no:<input type="text" name="amp1"><input type="submit" value="submit">
</form>

2)ServletExample.java:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletExample extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
HttpSession session = request.getSession(false);

String amp_number = request.getParameter("amp1").toString();
int amp_num = Integer.parseInt(amp_number);
session.setAttribute("xyz",amp_num);
getServletConfig().getServletContext().getRequestDispatcher("/modified/action.jsp").forward(request, response);
}
}

3)action.jsp:

<%=session.getAttribute("xyz")%>

It will be helpful for you.
Thanks









Related Tutorials/Questions & Answers:
How to implement session variables - JSP-Servlet
How to implement session variables  Hi, I have a servlet which gets..._num in the Jsp page (y). How should i do it? I'm trying using session.getAttribute SERVLET: ------------------- HttpSession session = request.getSession
How can we register the variables into a session?
How can we register the variables into a session?  How can we register the variables into a session
Advertisements
How do servlets work? Instantiation, session variables and multithreading
How do servlets work? Instantiation, session variables and multithreading  How do servlets work? Instantiation, session variables and multithreading
session variables server side
session variables server side  how to hold session variable for server side..?   request.getSession().setAttribute("wellSelection", uid
set cookieless session variables in jsp
set cookieless session variables in jsp  I want to know how to set cookieless session variables in jsp, because when I run my application multiple...(because these session variables internally make use of cookie). But I don't want
how to initialise variables in jsp?
how to initialise variables in jsp?   1. what is the problem in the folloing jsp <%@ page import="net.viralpatel.struts.helloworld.form.LoginForm" %> <%@ page language="java" contentType="text/html
PHP list all session variables
All session variables and its values are maintained by the the super global variable $_SESSION. $_SESSION is an associative array. It keeps the session.... Example of PHP List All Session Variables session1.php <?php   
how to implement spring security - Spring
how to implement spring security  Hai, iam working on small login application using hibernate on Spring. I wanted to implement spring security for my login application. Can any one tell me what is SpringSecurity, and how
Variables
Variables  What are the difference between Static variables, instance variables and local variables
How to implement FTP using java
How to implement FTP using java  Hi, I am a B.tech student and I want to implement FTP using Java to transfer files and exchange files between FTP client and FTP server. Could anyone help me for How to implement FTP using java
what is a thread initial state? how to implement it?
what is a thread initial state? how to implement it?  what is a thread initial state? how to implement
How To Turn On the Session Support?
How To Turn On the Session Support?  How To Turn On the Session Support
JSP - Update displayed content & session variables when user clicks on a button - JSP-Servlet
JSP - Update displayed content & session variables when user clicks on a button  Hi, I'm trying to setup a form in which the user can click... on the page. I've spent much of the day yesterday trying to figure out how to do
Duplicated Session Variables
Duplicated Session Variables     ... a new session variable because it has been created by the JSP container for you... name. Consider we have declared a session variable true in jsp page <%@ page
how to create session for login
how to create session for login  how to create session for login? how to display uploaded resume and images in the profile
how to get javascript variables into jsp - JSP-Servlet
how to get javascript variables into jsp  How to pass variables from java script into jsp page  Hi Friend, Try it: 1)pass.jsp: var msg="Hello World" window.location.href="show.jsp?value=" + msg
JSP - Update displayed content & session variables when user clicks on a button - JSP-Servlet
JSP - Update displayed content & session variables when user clicks on a button  Hi, I'm trying to setup a form in which the user can click... on the page. I've spent much of the day yesterday trying to figure out how to do
How to implement openId java web based application?
How to implement openId java web based application?  In my web application i want to implement the OpenId just like stackoverflow.com have to login.... Please tell me how to implement it in java web application in details
How to destory session in PHP
How to destory session in PHP  Hi, I have to write a php page for logout. Please tell me what i should write? ThanksADS_TO_REPLACE_1   Hi, You can use following code for logout: <? session_start(); session
session
session  how to implement login-logout session????   Please visit the following links: http://www.roseindia.net/quickguide/tomcat/Logout.shtml http://www.roseindia.net/jsp/javascriptpagerefresh.shtml http
how to implement ajax in struts2 to operate on selectbox in jsp
how to implement ajax in struts2 to operate on selectbox in jsp  I am doing a project on struts2 in which i have a jsp page which has 2 select boxes like </s:form>` ` now i want that when i select a district
How to implement a superclass Person? - Java Beginners
How to implement a superclass Person?  How to implement a superclass... of these classes and invokes the toString() method of each of these object. how to implement the class..and how to write the toString method? please help me...  
Session
Session   how to session maintaining in servlet with use of hidden fieds
How to implement this superclass Person - Java Beginners
How to implement this superclass Person  how to Implement a superclass Person which it need to Make two classes, Student and Instructor, that inherit...,birthYear=1969],salary=65000.0]''); } } i don't know how to write a coding
How do I set environment variables from Java?
How do I set environment variables from Java?  How do I set environment variables from Java
what is cloud computing? any how we can implement?
what is cloud computing? any how we can implement?  what is cloud computing? any how we can implement
How to implement xml parsing in iphone ? -RV
How to implement xml parsing in iphone ? -RV  In .h file @interface RootViewController : UITableViewController<NSXMLParserDelegate>{ IBOutlet UITableView *page1; NSMutableArray *titlearray
How to use session in struts 1.3
How to use session in struts 1.3  i want to know how to use session in Struts specially in logIn authentication
PHP Variables Across Files
php variables across files We know how Variables and Cookies... on the user's computer or variables can't take information to every new page. For this reason we use PHP session variable. PHP session variable is a method to store
Session
Session  How fined session is new or old
Session
and user would not able to retrieve the information. So, Session provides that facility to store information on server memory. 2)Variables are stored in session...Session   why do we need session? actually what stored in a session
Session
Session   How can we set the inactivity period on a per-session basis?   We can set the session time out programmatically by using the method setMaxInactiveInterval() of HttpSession
session
Session Management in PHP  Handling session in PHP. Can anyone please explain it with the help of an existing example about, how to handle a session while a user is logged in PHP
how to implement Single Sign On in enterprise application using spring framework
how to implement Single Sign On in enterprise application using spring framework  how i can implement the single sign on in my spring based web app..i am new to spring frame work..any help will be appreciated as its urgent
getting variables
getting variables   how to get variables from servlet program to webservice program
Servlet Session
in to maintain/manage the session and the various mechanism to implement... How to manage the Session Each session is associates with timeout because HTTP...Servlet Session Sometimes it is required to maintain a number of request
How to implement link in Web app lead to report with print and save features
How to implement link in Web app lead to report with print and save features   Hi friends, I developed a web application based on Java MVC architecture and Struts framework with Tomcat appserv, and I need to implement link lead
session
session  how can transfer data of one page to another page using session in java swing
session
session  create multiple login on one page in php but how to use session
How to implement Captcha in PHP Form using GD Library
How to implement Captcha in PHP Form using GD Library  Hi, How to show Captcha in PHP Form. So, please suggest any online reference so that i will try myself. Thanks, (adsbygoogle = window.adsbygoogle || []).push
How can we know that a session is started or not?
How can we know that a session is started or not?  How can we know that a session is started
session
session  is there any possibility to call one session from another session
session
session  is there any possibility to call one session from another session
session
session  Session management in Java
PHP related - how to explot a string with commas and store into two different variables?
PHP related - how to explot a string with commas and store into two different variables?  Hi, I have a string and is like this '[tubelist 123456,54321,56789,98765]'. I would like to extract the first value after the tubelist
How do we create session factory in hibernate?
How do we create session factory in hibernate?  Hi, How do we create session factory in hibernate? thanks
How can we destroy the session, how can we unset the variable of a session?
How can we destroy the session, how can we unset the variable of a session?  How can we destroy the session, how can we unset the variable of a session
Environment variables
Environment variables  How to set Environment Variables in JAVA 6 n Tomcat 6 CLASSPATH, JAVA_HOME, PATH, & CATALINA variables plzzz plzz help me
Implement push
Implement push  Hi.. How do you implement push on a flex applications? give me answer with example so i clearly understand Thanks  Ans: push implement on a flex applications using BlazeDS Server
How to implement ajax in struts2 to operate on select box in jsp
How to implement ajax in struts2 to operate on select box in jsp  I am doing a project on struts2 in which i have a jsp page which has 2 select boxes like <s:form action=""> <s:select id="d" name="dist" onchange

Ads