• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error in the code

 
mshaikh shaikh
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • 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();
}
}
 
Debal Guha
Greenhorn
Posts: 9
Eclipse IDE Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where did you keep the jsp file? In context root?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jsp file is in web pages folder
 
mohammed R farook
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after login failed i just get a blank page
 
Debal Guha
Greenhorn
Posts: 9
Eclipse IDE Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not works still get same error
 
Debal Guha
Greenhorn
Posts: 9
Eclipse IDE Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Context root
Untitled.jpg
[Thumbnail for Untitled.jpg]
 
mohammed R farook
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Context Path
Untitled.jpg
[Thumbnail for Untitled.jpg]
 
mshaikh shaikh
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
URL after login failed
http://localhost:8080/LoginEx/LoginAction
 
mshaikh shaikh
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic