Results 1 to 8 of 8
Thread: servlet issue
- 05-19-2010, 06:14 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 7
- Rep Power
- 0
servlet issue
I am running this program but instead of redirecting to the next page, the servlet LoginServlet just opens a blank page. Can anyone spot where i may have gone wrong? The servlet is in a package called Login.
Java Code:<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body bgcolor="blue"> <form name="f1" method="post" action="LoginServlet"> <table> <tr> <td>Username</td> <td><input type="text" name="t1" ></td> </tr> <tr> <td>Password</td> <td><input type="password" name="t2"></td> </tr> <tr> <td></td> <td><input type="submit" name="b1" value="LOGIN"></td> </tr> </table> </form> </body> </html>Java Code:package Login; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class LoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter pw = response.getWriter(); response.setContentType("text/html"); String name = request.getParameter("t1"); String password = request.getParameter("t2"); try { Connection con = DBConnect.DbConnection.getConnection(); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("SELECT username, password " + "FROM users " + "WHERE username = " + name + " " + "AND password = " + password); if (rs != null) { HttpSession session = request.getSession(); session.setAttribute("username", name); response.sendRedirect("next.jsp"); } else { response.sendRedirect("elogin.jsp"); } } catch (Exception e) { e.getMessage(); } } }
- 05-20-2010, 06:27 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Check the logs to see what exceptions were thrown.
- 05-20-2010, 06:50 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 7
- Rep Power
- 0
I seem to have it working now, the only issue i am having is the redirecting to the error page. There are no errors but its not redirecting. It redirects if the details are correct but if they are wrong it does not .
Java Code:Connection con = DBConnect.DbConnection.getConnection(); Statement st = con.createStatement(); String user = request.getParameter("t1"); String pass = request.getParameter("t2"); String strSQL = "SELECT username, password FROM users " + "WHERE username = '" + user + "' " + "AND password = '" + pass + "'"; ResultSet rs = st.executeQuery(strSQL); String username, password; HttpSession session = request.getSession(); while (rs.next()) { username = rs.getString("username"); password = rs.getString("password"); if (user.equals(username) && pass.equals(password)) { response.sendRedirect("next.jsp"); } else { response.sendRedirect("errorlogin.jsp"); } }
- 05-20-2010, 06:54 AM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Is the name of the JSP correct? Is it in the same folder as the next.jsp?
You should still have got an error message somewhere not just a blank page.
P.S Hopefully you are closing the connections in some finally block that you have not posted.
- 05-20-2010, 09:58 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Your logic's wrong.
First off:
This is just eating an exception. You should be doing e.printStackTrace().Java Code:} catch (Exception e) { e.getMessage(); }
Second:
If the username and password aren't in the db the resultset will be empty, so rs.next() will be false. So the code is never entered.Java Code:while (rs.next()) { username = rs.getString("username"); password = rs.getString("password"); if (user.equals(username) && pass.equals(password)) { response.sendRedirect("next.jsp"); } else { response.sendRedirect("errorlogin.jsp"); } }
There's no point rechecking the username/password against what was enetered since the SQL has already done that. So you want to do an "if" not a "while", and the else will be your error login page.
- 05-20-2010, 10:00 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
In fact why are you getting the result from the db based on "t1" and "t2" and then comparing the returned values against "username" and "password"?
- 05-20-2010, 11:43 AM #7
Member
- Join Date
- Feb 2009
- Posts
- 7
- Rep Power
- 0
Thanks for the help, i removed the comparison between username and password and used the if statement and it works great! You help was much appreciated!! Thanks again
- 05-20-2010, 11:55 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Similar Threads
-
Performance issue - How to Queue and process Servlet Requests
By gemgb in forum Java ServletReplies: 1Last Post: 09-27-2009, 03:36 AM -
Memory issue in servlet application in a tomcat container
By bedhinesh in forum Java ServletReplies: 2Last Post: 07-24-2009, 01:26 PM -
how can i link html+servlet+xml to form servlet
By balachandarr in forum Java ServletReplies: 0Last Post: 04-15-2009, 04:06 PM -
Servlet - apache tomcat/glassfish server shuts down on servlet usage
By StewS in forum Java ServletReplies: 1Last Post: 12-02-2008, 12:23 AM -
javax.servlet.ServletException: Wrapper cannot find servlet class util.t2
By osval in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 03:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks