Creating the servlet in Hibernate through NetBeans IDE by R4R Team

Now we need a servlet file for making connection for this we need to go in the file menu select -> new file -> web -> servlet ->

 Above code is shown below:


package mypack;

import java.io.IOException;

import java.io.PringWriter;

import java.servlet.ServletException;

import java.servlet.HttpServlet;

import java.servlet.http.HttpServletRequest;

import java.servlet.http.HttpServletResponse;


public class MyServlet1 extends HttpServlet

{


public void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

  {

response.setContentType("text/html;charset=UTF-8");

 Above Selected code is given below:


String first_name,last_name,age,profession;

first_name = request.getParameter("first_name");

last_name = request.getParameter("last_name");

age = request.getParameter("age");

profession = request.getParameter("profession");

SessionFactory sf = new Configuration().configure().buildSessionFactory();

Session s = sf.openSession();

Transaction tx = null;

try

  {

tx = s.beginTransaction();

Person a = new Person(first_name,last_name,age,profession);

s.save(a);

tx.commit();

  }catch(Exception e)

     {

       if(tx!=null) tx.rollback();

     }

    finally

       {

          if(s!=null)

            {

               s.close();

            }

        }

Above Selected code is given below which is shown in the image:

try(PrintWriter out = response.getWriter())

  {

 out.println("<!DOCTYPE=html>");

out.println("<html>");

out.println("<head>");

out.println("<title>Welcome</title>");

out.println("</head>");

out.println("<body>");

out.println("<h1>Servlet at  my request at "+ first_name +"</h1>");

out.println("</body>");

out.println("<html>");

}

}

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!