CodeX
CodeX

Reputation: 135

Redirecting in java

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");
        }
}

}

Upvotes: 0

Views: 3309

Answers (2)

Andrii Mishak
Andrii Mishak

Reputation: 38

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

Upvotes: 1

Tim Smith
Tim Smith

Reputation: 11

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.

Upvotes: 0

Related Questions