创建Java会话

时间:2012-11-14 23:53:35

标签: java jsp session

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

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

1 个答案:

答案 0 :(得分:0)

我认为您正在尝试在Java类中使用JSP代码。只有在JSP请求中才有一个可以在scriplets中使用的隐式对象。

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