0

验证用户登录凭据后,我正在尝试创建会话。我导入了以下内容:

import javax.servlet.http.HttpSession;

我正在使用以下代码:

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

但是,我收到以下有关的错误request

request cannot be resolved.

我的完整代码:

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

1 回答 1

0

在我看来,您正在尝试在 Java 类中使用 JSP 代码。只有在 JSP 请求中才有可以在 scriplet 中使用的隐式对象。

在 Java 中,需要使用 servlet 并覆盖方法 doGet(HttpServletRequest req, HttpServletResponse resp) 或 doPost(HttpServletRequest req, HttpServletResponse resp)

于 2012-11-15T00:08:02.127 回答