Issue
Dears,
I am using oracle Database. User can sign up in the application and can log in too.
- login.jsp -> Login form
- Signup.jsp -> User can sign up
- UserHandle.java -> to validate user details with the database.
- Controller.java -> Controller for the application.
Both the signup and login functionality is working as excepted individually in the post method of the controller. However if I am putting it together then it's not working.
Controller.java
package controller;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
/**
* Servlet implementation class Controller
*/
@WebServlet("/Controller")
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
private DataSource ds;
public void init() throws ServletException {
try {
InitialContext ic = new InitialContext();
ds = (DataSource) ic.lookup("java:comp/env/jdbc/myoracle");
} catch (Exception e) {
throw new ServletException(e);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
String address = null;
request.setAttribute("email", "");
request.setAttribute("password", "");
request.setAttribute("loginerror", "");
request.setAttribute("signupMessage", "");
if (action == null) {
address = "/login.jsp";
}
else if (action.equals("signup")) {
address = "/signup.jsp";
}
request.getServletContext().getRequestDispatcher(address).forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection con = null;
try {
con = ds.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String loginValidate = request.getParameter("loginValidate");
String signupValidation = request.getParameter("signupValidation");
//Validate Sign up
if (signupValidation.equals("yes")) {
userHandle user = new userHandle(con);
String email = request.getParameter("emailsignup");
String password = request.getParameter("passwordsignup");
try {
if (user.signupValidation(email, password)) {
request.setAttribute("signupMessage", "Succesfully Logged in...");
request.getServletContext().getRequestDispatcher("/success.jsp").forward(request, response);
} else {
request.setAttribute("signupMessage", "Invalid User/Name and password!!");
request.getServletContext().getRequestDispatcher("/signup.jsp").forward(request, response);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Validate login.
else if (loginValidate.equals("yes")) {
userHandle user1 = new userHandle(con);
String e = request.getParameter("email");
String p = request.getParameter("password");
try {
if (user1.userValidate(e, p)) {
request.setAttribute("loginerror", "Scucess");
request.getServletContext().getRequestDispatcher("/success.jsp").forward(request, response);
} else {
request.setAttribute("loginerror", "Invalid User Name and password!!");
request.getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
try {
con.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
Sign up is working fine and I have created vicky user with password 12.
However when I am using the vicky user to log in, I am getting the below error message:
However If I comment the Validate Sign up section in the controller than login functionality is working as excepted.
Controller.java with commented Validated Sign up Section:
package controller;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
/**
* Servlet implementation class Controller
*/
@WebServlet("/Controller")
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
private DataSource ds;
public void init() throws ServletException {
try {
InitialContext ic = new InitialContext();
ds = (DataSource) ic.lookup("java:comp/env/jdbc/myoracle");
} catch (Exception e) {
throw new ServletException(e);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
String address = null;
request.setAttribute("email", "");
request.setAttribute("password", "");
request.setAttribute("loginerror", "");
request.setAttribute("signupMessage", "");
if (action == null) {
address = "/login.jsp";
}
else if (action.equals("signup")) {
address = "/signup.jsp";
}
request.getServletContext().getRequestDispatcher(address).forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection con = null;
try {
con = ds.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String loginValidate = request.getParameter("loginValidate");
String signupValidation = request.getParameter("signupValidation");
//Validate Sign up
// if (signupValidation.equals("yes")) {
// userHandle user = new userHandle(con);
// String email = request.getParameter("emailsignup");
// String password = request.getParameter("passwordsignup");
//
// try {
// if (user.signupValidation(email, password)) {
// request.setAttribute("signupMessage", "Succesfully Logged in...");
// request.getServletContext().getRequestDispatcher("/success.jsp").forward(request, response);
// } else {
// request.setAttribute("signupMessage", "Invalid User/Name and password!!");
// request.getServletContext().getRequestDispatcher("/signup.jsp").forward(request, response);
//
// }
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
// }
//
//Validate login.
if (loginValidate.equals("yes")) {
userHandle user1 = new userHandle(con);
String e = request.getParameter("email");
String p = request.getParameter("password");
try {
if (user1.userValidate(e, p)) {
request.setAttribute("loginerror", "Scucess");
request.getServletContext().getRequestDispatcher("/success.jsp").forward(request, response);
} else {
request.setAttribute("loginerror", "Invalid User Name and password!!");
request.getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
try {
con.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
Will it be possible to help me why it's behaving like that? What I am doing wrong here? Many thanks for your time and feedback.
Login.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login</title>
</head>
<body>
<form action="/db/Controller" method="post">
Email: <input type="text" name="email" value="<%=request.getAttribute("email")%>"/>
<br>
Password: <input type="text" name="password" value="<%=request.getAttribute("password")%>"/>
<br>
<input type="submit" value="Login" />
<input type="hidden" name="loginValidate" value="yes"/>
<br>
</form>
<h2><%= request.getAttribute("loginerror") %></h2>
<br>
<a href="/db/Controller?action=signup"> Click here to sign up... </a>
</body>
</html>
userHandler.java
package controller;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class userHandle {
private String email;
private String password;
private Connection con;
public userHandle() {
}
public userHandle(Connection con) {
this.con = con;
}
public boolean userValidate(String email, String password) throws SQLException {
String sql = "select count(*) as count from users where email = ? and password = ?";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, email);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
int count = 0;
if(rs.next()) {
count=rs.getInt("count");
}
if( count ==0) {
return false;
}
return true;
}
public boolean exists(String email) throws SQLException {
String sql = "select count(*) as count from users where email = ?";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, email);
ResultSet rs = stmt.executeQuery();
int count = 0;
if(rs.next()) {
count=rs.getInt("count");
}
stmt.close();
if( count ==0) {
return false;
}
return true;
}
public boolean signupValidation(String email, String password) throws SQLException {
if (exists(email)){
return false;
}
else {
String sql = "insert into users values(?,?)";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, email);
stmt.setString(2, password);
int count = stmt.executeUpdate();
if(count == 0 ) {
return false;
}
}
return true;
}
}
success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Logged in...
</body>
</html>
signup.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Sign up...</title>
</head>
<body>
<h1> Welcome to the Club. Sign up for free!!!!</h1>
<form action="/db/Controller" method="post">
<br>
Email: <input type="text" name="emailsignup"/>
<br>
password: <input type="text" name="passwordsignup"/>
<br>
<input type="hidden" value="yes" name="signupValidation"/>
<input type="submit" value="Submit" />
</form>
<h2><%=request.getAttribute("signupMessage") %></h2>
<a href="/db/Controller" >Click here to log in...</a>
</body>
</html>
Solution
It seems you're missing request parameter "signupValidation"
in login.jsp
where it should be set like this:
<form action="/db/Controller" method="post">
Email: <input type="text" name="email" value="<%=request.getAttribute("email")%>"/>
<br>
Password: <input type="text" name="password" value="<%=request.getAttribute("password")%>"/>
<br>
<input type="submit" value="Login" />
<input type="hidden" name="loginValidate" value="yes"/>
<input type="hidden" name="signupValidation" value="no"/>
<br>
</form>
Similarly, you need to set missing parameter "loginValidate"
in signup.jsp
.
Also, you could avoid NullPointerException
when comparing some known constants to your request parameters in the controller:
if ("yes".equals(signupValidation)) { // no NPE here if signupValidation is null
// ... the rest of your code
}
Answered By - Alex Rudenko