• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

page redirect problem

 
Amrut Bingi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys,
I have been looking for the solution for the problem I'm facing since last week.
I've written jsp page for login as follows but the problem is that its not redirecting the page to other page in either of the cases whether login is successful or not.
please help me out

<html>
<head>
<title>login</title>
</head>
<body>
<%
String uname=request.getParameter("username");
String passwd=request.getParameter("password");
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:mydsn");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from members where email='"+uname+"' and '"+"password='"+passwd+"'");
int count=0;
while(rs.next()){count=count+1;}
if(count>0){
out.println("hello"+rs.getString(1));
session.setAttribute("username", rs.getString("fname"));
response.sendRedirect("index.jsp");
conn.close();

%>
<%
}
else{
response.sendRedirect("index.jsp");
%>
<%
}}
catch(Exception e){}
%>
</body>
</html>
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are three cases, not two.

1. Login successful.

2. Login not successful.

3. An exception is thrown.

Unfortunately since your catch-block is empty, you're ignoring any exceptions which are thrown and therefore you have no idea whether Case 3 happens.

By the way I'm curious why you put that code in a JSP when it isn't intended to generate any HTML. Why isn't it in a servlet?
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, welcome to the Ranch!
 
Amrut Bingi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks buddy.
actually i'm newbie in jsp. i don't know how to write servlets.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, there really isn't any point in learning JSP by itself, since any real-life web application will use both of them. So I'd suggest you hold off writing JSPs full of scriptlets at this point, since scriptlets have been obsolete for many years now. Take a few minutes to learn about servlets and how to use them in a well-written web application.
 
Amrut Bingi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmmm ok. thanks for the suggestion.
 
Amrut Bingi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but at this point of time I just want to make it work because my submission is their day after tomorrow
 
Siva Meka
Greenhorn
Posts: 6
IntelliJ IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amrut,

Try using "Absolute URL" in sendRedirect methond instead of just "index.jsp". That should solve your problem.

More explanation:
ServletResponse.sendRedirect():
public void sendRedirect(java.lang.String location)
throws java.io.IOException

Sends a temporary redirect response to the client using the specified redirect location URL. The URL must be absolute (for example, https://hostname/path/file.html ). Relative URLs are not permitted here.
Parameters:
location - the redirect location URL

You can use requestDispatcher.forward() method if you are redirecting within the same application with a relative url.

Hope this helps
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siva Meka wrote:Try using "Absolute URL" in sendRedirect methond instead of just "index.jsp".

The URL should be sever-relative if the resource is within the same web app.

You can use requestDispatcher.forward() method if you are redirecting within the same application with a relative url.

A redirect and a forward are not the same thing and they are not interchangeable.
 
Siva Meka
Greenhorn
Posts: 6
IntelliJ IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,
Thanks for your reply. But what is the solution for Amrut's problem here?
Where is he going wrong in his code?

When I googled for this problem, one of the stackoverflow answers was having a "" statement after response.sendredirect() call. Is that a correct solution?

thanks
Siva
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Siva Meka wrote:But what is the solution for Amrut's problem here?
Where is he going wrong in his code?

When I googled for this problem, one of the stackoverflow answers was having a "" statement after response.sendredirect() call. Is that a correct solution?


That certainly wasn't what I had in mind in my original response where I pointed to the overlooked third possibility. If my guess is correct then it won't help at all.

But what I posted was just a guess. Same with what you posted. We don't know what the problem is because we haven't been told. All we know is "its not redirecting the page to other page". We don't know what is happening instead, we don't know whether errors are being thrown (but we DO know why we don't know that, and it should be fixed), we don't know whether the server catches fire, we don't know anything.
 
Amrut Bingi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi paul,
you were right. Its the third case. there was an sql exception with drivermanager. thank you for your reply.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glad it turned out to be something simple.
 
Parth Chavda
Greenhorn
Posts: 5
Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Firend first of all you must known witch error is occur so use this code after "try" block

catch(Exception e)
{
out.println("Exception :"+e.getMessage());
}


use ResultSet rs=stmt.executeQuery("select * from members where email='"+uname+"' and password='"+passwd+"'");
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic