In this tutorial you will learn about the Servlet on Netbeans and its application with practical example.
Servlet on Netbeans
We have discussed servlet methods and also seen some example of Servlet in our previous article. Now let’s create a simple project in Netbeans IDE and understand Servlet step by step.
Step 1:- First we need to create a project so click on File in netbeans as like below image.
Step 2:- In Second step you have to click on New Project and then need to on Java Web after that select Web Application as like below image.
Step 3:- After selecting Name and Location of Project, you have to click on Next And then Finish. Check below image for reference.
Step 4:- You will get default index.html file. We will do some modification in this file later. In this step we will create servlet, for this please check below images.
Do right click on default Package and then select Servlet. You will get screen like below image.
Enter Your Servlet Name and then click Next. Check below Image.
Note: Make Sure you have clicked on check box because it use for declare your servlet in web.xml file. We are all set with Servlet creation. Click on Finish.
Step 5:- Index.html: This file will be our welcome file, on click button it will redirect to our TestServlet.java class. Check below code.
1 2 3 4 5 6 7 8 9 10 11 12 |
<html> <head> <title>W3Adda.com Servlet Example</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <form action="TestServlet"> <input type="submit" value="Click"/> </form> </body> </html> |
Output :-
This file output will look like below image.
Step 6:-
TestServlet.java: This is our Test Servlet class, Please check below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class TestServlet extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>W3adda.com Servlet Test</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>"); out.println("</body>"); out.println("</html>"); } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. * * @return a String containing servlet description */ @Override public String getServletInfo() { return "Short description"; }// </editor-fold> } |
Output :-
So we are done with this example, have a look to output image.