0

I am trying to create a session once user log in credientials are verified. I imported the following:

import javax.servlet.http.HttpSession;

I am using the following code:

HttpSession session = request.getSession(); 
session.setAttribute("sessionName","sessionValue");

However I am getting the following error in relation to request

request cannot be resolved.

My full code:

package com.q1labs.qa.xmlgenerator.controller.managesession;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import javax.servlet.http.HttpSession;
import com.q1labs.qa.xmlgenerator.model.db.DbLoginQueries;


public class Login {

    private DbLoginQueries query = new DbLoginQueries();

    public String login(String username, String password) throws SQLException, FileNotFoundException, ClassNotFoundException, IOException{

        String returnUrl = "";
        //Code verifiying users credentials (Code has been removed)

        //If valid is true then credentials are correct
        if(valid == true){
        //If credentials are correct then create a session
        HttpSession session = request.getSession(); 
        session.setAttribute("sessionName","sessionValue");

            returnUrl = "home.jsp";
        }else{
            returnUrl = "index.jsp";
        }

        return returnUrl;
    }
}
Colin747
  • 4,955
  • 18
  • 70
  • 118
  • possible duplicate of [HttpServletRequest cannot be resolved can you help?](http://stackoverflow.com/questions/1674224/httpservletrequest-cannot-be-resolved-can-you-help) – Brian Agnew Nov 14 '12 at 23:55
  • `HttpSession session = request.getSession();` where are you doing that, what is the signature of the that method/constructor? – Bhesh Gurung Nov 14 '12 at 23:56
  • @BheshGurung I am doing it in a Java class called Login and a public method called login which returns a string. I'm new to Java so hopefully that's what you're looking for. – Colin747 Nov 14 '12 at 23:59
  • 1
    So you don't have the variable `request` declared anywhere there. You need actually pass the request object to it. Where are you calling that method from? It should be like `public String login(HttpServletRequest request) {` – Bhesh Gurung Nov 15 '12 at 00:00
  • I'm calling the method from a .jsp page. – Colin747 Nov 15 '12 at 00:01
  • 1
    You need to move that invocation to some servlet. That kind of code in JSP is just a bad practice. – Bhesh Gurung Nov 15 '12 at 00:03
  • I've updated my post to include more of my code. What I'm trying to do is when a user posts the code my jsp page, I want to verify it and if correct create a session. – Colin747 Nov 15 '12 at 00:08
  • NullPointerException is thrown instead your request gets resolved ever. – Roman C Nov 15 '12 at 00:45
  • request.getSession(true) or request.getSession(false) to create new session or carry the old one – Hussain Akhtar Wahid 'Ghouri' Nov 15 '12 at 10:37

1 Answers1

0

It looks for me that you are trying to use a JSP code inside a Java class. Only in JSP request is an implicit object that can be used in scriplets.

In Java one needs to use servlets and override methods doGet(HttpServletRequest req, HttpServletResponse resp) or doPost(HttpServletRequest req, HttpServletResponse resp)

Alex
  • 7,007
  • 18
  • 69
  • 114
  • So when creating a session and handling them it all has to be done within the jsp page and cannot be done in a Java class? – Colin747 Nov 15 '12 at 00:10
  • IN a Java servlet one can get session inside doGet or doPost from the request argument: request.getSession(); – Alex Nov 15 '12 at 00:28