Java EE 02-First Servlet
-
Upload
fernando-gil -
Category
Education
-
view
1.629 -
download
3
description
Transcript of Java EE 02-First Servlet
![Page 1: Java EE 02-First Servlet](https://reader036.fdocuments.in/reader036/viewer/2022082808/5552f53ab4c90587048b4d39/html5/thumbnails/1.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022082808/5552f53ab4c90587048b4d39/html5/thumbnails/2.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022082808/5552f53ab4c90587048b4d39/html5/thumbnails/3.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022082808/5552f53ab4c90587048b4d39/html5/thumbnails/4.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022082808/5552f53ab4c90587048b4d39/html5/thumbnails/5.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022082808/5552f53ab4c90587048b4d39/html5/thumbnails/6.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022082808/5552f53ab4c90587048b4d39/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022082808/5552f53ab4c90587048b4d39/html5/thumbnails/8.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022082808/5552f53ab4c90587048b4d39/html5/thumbnails/9.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022082808/5552f53ab4c90587048b4d39/html5/thumbnails/10.jpg)
Next Lesson:Next Lesson:JPS TechnologyJPS Technology
Fernando [email protected]
Marzo 2012
Fernando [email protected]
Marzo 2012