16.10.2015 Views

Getting Started with WebSphere Application Server

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

58 <strong>Getting</strong> <strong>Started</strong> <strong>with</strong> <strong>WebSphere</strong> <strong>Application</strong> <strong>Server</strong> Community Edition<br />

3.5 Servlets<br />

A servlet is a mechanism in Java to provide dynamic Web content by processing HTTP<br />

requests and returning HTTP responses in the form of HTML and XML. To create a servlet<br />

follow this procedure:<br />

1. With your project selected, go to File -> New -> Servlet<br />

2. Create a package called servlets and use the name HelloWorld as shown in<br />

Figure 3.17; then click Finish.<br />

Figure 3.17 - Creating a servlet<br />

3. An editor will be started and will open the file HelloWorld.java which will<br />

include a template that makes it easy to write Servlet code. Add the following<br />

lines to the doGet method and save it:<br />

try{<br />

PrintWriter out = response.getWriter();<br />

out.println("Hello World");<br />

}<br />

catch(Throwable theException){<br />

theException.printStackTrace();<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!