Quantcast
Channel: Shaun Abram » Java
Viewing all articles
Browse latest Browse all 11

HelloWorld: JSP -> Servlet

$
0
0

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 basis to create a simple browser interface to some server side process.

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.


The setup steps for a simple JSP-Servlet setup are as follows.

1) Create a JSP page called HelloWorld.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%-- 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
--%>

<c:choose>
	<c:when test="${msg==null || msg==''}">
		<c:set var="greeting" value="World" scope="page" />
	</c:when>
	<c:otherwise>
		<c:set var="greeting" value="${msg}" scope="page" />
	</c:otherwise>
</c:choose>

<html>

<head>
<title>Hello ${greeting}!</title>
</head>

<body>
<center>Hello ${greeting}!</center>
<br></br>

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

</body>
</html>

2) Create a servlet to receive the user input (This is the same as in the Html->Servlet->JSP example).
HelloWorldServlet.java

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);
	} 

}

3) Create the web application’s deployment descriptor file:
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
	<welcome-file-list>
		<welcome-file>HelloWorld.jsp</welcome-file>
	</welcome-file-list>

	<servlet>
		<servlet-name>HelloWorldServlet</servlet-name>
		<servlet-class>com.helloworld.HelloWorldServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>HelloWorldServlet</servlet-name>
		<url-pattern>/HelloWorldServlet</url-pattern>
	</servlet-mapping>

</web-app>

That’s it.
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’s servlet jar (e.g. /tomcat/lib/servlet-api.jar) file too.

And of course, just like the HTML wasn’t necessary in my first posting, the Servlet is strictly speaking not necessary either. It doesn’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):

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%-- 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
--%>

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

<html>

<head>
<title>Hello ${greeting}!</title>
</head>

<body>
<center>Hello ${greeting}!</center>
<br></br>

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

</body>
</html>

Or, use the simplest possible JSP (HelloWorldSimplestNoServlet.jsp):

Hello ${param['msg']}!
<br></br>

<FORM ACTION="/HelloWorld/HelloWorldSimplestNoServlet.jsp">
	Enter greeting recipient:
	<INPUT TYPE="TEXT" NAME="msg" value="${param['msg']}">
	<p></p>
	<INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM>

I have attached all the in the attached HelloWorldJsp.war. Be sure to import the web libraries (the jstl jars) included in the war too.


Update (11 Sep 2011): I have added a maven based war to make it easier to build. For example, simply do
    mvn package to build into a war
    mvn tomcat:deploy to assemble and deploy the WAR file to Tomcat
    mvn tomcat:run to run under an embedded Tomcat server

See Maven Tomcat Plugin for more details.


Viewing all articles
Browse latest Browse all 11

Trending Articles