Java Servlets example using NetBeans 6. Pre-requirements: Install Java JDK 1.6 Install NetBeans IDE...

10
Java Servlets example using NetBeans 6

Transcript of Java Servlets example using NetBeans 6. Pre-requirements: Install Java JDK 1.6 Install NetBeans IDE...

Page 1: Java Servlets example using NetBeans 6. Pre-requirements: Install Java JDK 1.6 Install NetBeans IDE 6 (we will use version NetBeans IDE 6.7.1 update 16)

Java Servlets exampleusing NetBeans 6

Page 2: Java Servlets example using NetBeans 6. Pre-requirements: Install Java JDK 1.6 Install NetBeans IDE 6 (we will use version NetBeans IDE 6.7.1 update 16)

Pre-requirements: Install Java JDK 1.6 Install NetBeans IDE 6 (we will use version

NetBeans IDE 6.7.1 update 16) Remember to choose the setup of a web server

(GlassFish with

GlassFish prelude,

Apache Tomcat or both)

Page 3: Java Servlets example using NetBeans 6. Pre-requirements: Install Java JDK 1.6 Install NetBeans IDE 6 (we will use version NetBeans IDE 6.7.1 update 16)

Create a Web Application

File -> New Project -> Java Web -> Web Application Click Next, choose a name and a location for the

project. We will call it ITShop

Click Next,Choose the Server that you want to use(GlassFish or Apache Tomcat)

Click Finish

Page 4: Java Servlets example using NetBeans 6. Pre-requirements: Install Java JDK 1.6 Install NetBeans IDE 6 (we will use version NetBeans IDE 6.7.1 update 16)

Build the home page

An ‘index.jsp’ page is created automatically. Write whatever html code you wish in it. You can also use the tools from the palette on the left. We will write some hyperlink that will call the servlet

and pass a value to its doGet method. We also will put a form in that page that includes a

text input, a submit button and a reset button.

Page 5: Java Servlets example using NetBeans 6. Pre-requirements: Install Java JDK 1.6 Install NetBeans IDE 6 (we will use version NetBeans IDE 6.7.1 update 16)
Page 6: Java Servlets example using NetBeans 6. Pre-requirements: Install Java JDK 1.6 Install NetBeans IDE 6 (we will use version NetBeans IDE 6.7.1 update 16)

Create the servlet Right click the project -> New -> Servlet... Choose a name for the class (we will call it

‘PageCreatorServlet’). Click Next, choose a name for the servlet, (we will use the same name). Note that the check box of ‘Add information to deployment descriptor (web.xml)’ is checked.

Click Finish

Page 7: Java Servlets example using NetBeans 6. Pre-requirements: Install Java JDK 1.6 Install NetBeans IDE 6 (we will use version NetBeans IDE 6.7.1 update 16)

Edit the servlet The ‘PageCreatorServlet’ will be created

automatically, including its doGet and doPost methods. It also includes a ‘processRequest’ method that will be called from the doGet and doPost methods.

We can write our implementation in the doGet and doPost methods. But instead, here we will write in the processRequest method.

Page 8: Java Servlets example using NetBeans 6. Pre-requirements: Install Java JDK 1.6 Install NetBeans IDE 6 (we will use version NetBeans IDE 6.7.1 update 16)
Page 9: Java Servlets example using NetBeans 6. Pre-requirements: Install Java JDK 1.6 Install NetBeans IDE 6 (we will use version NetBeans IDE 6.7.1 update 16)

Note:

You can run the servlet alone for testing purposes instead of runing the entire project. Right click the servlet -> Run File. You can also

add some parameters to the servlet execution URI (for example: /PageCreatorServlet?DATA=“SW”).

You can also change this execustion URI. To do so, right click the servlet -> Tools -> Set Servlet Execution URI

Run the project and test it.

Page 10: Java Servlets example using NetBeans 6. Pre-requirements: Install Java JDK 1.6 Install NetBeans IDE 6 (we will use version NetBeans IDE 6.7.1 update 16)

Extra parts:

Cookies or sessions Database using JDBC