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;
}
}
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)