为不同的servlet使用一个会话属性

时间:2015-07-30 06:26:17

标签: java session servlets

我为用户创建了一个登录环境,我从数据库中检索了用户名和密码以进行登录。并在会话中将用户名设置为字符串。现在我想为另一个servlet使用相同的用户名。但是我不能在两个servlet中使用相同的字符串。如何为另一个servlet使用相同的会话属性.Below是我的代码。 ArrayList

loginservlet

import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; @WebServlet(urlPatterns = {"/LoginServlet"}) public class LoginServlet extends HttpServlet { String userid, pwd; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); Connection connection = null; Statement statement = null; userid = request.getParameter("uname"); pwd = request.getParameter("pass"); try { dbconnector dbc = new dbconnector(); connection = dbc.Open(); PreparedStatement ps = connection.prepareStatement("select * from member where uname='" + userid + "' and pass='" + pwd + "'"); ResultSet rs = ps.executeQuery(); if (rs.next()) { HttpSession session = request.getSession(); session.setAttribute("userid", userid); //out.println("welcome " + userid); //out.println("<a href='logout.jsp'>Log out</a>"); response.sendRedirect("success.jsp"); } else { request.setAttribute("errorMessage", "Invalid user or password"); RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp"); rd.include(request, response); } } catch (Exception e) { e.printStackTrace(); } } } 这里我想使用会话值userid

profile servlet

5 个答案:

答案 0 :(得分:2)

在第一个servlet中,您将值存储在session中。

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

同样,您需要在第二个session中从servlet检索这些值,例如

HttpSession session = request.getSession();
String userid=(String)session.getAttribute("userid");//cast to String, default its Object

现在在第二个userid中使用此servlet变量。

HttpSession#getAttribute()

preparestatement像这样遵循以避免sql注入

session

答案 1 :(得分:0)

首先将值存储在会话中,如下所示

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

像这样访问会话值

  HttpSession session=request.getSession();
  String username = session.getAttribute("userid").toString() ;

答案 2 :(得分:-1)

在'profile'servlet中,您可以使用以下方法查找HttpSession。

HttpSession httpSession = request.getSession(false);

'false'将确保在登录servlet之前调用配置文件servlet时不创建新会话。

获得会话后,您可以读取之前存储的属性值。

答案 3 :(得分:-1)

我能用什么意思?

如果你想在其他servlet中使用存储值,请执行此操作 HttpSession session = request.getSession(); String user = session.getAttribute(&#34; userid&#34;);

如果你想使用相同的密钥(&#34; userid&#34;)来为其设置新值 你不能这样做,因为会话范围在任何地方都很常见,即使你创建了新的会话对象,你在后台使用相同的会话,所以你应该选择新的字符串。

答案 4 :(得分:-1)

试试这个......

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

@WebServlet(urlPatterns = {"/LoginServlet"})
public class LoginServlet extends HttpServlet {

    String userid, pwd;

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Connection connection = null;
        Statement statement = null;

        userid = request.getParameter("uname");
        pwd = request.getParameter("pass");

    try {
        HttpSession session = request.getSession(true);//true if first session else use false
        if (SessionManager.checkUserSession(session)) {
            dbconnector dbc = new dbconnector();
            connection = dbc.Open();
            PreparedStatement ps = connection.prepareStatement("select * from member where uname='" + userid + "' and pass='" + pwd + "'");

            ResultSet rs = ps.executeQuery();
            if (rs.next()) {

                session.setAttribute("userid", userid);
                //out.println("welcome " + userid);
                //out.println("<a href='logout.jsp'>Log out</a>");
                response.sendRedirect("success.jsp");
            } else {

                request.setAttribute("errorMessage", "Invalid user or password");
                RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp");
                rd.include(request, response);

            }
        }
        request.setAttribute("statuscode", "HTTP_STATUS_INVALID_SESSION");
    } catch (Exception e) {
        e.printStackTrace();
    }

}

}

sessionmanager的代码

public class SessionManager {

    public static boolean checkUserSession(HttpSession loginSession) {
        if ((loginSession == null) || (loginSession.toString().isEmpty())) {
            return false;
        } else if (loginSession.isNew()) {
            return false;
        } else {
            return true;
        }
    }
}

用于更改用户名或使用

进行访问
HttpSession session = request.getSession();
String userid=(String)session.getAttribute("userid");