Creando un Servlet en J2EE mediante Eclipse 3.4.2 y JBoss 5.0.1 GA




En las opciones de instalación, marcar "Upgrade from previous version", ya que si tenéis una versión anterior os la actualizará.

Ahora descargamos el Eclipse ganymede 3.4.2, (IDE for EE). Aquí os dejo el link de descarga directa: Eclipse 3.4.2.
Una vez tenemos el Eclipse en nuestra máquina, lo descomprimimos donde queramos y lo iniciamos. Una vez iniciado, tenemos que crear un server. Hacemos File -> New Other, y escribimos server.
Ahora lo configuramos:



Una vez tenemos el servidor configurado, creamos una pequeña aplicación que se llamara "HolaServlet", y que tendrá una pequeño ejemplo en HTML que enviará un texto a un Servlet.
Lo configuramos de la siguiente manera, con las librerías indicadas, con las clases indicadas y los ficheros de configuración.

Los ficheros a crear son los siguientes:
indice.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Ejemplo Servlet JBoss 5.0 Eclipse 3.4.2</title>
</head>
<body>
<center>
<form action="http://localhost:8080/HolaServlet/NewServlet" method=Post>
Ejemplo Servlet JBoss 5.0 Eclipse 3.4.2<br><br>
Texto a enviar al Servlet: <input type=Text name="texto"><br><br>
<input type="Submit" value="Enviar">
<input type="Reset" value="Reset">
</form>
</center>
</body>
</html>
NewServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class NewServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void init(ServletConfig conf) throws ServletException {
super.init(conf);
}
public void doPost(HttpServletRequest peticion,
HttpServletResponse respuesta) throws ServletException, IOException {
respuesta.setContentType("text/html");
PrintWriter out = respuesta.getWriter();
String nombre = peticion.getParameter("texto");
out.println("<html><body>Hola soy un Servlet y he recibido " +
"el texto: <h1><font color='red'>" + nombre + "" +
"</font></h1></body></html>");
}
}
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Welcome to JBoss 5.0</display-name>
<description>Welcome to JBoss 5.0</description>
<servlet>
<servlet-name>NewServlet</servlet-name>
<servlet-class>NewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NewServlet</servlet-name>
<url-pattern>/NewServlet</url-pattern>
</servlet-mapping>
</web-app>
una vez creado el Servlet, lo iniciamos en el server que hemos creado.

Antes, podemos publicar nuestro proyecto, haciendo botón derecho sobre el JBoss v5.0 y hacemos publish. Esto nos muestra como genera el fichero .war y lo publica en el deploy del JBoss:
Buildfile: C:\Archivos de programa\dev\eclipseJ2EE\plugins\org.eclipse.jst.server.generic.jboss_1.5.206.v20090115\buildfiles\jboss323.xml
deploy.j2ee.web:
[jar] Building jar: D:\workspaceJ2EE\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\HolaServlet.war
[move] Moving 1 file to C:\jboss-5.0.1.GA\server\default\deploy
BUILD SUCCESSFUL
Total time: 10 seconds
deploy.j2ee.web:
[jar] Building jar: D:\workspaceJ2EE\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\HolaServlet.war
[move] Moving 1 file to C:\jboss-5.0.1.GA\server\default\deploy
BUILD SUCCESSFUL
Total time: 10 seconds
Si ahora iniciamos el server, podemos consultar la web indice.html en la url : http://localhost:8080/HolaServlet/indice.html, y nos aparecerá la web en HTML que hemos creado:


Comments
Post a Comment