【发布时间】:2012-11-14 23:53:35
【问题描述】:
我正在尝试在验证用户登录凭据后创建会话。我导入了以下内容:
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;
}
}
【问题讨论】:
-
HttpSession session = request.getSession();你在哪里做的,那个方法/构造函数的签名是什么? -
@BheshGurung 我在一个名为 Login 的 Java 类和一个名为 login 的公共方法中执行此操作,该方法返回一个字符串。我是 Java 新手,所以希望这就是您想要的。
-
所以你没有在任何地方声明变量
request。您实际上需要将请求对象传递给它。你从哪里调用该方法?应该是public String login(HttpServletRequest request) { -
您需要将该调用移至某个 servlet。 JSP 中的那种代码只是一种不好的做法。