Creating a servlet in Jboss

All

Intro

After playing with Undertow,  we can actually create a servlet and deploy in JBoss, which is quite simple, especially if you follow the Eclipse steps.

1. New Project – Dynamic Web Project

2. Create the Web server in Java Resources/src/package you want.

3. Change the .jsp so then your servlet can be used.

4. Change the Web.xml so then we track where is the servlet address.

Servlet

servlet.jsp

<?xml version=”1.0″ encoding=”ISO-8859-1″ ?>
<%@ page language=”java” contentType=”text/html; charset=ISO-8859-1″
pageEncoding=”ISO-8859-1″%>
<html xmlns=”http://w…content-available-to-author-only…3.org/1999/xhtml”&gt;
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″ />
<title>Hello World</title>
</head>
<body>

<%

String hello = “Hello Mike”;

%>

I came here to say: <% out.println(hello); %>

<form>
First name:
<input type=”text” name=”firstname” />
Last name:
<input type=”text” name=”lastname” />
</form>

<form action=”HelloServlet”>
<input type=”submit” value=”Send” />
</form>

</body>
</html>

Web.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE web-app PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.5//EN” “http://java.sun.com/dtd/web-app_2_5.dtd”&gt;
<web-app xmlns=”http://java.sun.com/xml/ns/javaee&#8221; version=”2.5″>
<display-name>WebApp-01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>servlet.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>

The servlet.java

public class HelloServlet extends HttpServlet {

static int port = 8080;
static int bufferSize = 337680;
static int responseSize = 100;
static String host = “localhost”;

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
StringBuffer requestURL = req.getRequestURL();

resp.setBufferSize(bufferSize);

resp.setContentType(“text/plain”);
resp.setCharacterEncoding(“UTF-8”);
resp.setContentLength(responseSize);

PrintWriter pw = resp.getWriter();
pw.write(requestURL.toString());
pw.write(Integer.toString(bufferSize));

for (int i = 0; i < responseSize; i++) {
pw.append(‘X’);
}

pw.close();
}

Deploy

We can deploy with Jboss in Deploy or we can Deploy straight on Eclipse, if we configure the server in Eclipse, for the lazy like me. So then just

Tutorial

The best tutorial I found about is this one

REFs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s