Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet How to implement session variables

Have Programming Question? Ask it here!
 
 
 


Kiran
How to implement session variables
1 Answer(s)      4 years and 2 months ago
Posted in : JSP-Servlet


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,

Advertisement
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
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
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 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
How To Turn On the Session Support?
How To Turn On the Session Support?  How To Turn On the Session Support
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
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 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? Thanks   Hi, You can use following code for logout: <? session_start(); session_destroy(); header
Variables
Variables  What are the difference between Static variables, instance variables and local variables
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 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 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...  
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
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 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
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
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
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
Session
Session   how to session maintaining in servlet with use of hidden fieds
How do I set environment variables from Java?
How do I set environment variables from Java?  How do I set environment variables from Java
Session
Session  How fined session is new or old
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
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
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
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
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 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 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
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
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
How to access session values through Ajax?
How to access session values through Ajax?  Suppose in a servlet a variable userName is kept in session. How can I access this variable from JSP through AJAX? Is it possible
session
session  how can transfer data of one page to another page using session in java swing
How to create Hibernate session factory name?
How to create Hibernate session factory name?  Hi, I am new to Hibernate and I would like to learn how to create Hibernate Session factory name... Hibernate Session How to create SessionFactory in Hibernate 4.3.1 Also you learn
How to write a session code - Java Beginners
How to write a session code   Once Again Thanks Deepak...Thanks for continuing responce I want using session in my project plz help me how to write a session code plz write a session code and post answer my personal id
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
how to make session timeout programatically - JSP-Servlet
how to make session timeout programatically  hi, i have a login page with username and password. how to make session timeout for 10minutes... to make session timeout programatically, you can use the method
How can we implement Pop-up in JQuery without Plugin?
How can we implement Pop-up in JQuery without Plugin?  Hi Sir How can we Implement pop-up in JQuery without using plug-in. Please provide me code with detailed answer. Thanks in advance.   Hello Friend You need
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
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
How can i implement the calculator programe in jsp code
How can i implement the calculator programe in jsp code  Please send code for the calculator using jsp technologies please guidelines to me.   Here is a simple jsp calculator code that accepts two numbers from the user
Advertisements
 

 

 

DMCA.com