I am doing a simple form validation. I am unable to forward to jsp page. please refer to the code below. Line-B and Line-C are working fine but Line-A was producing an error
import java.io.IOException;
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 org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
@WebServlet("/client")
public class client extends HttpServlet {
private static final long serialVersionUID = 1L;
public client() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = null;
String password = null;
username = request.getParameter("username");
password = request.getParameter("password");
SessionFactory sf1 = new Configuration().configure().buildSessionFactory();
Session s1 = sf1.openSession();
s1.beginTransaction();
Query q = s1.createQuery("from userdb where username = ?");
userdb u2 = (userdb) q.setString(0, username).uniqueResult();
if (u2 != null) {
if (u2.password.equals(password)) {
/*Line - A*/ request.getRequestDispatcher("home.jsp").include(request, response);
} else {
request.setAttribute("error", "Invalid username/password, please try again");
/*Line - B*/ request.getRequestDispatcher("Clientlog.jsp").include(request, response);
}
s1.getTransaction().commit();
s1.close();
}
userdb u1 = new userdb();
u1.setUsername(username);
u1.setPassword(password);
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
s.save(u1);
s.getTransaction().commit();
s.close();
/*Line - C*/ request.getRequestDispatcher("home.jsp").include(request, response);
}
}
Line-B and Line-C were successfully going to the requested page but Line-A was producing this ERROR
one way to overcome this is (since Line-A and Line-C are same), if i am using a flag=0
to check if it goes inside the block containing Line-A and marking the flag as 1, and moving the execution to Line-C it's working
import java.io.IOException;
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 org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
@WebServlet("/client")
public class client extends HttpServlet {
private static final long serialVersionUID = 1L;
public client() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int flag = 0;
String username = null;
String password = null;
username = request.getParameter("username");
password = request.getParameter("password");
SessionFactory sf1 = new Configuration().configure().buildSessionFactory();
Session s1 = sf1.openSession();
s1.beginTransaction();
Query q = s1.createQuery("from userdb where username = ?");
userdb u2 = (userdb) q.setString(0, username).uniqueResult();
if (u2 != null) {
if (u2.password.equals(password)) {
flag = 1;
/*Line - A*/ // request.getRequestDispatcher("home.jsp").include(request, response);
} else {
request.setAttribute("error", "Invalid username/password, please try again");
/*Line - B*/ request.getRequestDispatcher("Clientlog.jsp").include(request, response);
}
s1.getTransaction().commit();
s1.close();
}
if(flag == 0){
userdb u1 = new userdb();
u1.setUsername(username);
u1.setPassword(password);
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
s.save(u1);
s.getTransaction().commit();
s.close();
}
/*Line - C*/ request.getRequestDispatcher("home.jsp").include(request, response);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.