0

A Web Dynamic project on eclipse,

In a .jsp file I read the username and password from the user and called the logging function in the class below

The function should validate the username and password then redirect to to a different .jsp page

The redirection part below is Not working and has errors..

 package myPackage;
import java.sql.* ;
import javax.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class javaMethods extends HttpServlet   {


public String logging(String user_id, String password){
    ResultSet request = null;
    Statement stmt =null; 
    ResultSet rs = null;
    Class.forName("com.mysql.jdbc.Driver");
    java.sql.Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/users", "root", "maram");
    stmt=conn.createStatement();
    rs=stmt.executeQuery("SELECT * FROM usersData WHERE id = '"+user_id+"'");


        if(rs.next()) //if username was found
        {
             if (rs.getString(2).equals(password))
                {

                 if(rs.getString(3).equals("admin"))
                    {

                     ////REDIRECTING///
                    response.setStatus(response.SC_MOVED_TEMPORARILY);
                    response.setHeader("Location", "admin.jsp");
                    }
                 else
                    {
                    ////REDIRECTING///
                    response.setStatus(response.SC_MOVED_TEMPORARILY);
                    response.setHeader("Location", "welcome.jsp");
                    }
                }
            else //incorrect password
                {
                System.out.println ("Incorrect password");
                }

        }
    else //user name does not exist
        {
        System.out.println ("Username does not exist");
        }
}

}
CodeX
  • 135
  • 2
  • 13
  • "... and has errors“ - a StackTrace could be helpful ^^ – Alexander Jun 29 '15 at 08:20
  • error in resolving the type of response @Alexander – CodeX Jun 29 '15 at 08:28
  • See @Andrii's answer. You need to overwrite `doGet()` and pass the `response` variable to your method. Btw. where do `String user_id` and `String password` from? Are these `response.getParameter("user_id")`... ? – Alexander Jun 29 '15 at 08:51

2 Answers2

1

Please, take a look to Redirecting a request using servlets and the "setHeader" method not working

You should implement special interface methods which have references for request and response object references. For example,

public void doGet(HttpServletRequest request, HttpServletResponse response)

Your code do not have references for that objects, response even not exists

Community
  • 1
  • 1
0

Maybe i dont undetstand something, but redirection from jsp looks like

response.sendRedirect(params);

In your code you just change response of the same page.

Tim Smith
  • 11
  • 1
  • 3