<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Shaun Abram &#187; servlet</title>
	<atom:link href="http://www.shaunabram.com/tag/servlet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.shaunabram.com</link>
	<description>Java and Technology weblog</description>
	<lastBuildDate>Wed, 18 Jan 2012 00:39:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Spring MVC Hello World</title>
		<link>http://www.shaunabram.com/springmvchelloworld/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=springmvchelloworld</link>
		<comments>http://www.shaunabram.com/springmvchelloworld/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 05:07:03 +0000</pubDate>
		<dc:creator>sabram</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[OpenSource]]></category>
		<category><![CDATA[Products]]></category>
		<category><![CDATA[helloworld]]></category>
		<category><![CDATA[jsp]]></category>
		<category><![CDATA[servlet]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[springmvc]]></category>
		<category><![CDATA[webapp]]></category>
		<category><![CDATA[webframework]]></category>

		<guid isPermaLink="false">http://www.shaunabram.com/?p=1376</guid>
		<description><![CDATA[There is a good Getting Started with Spring MVC blog post over on the Spring team blog. I have created several Spring MVC projects for both work and play, and am attaching my own simple version of the HelloWorld example here, based on the Spring blog example. Find my maven ready source here. Like my [...]]]></description>
			<content:encoded><![CDATA[<p>There is a good <a href="http://blog.springsource.com/2011/01/04/green-beans-getting-started-with-spring-mvc/">Getting Started with Spring MVC</a> blog post over on the Spring team <a href="http://blog.springsource.com/">blog</a>.</p>
<p>I have created several <a href="http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html">Spring MVC</a> projects for both work and play, and am attaching my own simple version of the HelloWorld example here, based on the Spring blog example.<br />
Find <a href="http://www.shaunabram.com/attachments/SpringMVCHelloWorld.zip">my maven ready source here</a>.<br />
Like my previous <a href="http://www.shaunabram.com/helloworld-jsp-servlet/">JSP/Servlet example</a>, I find these templates useful for getting prototypes up and running.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaunabram.com/springmvchelloworld/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HelloWorld: JSP -&gt; Servlet</title>
		<link>http://www.shaunabram.com/helloworld-jsp-servlet/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=helloworld-jsp-servlet</link>
		<comments>http://www.shaunabram.com/helloworld-jsp-servlet/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 23:35:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[jsp]]></category>
		<category><![CDATA[servlet]]></category>

		<guid isPermaLink="false">http://www.shaunabram.com/?p=785</guid>
		<description><![CDATA[A while ago, I posted a simple HelloWorld Html->Servlet->JSP setup. Even though the html part is unnecessary (a JSP can obviously handle that alone), I deliberately included it as a template for Html/Servlet/JSP interaction. I have used it a bunch of times to get simple Proof of Concept projects up and running, or as a [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago, I posted a  simple <a href="http://www.shaunabram.com/helloworld-html-servlet-jsp/">HelloWorld Html->Servlet->JSP setup</a>. Even though the html part is unnecessary (a JSP can obviously handle that alone), I deliberately included it as a template for Html/Servlet/JSP interaction. I have used it a bunch of times to get simple Proof of Concept projects up and running, or as a basis to create a simple browser interface to some server side process. </p>
<p>Most of the more recent projects I have used it on however have used straight JSP->Servlet, with no .html file involved. So I am now posting that setup, including some simple JSTL EL and conditional logic.</p>
<p><span id="more-785"></span><br />
The setup steps for a simple JSP-Servlet setup are as follows.</p>
<p>1) Create a JSP page called HelloWorld.jsp</p>
<pre>
<code>&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
&lt;%-- Note the above core tag lib will require adding the 2 jstl jars to your web app lib folder.
     You can download them from here: https://jstl.dev.java.net/download.html
--%&gt;

&lt;c:choose&gt;
	&lt;c:when test="${msg==null || msg==''}"&gt;
		&lt;c:set var="greeting" value="World" scope="page" /&gt;
	&lt;/c:when&gt;
	&lt;c:otherwise&gt;
		&lt;c:set var="greeting" value="${msg}" scope="page" /&gt;
	&lt;/c:otherwise&gt;
&lt;/c:choose&gt;

&lt;html&gt;

&lt;head&gt;
&lt;title&gt;Hello ${greeting}!&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;center&gt;Hello ${greeting}!&lt;/center&gt;
&lt;br&gt;&lt;/br&gt;

&lt;FORM ACTION="/HelloWorld/HelloWorldServlet"&gt;
	Enter greeting recipient:
	&lt;INPUT TYPE="TEXT" NAME="msg" value="${msg}"&gt;
	&lt;p&gt;&lt;/p&gt;
	&lt;INPUT TYPE="SUBMIT" VALUE="Submit"&gt;
&lt;/FORM&gt;

&lt;/body&gt;
&lt;/html&gt;</code>
</pre>
<p>2) Create a servlet to receive the user input (This is the same as in the Html->Servlet->JSP example).<br />
HelloWorldServlet.java</p>
<pre>
<code>package com.shaunabram.helloworld;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorldServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

		//get inputs
		String msg = req.getParameter("msg");

		//any additional processing can be done here

		//set outputs
		req.setAttribute("msg", msg);

		//forward to jsp
		req.getRequestDispatcher("/HelloWorld.jsp").forward(req, res);
	} 

}</code>
</pre>
<p>3) Create the web application’s deployment descriptor file:<br />
web.xml</p>
<pre>
<code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;web-app&gt;
	&lt;welcome-file-list&gt;
		&lt;welcome-file&gt;HelloWorld.jsp&lt;/welcome-file&gt;
	&lt;/welcome-file-list&gt;

	&lt;servlet&gt;
		&lt;servlet-name&gt;HelloWorldServlet&lt;/servlet-name&gt;
		&lt;servlet-class&gt;com.helloworld.HelloWorldServlet&lt;/servlet-class&gt;
	&lt;/servlet&gt;

	&lt;servlet-mapping&gt;
		&lt;servlet-name&gt;HelloWorldServlet&lt;/servlet-name&gt;
		&lt;url-pattern&gt;/HelloWorldServlet&lt;/url-pattern&gt;
	&lt;/servlet-mapping&gt;

&lt;/web-app&gt;</code>
</pre>
<p>That&#8217;s it.<br />
Again, the easiest way I have found for this kind of development is to create in Eclipse as a Dynamic Web Project (available via the Web Tools Platform that comes with the in the “Eclipse IDE for Java EE Developers” package). When you have carried out the above steps, just right click the project -> Run As -> Run on Server (assuming you have a server like Tomcat or GlassFish setup in Eclipse…). You will also need to reference your chosen server&#8217;s servlet jar (e.g. /tomcat/lib/servlet-api.jar) file too.</p>
<p>And of course, just like the HTML wasn&#8217;t necessary in my first posting, the Servlet is strictly speaking not necessary either. It doesn&#8217;t do much and is more a placeholder for, or interface to, the server side Java logic your app may require. If you want just the  JSP implementation with no servlet involved, just use this jsp (HelloWorldNoServlet.jsp):</p>
<pre>
<code>&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
&lt;%-- Note the above core tag lib will require adding the 2 jstl jars to your web app lib folder.
     You can download them from here: https://jstl.dev.java.net/download.html
--%&gt;

&lt;c:choose&gt;
	&lt;c:when test="${param.msg==null || param.msg==''}"&gt;
		&lt;c:set var="greeting" value="World" scope="page" /&gt;
	&lt;/c:when&gt;
	&lt;c:otherwise&gt;
		&lt;c:set var="greeting" value="${param.msg}" scope="page" /&gt;
	&lt;/c:otherwise&gt;
&lt;/c:choose&gt;

&lt;html&gt;

&lt;head&gt;
&lt;title&gt;Hello ${greeting}!&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;center&gt;Hello ${greeting}!&lt;/center&gt;
&lt;br&gt;&lt;/br&gt;

&lt;FORM ACTION="/HelloWorld/HelloWorldNoServlet.jsp"&gt;
	Enter greeting recipient:
	&lt;INPUT TYPE="TEXT" NAME="msg" value="${param.msg}"&gt;
	&lt;p&gt;&lt;/p&gt;
	&lt;INPUT TYPE="SUBMIT" VALUE="Submit"&gt;
&lt;/FORM&gt;

&lt;/body&gt;
&lt;/html&gt;</code>
</pre>
<p>Or, use the simplest possible JSP (HelloWorldSimplestNoServlet.jsp):</p>
<pre>
<code>Hello ${param['msg']}!
&lt;br&gt;&lt;/br&gt;

&lt;FORM ACTION="/HelloWorld/HelloWorldSimplestNoServlet.jsp"&gt;
	Enter greeting recipient:
	&lt;INPUT TYPE="TEXT" NAME="msg" value="${param['msg']}"&gt;
	&lt;p&gt;&lt;/p&gt;
	&lt;INPUT TYPE="SUBMIT" VALUE="Submit"&gt;
&lt;/FORM&gt;</code>
</pre>
<p>I have attached all the in the attached <a href="http://www.shaunabram.com/attachments/HelloWorldJsp.war">HelloWorldJsp.war</a>. Be sure to import the web libraries (the jstl jars) included in the war too.</p>
<hr />
Update (11 Sep 2011): I have added a <a href="http://www.shaunabram.com/attachments/HelloWorld-mvn.zip">maven based war</a> to make it easier to build. For example, simply do </p>
<ul>
<code>mvn package</code> to build into a war<br />
<code>mvn tomcat:deploy</code> to assemble and deploy the WAR file to Tomcat<br />
<code>mvn tomcat:run</code> to run under an embedded Tomcat server
</ul>
<p>See <a href="http://mojo.codehaus.org/tomcat-maven-plugin/deployment.html">Maven Tomcat Plugin</a> for more details.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaunabram.com/helloworld-jsp-servlet/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>HelloWorld: Html -&gt; Servlet -&gt; JSP</title>
		<link>http://www.shaunabram.com/helloworld-html-servlet-jsp/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=helloworld-html-servlet-jsp</link>
		<comments>http://www.shaunabram.com/helloworld-html-servlet-jsp/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 09:56:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[el]]></category>
		<category><![CDATA[jsp]]></category>
		<category><![CDATA[servlet]]></category>
		<category><![CDATA[webapp]]></category>

		<guid isPermaLink="false">http://www.shaunabram.com/?p=657</guid>
		<description><![CDATA[A couple of times recently I have had to setup up some very simple code to display on the client side (browser) some values created on the server side based on basic user input. This has been part of some prototype/proof-of-concept work. I am posting my basic setup as an example in the form of [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of times recently I have had to setup up some very simple code to display on the client side (browser) some values created on the server side based on basic user input. </p>
<p>This has been part of some prototype/proof-of-concept work. I am posting my basic setup as an example in the form of the ubiquitous HelloWorld app. It uses a Html page to receive user input, which is submitted to a Servlet, processed, and the response displayed in a JSP using EL (Expression Language).</p>
<p>Obviously this could be done in various ways, including just JSPs, but this example acts as a template of Html/Servlet/JSP interaction.</p>
<p>1) Create a Html page called HelloWorld.html</p>
<pre>
<code>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Greeting Setup - Hello Who?&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;FORM ACTION="/HelloWorld/HelloWorldServlet"&gt;
	Enter greeting recipient:
	&lt;INPUT TYPE="TEXT" NAME="msg"&gt;
	&lt;p&gt;&lt;/p&gt;
	&lt;INPUT TYPE="SUBMIT" VALUE="Submit"&gt;
&lt;/FORM&gt;

&lt;/body&gt;
&lt;/html&gt;</code>
</pre>
<p>2) Create a servlet to receive the user input<br />
HelloWorldServlet.java</p>
<pre>
<code>package com.helloworld;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorldServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

		//get inputs
		String msg = req.getParameter("msg");

		//any additional processing can be done here

		//set outputs
		req.setAttribute("msg", msg);

		//forward to jsp
		req.getRequestDispatcher("/HelloWorld.jsp").forward(req, res);
	} 

}</code>
</pre>
<p>3) Create a JSP to display the response<br />
HelloWorld.jsp:</p>
<pre>
<code>Hello ${msg}</code>
</pre>
<p>4) Create the web application&#8217;s deployment descriptor file:<br />
web.xml</p>
<pre>
<code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;web-app&gt;
	&lt;welcome-file-list&gt;
		&lt;welcome-file&gt;HelloWorld.html&lt;/welcome-file&gt;
	&lt;/welcome-file-list&gt;

	&lt;servlet&gt;
		&lt;servlet-name&gt;HelloWorldServlet&lt;/servlet-name&gt;
		&lt;servlet-class&gt;com.helloworld.HelloWorldServlet&lt;/servlet-class&gt;
	&lt;/servlet&gt;

	&lt;servlet-mapping&gt;
		&lt;servlet-name&gt;HelloWorldServlet&lt;/servlet-name&gt;
		&lt;url-pattern&gt;/HelloWorldServlet&lt;/url-pattern&gt;
	&lt;/servlet-mapping&gt;

&lt;/web-app&gt;
</code>
</pre>
<p>That&#8217;s it. </p>
<p>The easiest way I have found for this kind of development is to create in Eclipse as a <a href="http://help.eclipse.org/help32/index.jsp?topic=/org.eclipse.wst.webtools.doc.user/topics/twcreprj.html">Dynamic Web Project</a> (available via the <a href="http://www.eclipse.org/webtools/">Web Tools Platform</a> that comes with the in the <a href="http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/galileosr1">&#8220;Eclipse IDE for Java EE Developers&#8221;</a> package). When you have carried out the above steps, just right click the project -> Run As -> Run on Server (assuming you have a server like Tomcat or GlassFish setup in Eclipse&#8230;). </p>
<p>I have <a href="http://www.shaunabram.com/attachments/HelloWorld.war">attached</a> the source in a war file.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shaunabram.com/helloworld-html-servlet-jsp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

