Meaningless Drivel is fun!*
The moose likes JSP and the fly likes page redirect problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "page redirect problem" Watch "page redirect problem" New topic
Author

page redirect problem

Amrut Bingi
Greenhorn

Joined: Apr 08, 2013
Posts: 5
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
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

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
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

And, welcome to the Ranch!
Amrut Bingi
Greenhorn

Joined: Apr 08, 2013
Posts: 5
thanks buddy.
actually i'm newbie in jsp. i don't know how to write servlets.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

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

Joined: Apr 08, 2013
Posts: 5
hmmm ok. thanks for the suggestion.
Amrut Bingi
Greenhorn

Joined: Apr 08, 2013
Posts: 5
but at this point of time I just want to make it work because my submission is their day after tomorrow
Siva Meka
Greenhorn

Joined: Aug 11, 2008
Posts: 6

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

OCJP1.6, OCA 9i , IBM SOA Certified
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61421
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Siva Meka
Greenhorn

Joined: Aug 11, 2008
Posts: 6

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
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

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

Joined: Apr 08, 2013
Posts: 5
Hi paul,
you were right. Its the third case. there was an sql exception with drivermanager. thank you for your reply.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

Glad it turned out to be something simple.
Parth Chavda
Greenhorn

Joined: Apr 21, 2013
Posts: 5

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+"'");
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: page redirect problem