Java EE 02-First Servlet

10
Introduction to Introduction to Java Enterprise Java Enterprise Edition Edition Configuring JBoss within Eclipse Write your first Servlet Fernando Gil [email protected] Marzo 2012

description

How to create your first Java Servlet Using Eclipse and JBoss

Transcript of Java EE 02-First Servlet

Page 1: Java EE 02-First Servlet

Introduction to Introduction to Java Enterprise Java Enterprise

EditionEditionConfiguring JBoss within Eclipse

Write your first Servlet

Fernando [email protected]

Marzo 2012

Fernando [email protected]

Marzo 2012

Page 2: Java EE 02-First Servlet

Agenda

How to configure JBoss AS within Eclipse in order to start the develop of JavaEE applications.

We'll write our first Servlet class. We'll discuss the deployment descriptor.

Page 3: Java EE 02-First Servlet

JBoss AS 6.1 & Eclipse Indigo

First download Eclipse IDE for Java Developers from: http://www.eclipse.org/downloads and JBoss 6.1 from: http://www.jboss.org/jbossas/downloads/

Open Eclipse.

Create a New Server:

Go to File/New/Other

Select Server OptionSearch JBoss 6.xBrowse JBoss Home

Directory (the folder

where you saved

JBoss)Click Finish

Page 4: Java EE 02-First Servlet

Important

If you can not find JBoss 6.x in the servers list, try to Download additional server adapters:

Sometimes eclipse can not find the JBoss adapters, in that case check this video tutorial to solve the problem: Eclipse & JBoss AS 6

Page 5: Java EE 02-First Servlet

Dynamic Web Project

Open Eclipse and select File/New/Dynamic Web Project

Set the Project Name and the location

In Target Runtime we must select the runtime of JBoss 6.x

The Dynamic web module version will be set automatically

We can Click Finish

Page 6: Java EE 02-First Servlet

Creating our Servlet

In order to keep the example simple, we'll create a servlet selecting File/New/Class and set the values like in the picture:

Page 7: Java EE 02-First Servlet

The Code

package com.dss.tut;

import java.io.*;import javax.servlet.*;import javax.servlet.http.*;

public class FirstServlet extends HttpServlet {/* The service() method handles requests and creates responses. * The servlet container automatically calls this method when * it gets any request for this servlet.*/public void service(HttpServletRequest req, HttpServletResponse resp)

throws IOException, ServletException{PrintWriter pw = resp.getWriter();

pw.println("<html>");pw.println(" <head></head>");pw.println(" <body>");pw.println(" <h1>Hello! this is my first

Servlet</h1>");pw.println(" </body>");pw.println("</html>");

}}

Page 8: Java EE 02-First Servlet

Deployment Descriptor We must indicate to the Application Server the configurations of our Servlets. The Deployment

descriptor is a XML file to accomplish this task. We will talk more about this file later. By the moment we need to create a new file called web.xml in the directory: WebContent/WEB-INF of our project.

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

<servlet><!-- Define our servlet indicating one name to identify it and

the class which represent it including the package --><servlet-name>First</servlet-name><servlet-class>com.dss.tut.FirstServlet</servlet-class>

</servlet> <servlet-mapping>

<!-- Link one existent servlet with an url. Whit this we can specify the way we can access the Servlet by the browser--><servlet-name>First</servlet-name><url-pattern>/MyServlet</url-pattern>

</servlet-mapping></web-app>

Page 9: Java EE 02-First Servlet

Compilation and Deployment

Now we just need to select our project, right click, Run As.., Run on Server, Select our JBoss Server and wait.

Remember the url pattern that we specified in the deployment descriptor, because that is the way we'll access to the Servlet.

Page 10: Java EE 02-First Servlet

Next Lesson:Next Lesson:JPS TechnologyJPS Technology

Fernando [email protected]

Marzo 2012

Fernando [email protected]

Marzo 2012