Reputation: 11
I'm making webpage where users can search for a rented product by rentID, in the Servlet I want to invoke a DAO method to find the rented product based on that rentID and redirect the user to a new webpage if the rent is found or not.
This is the JSP where users search product by ID
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org /TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h3>Find Rent</h3><br>
<form action="FindRent" method="GET">
<fieldset>
<p>
<label for="id">Rent ID</label>
<input type="text" name="id"/>
</p>
<p>
<input type="submit" value="Search" />
</p>
</fieldset>
</form>
</body>
</html>
This is the DAO implementation.
public class RentHibernateDao implements RentDao {
@Override
public void saveRent(Rent rent) {
SessionFactory factory =
HibernateUtil.getSessionFactory();
Session session = factory.openSession();
session.getTransaction().begin();
session.save(rent);
session.getTransaction().commit();
session.close();
}
@Override
public Rent find(Integer rentId) {
SessionFactory factory =
HibernateUtil.getSessionFactory();
Session session = factory.openSession();
session.getTransaction().begin();
Rent rent=(Rent)session.get(Rent.class, rentId);
session.close();
return rent;
}
@Override
public void EndRent(Integer rentId) {
SessionFactory factory =
HibernateUtil.getSessionFactory();
Session session = factory.openSession();
session.getTransaction().begin();
Rent rent=this.find(rentId);
rent.setRealReturnDate(new Date());
rent.getProduct().changeStatus();
session.saveOrUpdate(rent);
session.getTransaction().commit();
session.close();
}
And this is the Servlet. How can I invoke the method?
public class FindRent extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Rent find() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Upvotes: 0
Views: 2450
Reputation: 2288
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int rentId= Integer.parseInt(request.getParameter("id")); // capture the rent id
RentDao dao= new RentHibernateDao(); // create the dao object
dao.find(rentId); // If successful, it returns you an object of Rent class
// means Id is there
// put your processing logic here
request.getRequestDispatcher("desiredpage.jsp").forward(request, response); // forward to your desired page
}
Upvotes: 1