Netbeans Tutorial Servlet

11
Netbeans Tutorial for Servlet Programming 1. Create New Project using File Menu New Project or by Ctrl + Shift + N, you will get following wizard. 2. In Step 1, select Java Web from Categories, and select Web Application from Projects. Then click on Next button. 3. In Step 2 give the suitable Project Name (ServletExample) and Project Location (D:\) to your web application and then click on Next button.

Transcript of Netbeans Tutorial Servlet

Page 1: Netbeans Tutorial Servlet

Netbeans Tutorial for Servlet Programming

1. Create New Project using File Menu New Project or by Ctrl + Shift + N, you will get following wizard.

2. In Step 1, select Java Web from Categories, and select Web Application from Projects. Then click on Next button.

3. In Step 2 give the suitable Project Name (ServletExample) and Project Location (D:\) to your web application and then click on Next button.

Page 2: Netbeans Tutorial Servlet

4. Step 3 is about Server and Settings, in that you can select server, J2EE version etc. Choose J2EE 1.4 as Java EE Version in this step, and click on Next button.

5. Step 4 is about to choose Frameworks, in that you can add different framework to your web application like Java Server Faces, Hibernate etc. Then click on Finish button.

Page 3: Netbeans Tutorial Servlet

6. New web application named ServletExample is created. By default one index.jsp file is created and it has been set as a welcome page. You can see project window on left pane, in that directory structure of current project is shown. You can see different directories like Web Pages, Source Packages, and Libraries etc.

7. Now we want create a new servlet file, for that there are different ways. One of them is just right click on ServletExample (Project Name), select New Servlet as shown below.

Page 4: Netbeans Tutorial Servlet

8. Now you will get following Steps wizard. You have already chosen file type so no need to go for Step1, in Step 2 you have to provide appropriate class name (FirstServlet) to you Servlet, and click on Next button.

Page 5: Netbeans Tutorial Servlet

9. In Step 3 you have to configure servlet deployment (web.xml), in that you have to provide Servlet name which is similar to class name and URL Pattern for accessing that servlet page by that way.

10.

Check at left pane in project window, FirstServlet.java file is created under Source Packages in default package. You can also check in right pane when servlet file is created some default code are added automatically like a public class with similar name with file name etc.

Page 6: Netbeans Tutorial Servlet

11. You may check Web.xml file for Servlet Configuration. Open Web Pages WEB-INF web.xml you can see deployment of FirstServlet is done by netbeans. No need to do manually.

12. Now in FirstServlet.java file do following changes.

Page 7: Netbeans Tutorial Servlet

13. Save FirstServlet and go to the Run menu select Run File option to execute servlet fie. You can also do with Shift + F6.

14. Now will get following dialog box to set Servlet Execution URI. By default URI is same that you had given at the time of file creation. Just click on Ok button.

Page 8: Netbeans Tutorial Servlet

15. You will get following output in your default browser.

16.

We can also set some parameters for servlet file. For that open web.xml file again and click on Servlet tab as below. For setting initial parameter click on Add button as shown below. Set parameters and its value and click on ok button.

Page 9: Netbeans Tutorial Servlet

17. Now check the xml documentation by clicking on XML tag as below.