wood burning stoves*
The moose likes Servlets and the fly likes Error in the code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Error in the code" Watch "Error in the code" New topic
Author

Error in the code

mshaikh shaikh
Greenhorn

Joined: Feb 20, 2013
Posts: 24
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

Joined: Feb 21, 2013
Posts: 9

Where did you keep the jsp file? In context root?


You just need to know where to find it!!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to CodeRanch mshaikh shaikh
not sure: think about out.close() . for more detail search here .
mshaikh shaikh
Greenhorn

Joined: Feb 20, 2013
Posts: 24
jsp file is in web pages folder
mohammed R farook
Greenhorn

Joined: Feb 27, 2013
Posts: 13
code seems to be fine . what you are getting when login failed ??? any 404 error or some thing else
mshaikh shaikh
Greenhorn

Joined: Feb 20, 2013
Posts: 24
after login failed i just get a blank page
Debal Guha
Greenhorn

Joined: Feb 21, 2013
Posts: 9

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

Joined: Feb 20, 2013
Posts: 24
not works still get same error
Debal Guha
Greenhorn

Joined: Feb 21, 2013
Posts: 9

can you take a screenshot of your exploded directory structure from context root and paste here?
mshaikh shaikh
Greenhorn

Joined: Feb 20, 2013
Posts: 24
Context root


[Untitled.jpg]

mohammed R farook
Greenhorn

Joined: Feb 27, 2013
Posts: 13
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

Joined: Feb 20, 2013
Posts: 24
Context Path


[Untitled.jpg]

mshaikh shaikh
Greenhorn

Joined: Feb 20, 2013
Posts: 24
URL after login failed
http://localhost:8080/LoginEx/LoginAction
mshaikh shaikh
Greenhorn

Joined: Feb 20, 2013
Posts: 24
not working i have create other pages but get same error
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error in the code
 
Similar Threads
Unable to update database
How i will display error details in the same jsp page
'The requested resource is not available' error
Same Request Calling again