私は2つのサーブレットを持っています。最初のサーブレットでは、私は、データベースからのuserIdを取得していますし、このセッション変数に格納します。2つのサーブレット間のセッション変数へのアクセス
String uId = function.getLogin(username, password); //method getting the id
HttpSession session = request.getSession();
session.setAttribute("userId", uId); // here I'm setting the session variable with the id
今私は、セッション変数からそのユーザーIDを取得する2番目のサーブレットが、java.langでの。 NullPointerExceptionがスローされています。
HttpSession session = request.getSession(true);
String uId = session.getAttribute("userId").toString();
int userId = Integer.parseInt(uId); //this is the code that I'm using in the second servlet, and throwing the NullPointerException
私は何か間違っていますか?あなたの助けをありがとう
キャストの前に 'uId'の値を確認/印刷できますか? – Viruzzo
サーブレットは同じwebappにありますか? 2つのサーブレットを呼び出すために同じブラウザを使用していますか? 2回の通話の間にどれくらい時間がかかりますか?なぜそれは整数のように見えるので、あなたはどこでもIntegerをuserIdに使用しないのですか? –
このクライアント用にまだ新しいセッションがない場合にのみ、新しいセッションが作成されます。 –