legalizeSINCE88
legalizeSINCE88

Reputation: 97

HttpSession with Servlet + Java not working

i have the following pice of code 'anmelden.java':

@WebServlet("/anmelden")
public class anmelden extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String benutzer = request.getParameter("benutzer"); 
    String passwort = request.getParameter("passwort");

    try {
        PrintWriter out = response.getWriter();
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/test","admin","*****");
        PreparedStatement stmt = con.prepareStatement("SELECT benutzer,passwort,rolle FROM login WHERE benutzer = ? AND passwort = ?");
        stmt.setString(1, benutzer);
        stmt.setString(2, passwort);
        ResultSet rs = stmt.executeQuery();


        if(rs.next())
        {

            HttpSession session = request.getSession();
            session.setAttribute("benutzer", rs.getString("benutzer"));
            RequestDispatcher dis = request.getRequestDispatcher("mandant.jsp");
            dis.forward(request, response);

            out.print("1");

        }
        else
        {
            out.print("Benutzername und/oder Passwort falsch");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

This is my jsp file 'login.jsp':

        $("#anmelden").click(function(){

            var benutzer = $("#benutzer").val();
            var passwort = $("#passwort").val();

            if(benutzer == "" || passwort == "")
                {
                return;
                }
            $.ajax({
                url:"anmelden",
                type:"POST",
                data:"benutzer="+benutzer+"&passwort="+passwort
            }).success(function(data){              
                var erfolg = data;
                if(erfolg == "1")
                {
                    window.location.href="http://localhost:8080/PSD/mandant.jsp";                       
                    }
                else
                    {
                    $("#ok").text(erfolg);
                    }
            });
        });

As u can see i tries to set the name coming from my DB into my session Attribute. I want to use the Attribute in my 'mandant.jsp' file. But it dosen't work - all what happens is, that my 'login.jsp' file which makes the ajax call, print the code from 'mandant.jsp' into my div as text. So it dosen't opend the next page as i want -.-

But if i comment out the HttpSession block then it works fine but then i can't use ,of course,the session Attribute.

So what's wrong or what must i change so that this code works?

Many thanks

Upvotes: 0

Views: 1005

Answers (2)

Roberto Linares
Roberto Linares

Reputation: 2225

You are mixing two types of communication here, from the JSP page you are making an ajax call but from the Servlet you are making a Dispatch redirect.

If you want the login page to be redirected after a a successful login then don't call the Servlet with an ajax call and better do a form submit.

If you rather want to only check credentials on the servlet and redirect from the client then keep the ajax call but avoid the request dispatcher in the servlet and return a success/error code instead. Then capture that code from the ajax response and redirect to a successful page if you want.

Upvotes: 1

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85781

This is because this part of the code:

RequestDispatcher dis = request.getRequestDispatcher("mandant.jsp");
dis.forward(request, response);

is generating the HTML from mandant.jsp file using the request object (along with HttpSession and ServletContext) to fulfill any Expression Language and writing this HTML into the response. Just remove these lines and you'll be ok.

Upvotes: 1

Related Questions