• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error in the code

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have following program can any one help me to find out wat is te error in it, it is login successfully, but on login failed it is not redirect to the loginfom.jsp page,

LoginForm.jsp file
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Page </title>
</head>
<body>
<%=request.getAttribute("errorMessage")%>
<form action="LoginAction" method="POST">
<table border="0">
<tr>
<td>User ID</td>
<td><input type="text" name="uid" /></td></tr>
<tr>
<td>Password</td>
<td> <input type="password" name="psw" /></td></tr>
<tr> <td colspan="2"><input type="submit" value="Log Me !" />
</td></tr>
</table>
</form>
</body>
</html>

LoginAction.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String uid, psw; // creating local variables for storing data which are taken from Form Fields.
uid = request.getParameter("uid"); // getting data from Form Field using Fileld name.
psw = request.getParameter("psw");
Class.forName("com.mysql.jdbc.Driver"); // creating database connection
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user", "root", "nbuser"); //loading jdbc Drivers
String q = "select * from user where user='" + uid + "' AND password='" + psw + "'"; // writing SQL Query
java.sql.Statement st = con.createStatement();
ResultSet rs = st.executeQuery(q); // executing SQL Query
String dpsw, duid; // creating local variables for storing data which are taken from Form databse.
while (rs.next()) {
duid = rs.getString(1);
dpsw = rs.getString(2);

if(duid.equals(uid) && dpsw.equals(psw)) {
HttpSession s = request.getSession(); // Creating Session Object
s.setAttribute("suid", uid); // Creating Session Value
RequestDispatcher rd=request.getRequestDispatcher("homepage.jsp");
request.setAttribute("uid", uid);
rd.forward(request, response);
}
else if(!(duid.equals(uid) && dpsw.equals(psw))){
request.setAttribute("errorMessage", "Invalid username or password");
RequestDispatcher rd=request.getRequestDispatcher("LoginForm.jsp");

rd.forward(request, response);
}

}
} catch (Exception e) {
out.print(e);
} finally {
out.close();
}
}
 
Greenhorn
Posts: 9
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did you keep the jsp file? In context root?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch mshaikh shaikh
not sure: think about out.close() . for more detail search here .
 
mshaikh shaikh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
jsp file is in web pages folder
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code seems to be fine . what you are getting when login failed ??? any 404 error or some thing else
 
mshaikh shaikh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
after login failed i just get a blank page
 
Debal Guha
Greenhorn
Posts: 9
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you try opening and closing the PrintWriter from within your if block since, you don't need to work with response till the login is successful. See if your luck favors!!
 
mshaikh shaikh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not works still get same error
 
Debal Guha
Greenhorn
Posts: 9
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you take a screenshot of your exploded directory structure from context root and paste here?
 
mshaikh shaikh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Context root
Untitled.jpg
[Thumbnail for Untitled.jpg]
 
mohammed R farook
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok good .. can you share the url where you got in blank page . the thing is if you are using forward the url is not chaged ..

also try to use some new error pages while login failed instead of "LoginForm.jsp" then you will get some idea
 
mshaikh shaikh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Context Path
Untitled.jpg
[Thumbnail for Untitled.jpg]
 
mshaikh shaikh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
URL after login failed
http://localhost:8080/LoginEx/LoginAction
 
mshaikh shaikh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not working i have create other pages but get same error
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic