How to Create a session in Servlet

will somone show me the code to create a session in servlet?
if a session can be created in servlet file, it can be used all the jsp files in a same web ?
Thanks!!!

Session in Java web application is started by the web server itself.
In a servlet you can get the session by
HttpSession session = request.getSession();
It will return the current session associated with this request, or if the request does not have a session, creates one.

Similar Messages

  • How to Create a Session variable in JSP?

    How to create a session variable and add attributes to it?
    For example i want to create a session variable called "name" and add value "user" to it? if i try to do it with setAttribute(), it is giving error as both is of type string? how to do it? kindly help me

    This is variable what is working in my aplication
    String variable;
    if(user == null){
                   user=new String("");
         }//if you don'y do this you will have null pointer exception
    session.setAttribute("user",user);//put value
              String name=(String)session.getAttribu("user");      //get value;
    I hope this will help

  • How to create a session variable in Apex?

    Hello colleagues,
    Is there someone do know how to create a session variable in Apex?
    How to get them?
    Thanks
    Best Regards

    Hello,
    What do you mean by 'session variable'? You mean something that stores the value int he users session state?
    I really recommend reading the 2-Day Developer guide documentation, there is a complete section on session state -
    http://download.oracle.com/docs/cd/E10513_01/doc/appdev.310/e10499/concept.htm#CIHCFHBD
    Hope this helps,
    John
    Blog: http://jes.blogs.shellprompt.net
    Work: http://www.apex-evangelists.com
    Author of Pro Application Express: http://tinyurl.com/3gu7cd

  • How to create a session using call transaction method.

    hi , this is nagaraju,
    How to create a session using call transaction method.

    Hi,
    About Data Transfer In R/3 System
    When a company decides to implement the SAP R/3 to manage business-critical data, it usually does not start from a no-data situation. Normally, a SAP R/3 project comes into replace or complement existing application.
    In the process of replacing current applications and transferring application data, two situations might occur:
    •     The first is when application data to be replaced is transferred at once, and only once.
    •     The second situation is to transfer data periodically from external systems to SAP and vice versa.
    •     There is a period of time when information has to be transferred from existing application, to SAP R/3, and often this process will be repetitive.
    The SAP system offers two primary methods for transferring data into SAP systems. From non-SAP systems or legacy system. These two methods are collectively called “batch input” or “batch data communication”.
    1. SESSION METHOD
    2. CALL TRANSACTION
    3. DIRECT INPUT
    First step for both the methods is to upload the data to internal table. From Internal Table, the data is transferred to database table by two ways i.e., Session method and Call transaction.
    Session is intermediate step between internal table and database table. Data along with its action is stored in session i.e., data for screen fields, to which screen it is passed, the program name behind it, and how the next screen is processed.
    When the program has finished generating the session, you can run the session to execute the SAP transactions in it. Unless session is processed, the data is not transferred to database table.
    A technique similar to SESSION method, while batch input is a two-step procedure, Call Transaction does both steps online, one after the other. In this method, you call a transaction from your program.
    SESSION METHOD
    Data is not updated in database table unless Session is processed.
    No sy-subrc is returned.
    Error log is created for error records.
    Updation in database table is always synchronous
    CALL TRANSACTION
    Immediate updation in database table.
    Sy-subrc is returned.
    Errors need to be handled explicitly
    Updation in database table can be synchronous Or Asynchronous.
    Regards,
    Sruthi.

  • How to create a session cookie on demand

    Hi,
    I search the web but couldn't find anything related to creating session cookies on demand. I want to create a session cookie storing encrypted user tokens when there is none, for example, when the first page is called.
    The encryption part is OK, but I want how can I intercept every call to a set of pages and create the session cookie if it doesn't exist.
    I'm using ADF, of course, and Weblogic.
    Anyone can provide some examples or source code?
    Thanks.

    Cookies are accessible via the http request and response, there you can add new cookies and or change existing ones.
            ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
            HttpServletResponse response = (HttpServletResponse) ectx.getResponse();
            // get existing cookies
            Cookies [] cookies =((HttpServletRequest)ectx.getRequest()).getCookies();
            // create and set a new one
            Cookie cookie = new Cookie( "key", "value" );
            response.addCookie( cookie );This code should work in a bean. After setting the cookie you need to implement a servlet filter or a page phase listener where you check the requested url and then check for your cookie.
    Timo

  • How to create a session by AppsLocalLogin.jsp

    first, in auth.jsp we check fnd_global.user_name against "ANONYMOUS" to see whether user is logged on.
    if not, we invoke AppsLocalLogin.jsp to create a session.
    http://server:port/OA_HTML/AppsLocalLogin.jsp?requestUrl=report.jsp%3Fparam1%3Dyyy
    the login page shows, page re-directed to report.jsp.
    however fnd_global.user_name doesn't work. it still returns "ANONYMOUS".
    if user logged on OA normally, then the whole thing worked. it seems fnd_global.user_name doesn't like the way we login.
    how should we invoke AppsLocalLogin.jsp so it'll create a proper session?
    part of report.jsp:
    WebAppsContext ctx =WebRequestUtil.validateContext(request, response);
    conn = ctx.getJDBCConnection();
    sql = "select fnd_global.user_name from dual";
    (we tried to invoke RF.jsp instead.
    doesn't work because we don't know how to pass the param1 to report.jsp thru RF.jsp)

    This is variable what is working in my aplication
    String variable;
    if(user == null){
                   user=new String("");
         }//if you don'y do this you will have null pointer exception
    session.setAttribute("user",user);//put value
              String name=(String)session.getAttribu("user");      //get value;
    I hope this will help

  • How to create another session for a help page ?

    Hello
    I am trying to create help system for an application, which would be similar to apex's own help system. The main thing is that the help page/application/procedure would run independently of the calling application, and therefore it needs to create another session, I suppose ? How do you implement that ? I have also tried to find description of the package wwv_flow_help, but haven't found anywhere. Is it useful here ?
    Thanks for any comments

    If the link is from a page in your application then that's exactly what you want to do using javasScript to open a new window but keeping the same session ID in the request. In this way, when the new request is received by the Application Express engine, it will have the session cookie that goes with the specified session ID.
    Scott
    P.S. You can help us by telling us your first name and putting it into your handle and/or profile.

  • How to create crystal reports using servlets/jsp

    plase any body help me how to create and interact with crystal reports using java

    To use the inproc RAS SDK with CR.NET, you'd have to purchase either (1) Crystal Reports XI Release 2 Developer edition, and apply Service Pack 2 or above, or (2) Crystal Reports 2008 (not Crystal Reports Basic that comes with Visual Studio 2008).
    Sincerely,
    Ted Ueda

  • ADF - How to create a session in ADF

    Hi, how I would to create a schema of session im my application?

    http://docs.oracle.com/javaee/5/tutorial/doc/bnaqm.html
    1-create a View object. For example:
    SELECT USU.USUARIO, USU.TIPO_USUARIO,
    USU.VENDEDOR_NUMERO,
    usu.NOMBRE,
    Usu.FUENTE_CIA, USU.COMPANIA
    FROM VENTAS.USUARIOS USU
    WHERE USU.USUARIO = upper(:pUsuario)
    2-set parameter (:pUsuario) with default value expresion:
    adf.context.securityContext.userName
    3-Use ViewObject in backing bean with session scope.
    package view.backing;
    import java.io.IOException;
    import java.util.Date;
    import javax.faces.context.ExternalContext;
    import javax.faces.context.FacesContext;
    import javax.faces.event.ActionEvent;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import oracle.adf.model.BindingContext;
    import oracle.adf.model.binding.DCBindingContainer;
    import oracle.jbo.Row;
    public class Usuario {
    String TipoUsuario, VendedorNumero, Nombre, FuenteCia, Compania;
    Date Hoy;
    Boolean Leido = false;
    public void Logout(ActionEvent actionEvent) throws IOException {
    String javaScriptText = "window.close();";
    ExternalContext ectx =
    FacesContext.getCurrentInstance().getExternalContext();
    HttpServletResponse response = (HttpServletResponse)ectx.getResponse();
    String url = "VentasMenu.jspx";
    HttpSession session = (HttpSession)ectx.getSession(false);
    session.invalidate();
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("expires", "0");
    response.setHeader("Pragma", "no-cache");
    try {
    response.sendRedirect(url);
    FacesContext.getCurrentInstance().responseComplete();
    } catch (IOException e) {
    e.printStackTrace();
    FacesContext.getCurrentInstance().responseComplete();
    public Date getHoy() {
    return new java.util.Date();
    void iniciarValores() {
    DCBindingContainer bindings = (DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
    //ViewObject find user data from DB
    Row rowUsuario = bindings.findIteratorBinding("voUsuario1Iterator").getRowAtRangeIndex(0);
    if (rowUsuario != null) {
    TipoUsuario = rowUsuario.getAttribute("TipoUsuario").toString();
    VendedorNumero = rowUsuario.getAttribute("VendedorNumero").toString();
    Nombre = rowUsuario.getAttribute("Nombre").toString();
    FuenteCia = rowUsuario.getAttribute("FuenteCia").toString();
    Compania = rowUsuario.getAttribute("Compania").toString();
    Leido = true;
    public String getTipoUsuario() {
    if (!Leido) {
    iniciarValores();
    return TipoUsuario;
    public String getVendedorNumero() {
    if (!Leido) {
    iniciarValores();
    return VendedorNumero;
    public String getNombre() {
    if (!Leido) {
    iniciarValores();
    return Nombre;
    public String getFuenteCia() {
    if (!Leido) {
    iniciarValores();
    return FuenteCia;
    public String getCompania() {
    if (!Leido) {
    iniciarValores();
    return Compania;
    }

  • How  to create Tab pages using servlet

    I want to create a tab pages which can carry different pages. Can you help me on this?? Provided if you have some guideline or program which can help me.
    Thanks
    Message was edited by:
    wesleygch

    those kinda tab pages should be coded :) there is no built-in support for them!
    like BalusC wrote you better sould export the layout into CSS, then, you have the option of using javascript when switching between tabs.
    if you want to avoid using of javascript or you want to do it serverside, then you'll have to write that tab handling in your jsp page

  • How do i Create charts using JSP/Servlet & Database

    I have to create charts which shows the graph of stock exchange.
    i have a database that keeps the data for creating charts.
    But i did not know how to create charts using jsp-servlet.
    Any Example might help me to go forward.
    Any help will be really appreciated.
    Please Advice me.

    JFreeChart - You can generate the charts then convert them to image formats (PNG and JPEG) all using the JFreeChart API
    http://www.jfree.org/jfreechart/

  • Session in Servlet and JSP

    Hi
    How do I maintain a session in the scenario below:
    (1) Servlet A creates a session
    (2) Servlet A dispatch JSP A
    (3) JSP A capture some user inputs and post to Servlet B
    So far both (1) and (2) are working fine, but to get on to (3) the session is lost upon reaching Servlet B
    Pls Advise...

    Hi mirage2000,
    You mean that when you are calling the Servlet B, the request object has no session inside it's a bit strange.
    Let's take the following example wich is quite simple:
    Servlet A:
    package servlet;
    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class ServletA extends HttpServlet {
    public void init (ServletConfig config) throws ServletException {
    super.init(config);
    public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    doPost(req, res);
    public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    String nomClient = "Toto";
    HttpSession session = null;
    try {
    session = req.getSession();
    req.setAttribute("servlet.a.nom", nomClient );
    getServletConfig().getServletContext().getRequestDispatcher("/jspa.jsp").forward(req, res);
    } catch (Exception e) {
    System.out.println("Pas cool !");
    JSP A:
    <%@ page import="java.util.*"%>
    <% String nom = (String)request.getAttribute("servlet.a.nom"); %>
    <HTML>
    <HEAD><TITLE>Age ?</TITLE></HEAD>
    <BODY>
    <BR>
    <BR>
    What is your age ?
    <BR>
    <FORM ACTION="../servlet/ServletB" METHOD="post">
    <INPUT TYPE="text" NAME="age" SIZE="3">
    <BR>
    <INPUT TYPE="submit" NAME="action" VALUE="SEND"> </FORM>
    </BODY>
    </HTML>
    Servlet B:
    package servlet;
    import java.io.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    public class ServletB extends HttpServlet {
    public void init (ServletConfig config) throws ServletException {
    super.init(config);
    public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    doPost(req, res);
    public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    HttpSession session = null;
    PrintWriter out = res.getWriter();
    try {
    String action = req.getParameter("action");
    if (action.equals("SEND") ) {
    res.setContentType(CONTENT_TYPE);
    session = req.getSession();
    String age = (String)req.getParameter("age");
    String nomClient = (String)session.getAttribute("servlet.a.nom");
    out.println("<HTML><HEAD></HEAD><BODY>");
    out.println("Hello mister <B>" + nomClient + "</B>");
    out.println("<BR>You are <B>" + age + "</B> years old !!");
    out.println("</BODY></HTML>");
    out.flush();
    out.close();
    } catch (Exception e) {
    System.out.println("Pas cool !");
    This exemple is really simple and is working perfectly on my Apache Tomcat test server.
    Be more precise maybe you forgot to do some stuff.
    Regards
    Julien

  • Create a session for a web service in jdeveloper10g

    Hi All,
    Question 1: How to create a session for a java web service created in jdeveloper10g.
    Question 2: How to restrict a particular user for accessing a java web service.
    I have done this by using key store. But when creating the java web service client, again we have to configure the key store, which should not happen in my case. Hence i like to create a method for login in a web service, create a session id for the user if he is a valid user and send the session id back to web service client. Using the session id they can access other methods in the web service.
    Can any one suggest how I can accomplish this. Thanks in advance.

    I don't know this can be disscussed or not.
    Please delete my topic if this can't be disscussed here.
    Thank you.

  • How to create session in JSP & Servlet

    Hi All
    I'm really new to JSP & Servlet. So I want to know how to create sessions and how to pass details to another web page
    Thanks
    Padma

    You can easily pass objects from one jsp or servlet to another.
    On your servlet execute method (for example), you can do this as follows:
    HttpSession session = request.getSession();
    User u = new User();
    session.setAttribute("user", u);And get it again on other servlets:
    HttpSession session = request.getSession();
    User user = (User) session.getAttribute("user");I hope this helps you.

  • How do I create a session?

    In order to use methods like getValidSessions() in the Session class of the SessionAPI for iPlanet Portal Server, I need a Session object. There is no obvious way to construct one based on the javadocs. I can get one from a SessionID object, but in order to create a SessionID object, I need a HTTP request or an encrypted string, i.e. SessionID( String sid ). How can I create a Session object, or how can I create a SessionID object (which will allow me to get a Session object)? Any help is much appreciated. NOTE: I'm building a plain Java utility to use the Portal Server API to retrieve session and profile information, and I'm not implementing it as a servlet.

    From com.iplanet.portalserver.providers.ProviderAdapater use the getSession() method whish returns com.iplanet.portalserver.session.Session object
    Then from Session class use the method getValidSessions()method

Maybe you are looking for